44import com .DecodEat .domain .products .entity .ProductNutrition ;
55import com .DecodEat .domain .products .repository .ProductRepository ;
66import com .DecodEat .domain .report .converter .ReportConverter ;
7- import com .DecodEat .domain .report .dto .request .ImageUpdateRequestDto ;
87import com .DecodEat .domain .report .dto .request .ProductNutritionUpdateRequestDto ;
98import com .DecodEat .domain .report .dto .response .ReportResponseDto ;
109import com .DecodEat .domain .report .entity .*;
1413import com .DecodEat .domain .report .repository .ReportRecordRepository ;
1514import com .DecodEat .domain .users .entity .User ;
1615import com .DecodEat .global .apiPayload .code .status .ErrorStatus ;
16+ import com .DecodEat .global .aws .s3 .AmazonS3Manager ;
1717import com .DecodEat .global .exception .GeneralException ;
1818import lombok .RequiredArgsConstructor ;
1919import org .springframework .data .domain .Page ;
2222import org .springframework .data .domain .Sort ;
2323import org .springframework .stereotype .Service ;
2424import org .springframework .transaction .annotation .Transactional ;
25+ import org .springframework .web .multipart .MultipartFile ;
26+
27+ import java .util .UUID ;
2528
2629import static com .DecodEat .global .apiPayload .code .status .ErrorStatus .*;
2730
@@ -33,6 +36,7 @@ public class ReportService {
3336 private final NutritionReportRepository nutritionReportRepository ;
3437 private final ImageReportRepository imageReportRepository ;
3538 private final ReportRecordRepository reportRecordRepository ;
39+ private final AmazonS3Manager amazonS3Manager ;
3640
3741 public ReportResponseDto requestUpdateNutrition (User user , Long productId , ProductNutritionUpdateRequestDto requestDto ){
3842
@@ -103,7 +107,7 @@ public ReportResponseDto rejectReport(Long reportId){
103107 * @param reportId 수락할 신고의 ID
104108 * @return 처리 결과를 담은 DTO
105109 */
106- public ReportResponseDto acceptReport (Long reportId , ImageUpdateRequestDto requestDto ){
110+ public ReportResponseDto acceptReport (Long reportId , MultipartFile newImageUrl ){
107111 // 1. ID로 신고 내역 조회
108112 ReportRecord reportRecord = reportRecordRepository .findById (reportId )
109113 .orElseThrow (() -> new GeneralException (REPORT_NOT_FOUND ));
@@ -115,16 +119,29 @@ public ReportResponseDto acceptReport(Long reportId, ImageUpdateRequestDto reque
115119
116120 Product product = reportRecord .getProduct ();
117121
118- // 3. 신고 유횽에 따른 로직 분기
122+ // 3. 신고 유형에 따른 로직 분기
119123 if (reportRecord instanceof NutritionReport ) {
120124 ProductNutrition productNutrition = product .getProductNutrition ();
121125 productNutrition .updateFromReport ((NutritionReport ) reportRecord );
122126
123127 } else if (reportRecord instanceof ImageReport ) {
124- // 새로운 이미지가 없는 경우 이미지 삭제 -> null로 처리
125- // 새로운 이미지가 있는 경우 해당 이미지로 변경
126- String newImageUrl = (requestDto != null ) ? requestDto .getNewImageUrl () : null ;
127- product .updateProductImage (newImageUrl );
128+
129+ String oldImageUrl = product .getProductImage ();
130+
131+ // 새로운 이미지가 있는 경우 새로운 이미지로 변경
132+ if (newImageUrl != null && !newImageUrl .isEmpty ()) {
133+ String imageKey = "products/" + UUID .randomUUID () + "_" + newImageUrl .getOriginalFilename ();
134+ String uploadedImageUrl = amazonS3Manager .uploadFile (imageKey , newImageUrl );
135+ product .updateProductImage (uploadedImageUrl );
136+ } else {
137+ // 새로운 이미지가 없는 경우 이미지 삭제 -> null로 처리
138+ product .updateProductImage (null );
139+ }
140+
141+ if (oldImageUrl != null && !oldImageUrl .isEmpty ()) {
142+ String oldImageKey = amazonS3Manager .getKeyFromUrl (oldImageUrl );
143+ amazonS3Manager .deleteFile (oldImageKey );
144+ }
128145 }
129146
130147 // 4. reportstatus 상태를 accepted 변경
0 commit comments