-
Notifications
You must be signed in to change notification settings - Fork 0
Test #83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Test #83
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
6e140e0
메뉴_이미지_파일_업로드_기능_추가 : feat : {S3Service - upload 기능 구현} https://githu…
kjh0718 a340bb4
메뉴_이미지_파일_업로드_기능_추가 : feat : {S3 upload 기능 test 삭제} https://github.co…
kjh0718 2a231f8
메뉴_이미지_파일_업로드_기능_추가 : feat : {menu image 파일 업로드 기능 구현} https://github…
kjh0718 8430fef
메뉴_이미지_파일_업로드_기능_추가 : feat : {메뉴 이미지 해당 식당별 경로 생성} https://github.com…
kjh0718 6ebcd32
메뉴_이미지_파일_업로드_기능_추가 : feat : {메뉴 생성시 이미지 추가 기능 + docs 수정} https://git…
kjh0718 57dfdbc
메뉴_이미지_파일_업로드_기능_추가 : feat : {s3 deletefile 기능 구현} https://github.com…
kjh0718 06ad745
메뉴_이미지_파일_업로드_기능_추가 : feat : {menu 관리자 전용 API 경로 분리} https://github.c…
kjh0718 c2f800f
메뉴_이미지_파일_업로드_기능_추가 : feat : {spring-cloud-aws-starter-s3 버전 3.1.0 ->…
kjh0718 3f38909
메뉴_이미지_파일_업로드_기능_추가 : feat : {crateMenu:이미지포함 생성시 이미지 url까지 반환,기존 이미지…
kjh0718 15d0e12
메뉴_이미지_파일_업로드_기능_추가 : feat : {메뉴 저장 실패시 이미지 삭제} https://github.com/Ca…
kjh0718 202ca11
메뉴_이미지_파일_업로드_기능_추가 : feat : {로그 수정} https://github.com/CampusTable/c…
kjh0718 eb1cf7a
Merge pull request #82 from CampusTable/20260125_#76_메뉴_이미지_파일_업로드_기능_추가
kjh0718 585659a
메뉴_이미지_파일_업로드_기능_추가 : feat : {s3 환경변수} https://github.com/CampusTable…
kjh0718 c813338
메뉴_이미지_파일_업로드_기능_추가 : feat : {createMenu: save -> saveAndFlush, uploa…
kjh0718 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ | |
| import com.campustable.be.domain.menu.dto.MenuUpdateRequest; | ||
| import com.campustable.be.domain.menu.entity.Menu; | ||
| import com.campustable.be.domain.menu.repository.MenuRepository; | ||
| import com.campustable.be.domain.s3.service.S3Service; | ||
| import com.campustable.be.global.exception.CustomException; | ||
| import com.campustable.be.global.exception.ErrorCode; | ||
| import java.util.ArrayList; | ||
|
|
@@ -19,6 +20,7 @@ | |
| import org.springframework.stereotype.Service; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
| import org.springframework.web.multipart.MultipartFile; | ||
|
|
||
|
|
||
| @Slf4j | ||
|
|
@@ -29,37 +31,79 @@ public class MenuService { | |
| private final MenuRepository menuRepository; | ||
| private final CategoryRepository categoryRepository; | ||
| private final CafeteriaService cafeteriaService; | ||
| private final S3Service s3Service; | ||
|
|
||
|
|
||
| @Transactional | ||
| public MenuResponse createMenu(MenuRequest request) { | ||
| public MenuResponse createMenu(MenuRequest request, MultipartFile image) { | ||
|
|
||
| Category category = categoryRepository.findById(request.getCategoryId()) | ||
| .orElseThrow(() -> { | ||
| log.warn("createMenu: 유효하지 않은 category id"); | ||
| return new CustomException(ErrorCode.CATEGORY_NOT_FOUND); | ||
| }); | ||
|
|
||
| Optional<Menu> existingMenu = menuRepository.findByCategoryAndMenuName( | ||
| category, | ||
| request.getMenuName() | ||
| ); | ||
| menuRepository.findByCategoryAndMenuName(category, request.getMenuName()) | ||
| .ifPresent(menu -> { | ||
| log.error("createMenu: 이미 해당 카테고리에 menu가 존재합니다. 생성이 아닌 수정을 통해 진행해주세요."); | ||
| throw new CustomException(ErrorCode.MENU_ALREADY_EXISTS); | ||
| }); | ||
|
|
||
| Menu menu = request.toEntity(category); | ||
| Menu savedMenu = menuRepository.save(menu); | ||
|
|
||
| if (existingMenu.isPresent()) { | ||
| log.error("createMenu: 이미 해당 카테고리에 menu가 존재합니다. 생성이 아닌 수정을 통해 진행해주세요."); | ||
| throw new CustomException(ErrorCode.MENU_ALREADY_EXISTS); | ||
| if (image != null && !image.isEmpty()) { | ||
| return uploadMenuImage(savedMenu.getId(), image); | ||
| } | ||
|
|
||
| Menu menu = request.toEntity(category); | ||
| return MenuResponse.from(menuRepository.save(menu)); | ||
| return MenuResponse.from(savedMenu); | ||
|
|
||
| } | ||
|
|
||
| @Transactional | ||
| public MenuResponse uploadMenuImage(Long menuId, MultipartFile image) { | ||
| Menu menu = menuRepository.findById(menuId) | ||
| .orElseThrow(() -> new CustomException(ErrorCode.MENU_NOT_FOUND)); | ||
|
|
||
| if (image == null || image.isEmpty()) { | ||
| throw new CustomException(ErrorCode.INVALID_FILE_REQUEST); | ||
| } | ||
|
|
||
| String oldUrl = menu.getMenuUrl(); | ||
| String cafeteriaName = menu.getCategory().getCafeteria().getName(); | ||
| String dirName = "menu/" + cafeteriaName; | ||
|
|
||
| String newUrl = s3Service.uploadFile(image, dirName); | ||
| menu.setMenuUrl(newUrl); | ||
| Menu savedMenu; | ||
| try { | ||
| savedMenu = menuRepository.saveAndFlush(menu); | ||
| } catch (Exception e) { | ||
| try { | ||
| s3Service.deleteFile(newUrl); | ||
| } catch (Exception ex) { | ||
| log.warn("uploadMenuImage: 신규 이미지 정리 실패. newUrl={}", newUrl, ex); | ||
| } | ||
| log.error("uploadMenuImage: 메뉴 저장 실패. menuId={}", menuId, e); | ||
| throw e; | ||
| } | ||
|
|
||
| if (oldUrl != null && !oldUrl.isBlank()) { | ||
| try { | ||
| s3Service.deleteFile(oldUrl); | ||
| } catch (Exception e) { | ||
| log.warn("uploadMenuImage: 기존 이미지 삭제 실패. oldUrl={}", oldUrl, e); | ||
| } | ||
| } | ||
|
|
||
| return MenuResponse.from(savedMenu); | ||
| } | ||
|
|
||
| @Transactional(readOnly = true) | ||
| public MenuResponse getMenuById(Long menuId) { | ||
|
|
||
| Menu menu = menuRepository.findById(menuId) | ||
| .orElseThrow(()->{ | ||
| .orElseThrow(() -> { | ||
| log.error("getMenuById : 유효하지않은 menuId"); | ||
| return new CustomException(ErrorCode.MENU_NOT_FOUND); | ||
| }); | ||
|
|
@@ -137,9 +181,15 @@ public void deleteMenu(Long menuId) { | |
| if (menu.isEmpty()) { | ||
| log.error("menuId not found {}", menuId); | ||
| throw new CustomException(ErrorCode.MENU_NOT_FOUND); | ||
| } else { | ||
| menuRepository.delete(menu.get()); | ||
| } | ||
| if (menu.get().getMenuUrl() != null && !menu.get().getMenuUrl().isBlank()) { | ||
| try { | ||
| s3Service.deleteFile(menu.get().getMenuUrl()); | ||
| } catch (Exception e) { | ||
| log.warn("deleteMenu: 이미지 삭제 실패. menuId={}, url={}", menuId, menu.get().getMenuUrl(), e); | ||
| } | ||
| } | ||
| menuRepository.deleteById(menuId); | ||
|
Comment on lines
+185
to
+192
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. S3 삭제와 DB 삭제 순서 조정 필요 현재 S3 파일을 삭제한 후 DB 레코드를 삭제합니다. DB 삭제를 먼저 수행하고, 성공 시 S3 파일을 삭제하는 것이 안전합니다. 수정 제안 `@Transactional`
public void deleteMenu(Long menuId) {
Optional<Menu> menu = menuRepository.findById(menuId);
if (menu.isEmpty()) {
log.error("menuId not found {}", menuId);
throw new CustomException(ErrorCode.MENU_NOT_FOUND);
}
+ String menuUrl = menu.get().getMenuUrl();
+ menuRepository.deleteById(menuId);
+
+ if (menuUrl != null && !menuUrl.isBlank()) {
- if (menu.get().getMenuUrl() != null && !menu.get().getMenuUrl().isBlank()) {
try {
- s3Service.deleteFile(menu.get().getMenuUrl());
+ s3Service.deleteFile(menuUrl);
} catch (Exception e) {
- log.warn("deleteMenu: 이미지 삭제 실패. menuId={}, url={}", menuId, menu.get().getMenuUrl(), e);
+ log.warn("deleteMenu: 이미지 삭제 실패. menuId={}, url={}", menuId, menuUrl, e);
}
}
- menuRepository.deleteById(menuId);
}🤖 Prompt for AI Agents |
||
| } | ||
|
|
||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.