diff --git a/README.md b/README.md index a2ca35d..a0748db 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ ๐Ÿ‘ค ํŒ€์› ์†Œ๊ฐœ --- -**๋ฐ•์ˆ˜ํ˜„(ํŒ€์žฅ)** +**๋ฐ•์ˆ˜ํ˜„(๋ฐฑ์—”๋“œ ๋ฐ Infra, ํŒ€์žฅ)** - ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์„ค๊ณ„ - nginx, docker, CICD๋ฅผ ํ†ตํ•œ ec2๋ฐฐํฌ - ์•„ํ‚คํ…์ฒ˜ ์„ค๊ณ„๋ธŒ๋ฆฌ๋” ๋ชจ์•„๋ณด๊ธฐ ๊ฑฐ๋ฆฌ์ˆœ ์ •๋ ฌ ๋ฐ ํŽ˜์ด์ง•์ฒ˜๋ฆฌ @@ -39,7 +39,7 @@ - ๋ถ„์–‘์‹ ์ฒญ๋‚ด์—ญ ํŽ˜์ด์ง• ์ฒ˜๋ฆฌ - ํšŒ์› ์ •๋ณด ์ˆ˜์ • -**์˜ค์ฐฝํ˜„(ํ”„๋ก ํŠธ ๋ถ€ํŒ€์žฅ)** +**์˜ค์ฐฝํ˜„(ํ”„๋ก ํŠธ, ๋ถ€ํŒ€์žฅ)** - ๋กœ๊ทธ์ธ - ํšŒ์›๊ฐ€์ž… - ํšŒ์›ํƒˆํ‡ด diff --git a/src/main/java/com/example/petree/domain/adopter/dto/ReviewDto.java b/src/main/java/com/example/petree/domain/adopter/dto/ReviewDto.java index ce57c2a..82b27c4 100644 --- a/src/main/java/com/example/petree/domain/adopter/dto/ReviewDto.java +++ b/src/main/java/com/example/petree/domain/adopter/dto/ReviewDto.java @@ -69,6 +69,7 @@ public static class ReviewUpdateRequestDto { @Length(max = 3000, message = "๋‚ด์šฉ์€ ์ตœ๋Œ€ 3000์ž๊นŒ์ง€ ์ž…๋ ฅ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.") @Schema(description = "๋‚ด์šฉ", example = "์ •์„ฑ์Šค๋Ÿฝ๊ฒŒ ํ‚ค์šฐ๊ณ˜์Šต๋‹ˆ๋‹ค!") private String content; + @NotBlank(message = "์ด๋ฏธ์ง€ ์‚ญ์ œ ์—ฌ๋ถ€๋Š” ํ•„์ˆ˜ ์ž…๋ ฅ๊ฐ’์ž…๋‹ˆ๋‹ค.") @Schema(description = "์ด๋ฏธ์ง€ ์‚ญ์ œ ์—ฌ๋ถ€") private boolean deleteImages; @NotEmpty(message = "์ด๋ฏธ์ง€๋Š” ํ•„์ˆ˜ ์ž…๋ ฅ๊ฐ’์ž…๋‹ˆ๋‹ค.") diff --git a/src/main/java/com/example/petree/domain/dog/controller/PossessionController.java b/src/main/java/com/example/petree/domain/dog/controller/PossessionController.java index 6938326..db7d4f3 100644 --- a/src/main/java/com/example/petree/domain/dog/controller/PossessionController.java +++ b/src/main/java/com/example/petree/domain/dog/controller/PossessionController.java @@ -1,5 +1,7 @@ package com.example.petree.domain.dog.controller; +import com.example.petree.domain.adopter.domain.Adopter; +import com.example.petree.domain.adopter.domain.Review; import com.example.petree.domain.breeder.domain.Breeder; import com.example.petree.domain.breeder.repository.BreederRepository; import com.example.petree.domain.dog.domain.Dog; @@ -11,6 +13,8 @@ import com.example.petree.domain.main_breed.domain.DogType; import com.example.petree.domain.main_breed.domain.MainBreed; import com.example.petree.domain.main_breed.service.MainBreedService; +import com.example.petree.domain.member.domain.Member; +import com.example.petree.domain.member.repository.MemberRepository; import com.example.petree.global.Response; import com.example.petree.global.ResponseSchema; import com.example.petree.global.error.exception.MissingPrincipalException; @@ -23,6 +27,7 @@ import io.swagger.v3.oas.annotations.responses.ApiResponses; import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; @@ -70,12 +75,14 @@ @RequiredArgsConstructor @RequestMapping("/api/breeder/dogs") @Tag(name = "๋ธŒ๋ฆฌ๋” ํšŒ์›, ๋งˆ์ดํŽ˜์ด์ง€ ๋ณด์œ ๊ฒฌ์ข…๊ด€๋ฆฌ", description = "๋ธŒ๋ฆฌ๋” ํšŒ์›, ๋งˆ์ดํŽ˜์ด์ง€ ๋ณด์œ ๊ฒฌ์ข…๊ด€๋ฆฌ API") +@Slf4j public class PossessionController { private final DogService dogService; private final DogRepository dogRepository; private final Response response; private final BreederRepository breederRepository; + private final MemberRepository memberRepository; @GetMapping @Operation( @@ -194,17 +201,37 @@ public ResponseEntity update(Principal principal, if (principal == null) { throw new MissingPrincipalException(); } - Breeder breeder= breederRepository.findByEmail(principal.getName()).orElse(null); - if(breeder != null) { - dogService.deleteAndAddImages(breeder,id,possessionDogDto); + Dog dog = dogRepository.findById(id).orElse(null); - dogService.update(breeder ,id, possessionDogDto); + if (dog == null) { + return response.fail(HttpStatus.NOT_FOUND, "ํ•ด๋‹น ID์˜ ๋ณด์œ ๊ฒฌ์ข… ๊ธ€์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."); + } - return response.success(HttpStatus.OK, possessionDogDto); + Member member = memberRepository.findByEmail(principal.getName()).orElse(null); - }else { - return response.fail(HttpStatus.FORBIDDEN, Map.of("message", "๋ธŒ๋ฆฌ๋” ํšŒ์›์ด ์•„๋‹™๋‹ˆ๋‹ค."));} + if (member != null) { + if (member.getRole().getTitle().equals("BREEDER")) { + + int maxImageCount = 4; + int currentImageCount = possessionDogDto.getDogImgFiles().size(); + log.info(currentImageCount + "์š”์ฒญํ•œ ์ด๋ฏธ์ง€ ๊ฐœ์ˆ˜"); + if(currentImageCount <= 0){ + return response.fail(HttpStatus.BAD_REQUEST, "์ด๋ฏธ์ง€๋Š” ํ•„์ˆ˜๋กœ ๋“ฑ๋กํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค."); + } + + if (currentImageCount > maxImageCount) { + return response.fail(HttpStatus.BAD_REQUEST, "์ด๋ฏธ์ง€๋Š” ์ตœ๋Œ€ " + maxImageCount + "๊ฐœ๊นŒ์ง€ ์—…๋กœ๋“œ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค."); + } + + dogService.update((Breeder) member, dog, possessionDogDto); + return response.success(HttpStatus.OK, "์ˆ˜์ •์ด ์„ฑ๊ณต์ ์œผ๋กœ ์ด๋ฃจ์–ด์กŒ์Šต๋‹ˆ๋‹ค."); + } else { + return response.fail(HttpStatus.FORBIDDEN, "๋ธŒ๋ฆฌ๋” ํšŒ์›์ด ์•„๋‹™๋‹ˆ๋‹ค."); + } + } else { + return response.fail(HttpStatus.UNAUTHORIZED, "๋กœ๊ทธ์ธ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค."); + } } diff --git a/src/main/java/com/example/petree/domain/dog/domain/DogImgFile.java b/src/main/java/com/example/petree/domain/dog/domain/DogImgFile.java index 0561a21..619a469 100644 --- a/src/main/java/com/example/petree/domain/dog/domain/DogImgFile.java +++ b/src/main/java/com/example/petree/domain/dog/domain/DogImgFile.java @@ -1,6 +1,9 @@ package com.example.petree.domain.dog.domain; +import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Getter; +import lombok.NoArgsConstructor; import org.hibernate.annotations.GenericGenerator; import javax.persistence.*; @@ -10,6 +13,9 @@ @Entity @Table(name = "dog_img_file") @Getter +@Builder +@AllArgsConstructor +@NoArgsConstructor public class DogImgFile { @Id @GeneratedValue(strategy = GenerationType.AUTO,generator = "native") @@ -18,18 +24,7 @@ public class DogImgFile { private String originalFileName; private String fileName; private String fileUrl; - - @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "dog_id") private Dog dog; - - public DogImgFile (String originalFileName, String fileName, String fileUrl,Dog dog) { - this.originalFileName= originalFileName; - this.fileName= fileName; - this.fileUrl=fileUrl; - this.dog=dog; - } - public DogImgFile() { - } } \ No newline at end of file diff --git a/src/main/java/com/example/petree/domain/dog/dto/PossessionDogDto.java b/src/main/java/com/example/petree/domain/dog/dto/PossessionDogDto.java index 08f2655..6f449e1 100644 --- a/src/main/java/com/example/petree/domain/dog/dto/PossessionDogDto.java +++ b/src/main/java/com/example/petree/domain/dog/dto/PossessionDogDto.java @@ -116,28 +116,37 @@ public List getDogImgFiles() { public static class UpdateDogDto{ @Schema(description = "๋ณด์œ  ๊ฒฌ์ข… ์„ฑ๋ณ„", example = "FEMALE") + @NotNull(message = " ๊ฐ•์•„์ง€์˜ ์„ฑ๋ณ„์€ ํ•„์ˆ˜ ์ž…๋ ฅ ๊ฐ’์ž…๋‹ˆ๋‹ค.") private Gender gender; @Schema(description = "๋ณด์œ  ๊ฒฌ์ข… ์ถœ์ƒ์ผ", example = "2021-03-21") + @NotNull(message = "๊ฐ•์•„์ง€์˜ ์ถœ์ƒ์ผ์€ ํ•„์ˆ˜ ์ž…๋ ฅ ๊ฐ’์ž…๋‹ˆ๋‹ค.") @DateTimeFormat(pattern = "yyyy-MM-dd") private LocalDate birthDate; @Schema(description = "๋ณด์œ  ๊ฒฌ์ข… ์ด๋ฆ„", example = "์ด๋•…์ฝฉ") + @NotBlank(message = "๊ฐ•์•„์ง€์˜ ์ด๋ฆ„์€ ํ•„์ˆ˜ ์ž…๋ ฅ ๊ฐ’์ž…๋‹ˆ๋‹ค.") private String name; @Schema(description = "๋ณด์œ  ๊ฒฌ์ข… ํŠน์ง•", example = "3.7kg์˜ ์†Œํ˜•๊ฒฌ") + @NotBlank(message = "๊ฐ•์•„์ง€์˜ ์ƒ์„ธ๋‚ด์šฉ์€ ํ•„์ˆ˜ ์ž…๋ ฅ ๊ฐ’์ž…๋‹ˆ๋‹ค.") + @Size(max = 2000, message = "ํŠน์ง•์€ 2000์ž ์ด๋‚ด๋กœ ์ž‘์„ฑ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.") private String management; @Schema(description = "๋ณด์œ  ๊ฒฌ์ข… ์ž…์–‘ ์ง„ํ–‰ ์ƒํƒœ",example = "AVAILABLE") + @NotBlank(message = "๋ณด์œ  ๊ฒฌ์ข… ์ž…์–‘ ์ง„ํ–‰ ์ƒํƒœ๋Š” ํ•„์ˆ˜ ์ž…๋ ฅ ๊ฐ’์ž…๋‹ˆ๋‹ค.") private Status status; - @Schema(description = "์‚ญ์ œํ•  ์ด๋ฏธ์ง€ ํŒŒ์ผ ์ด๋ฆ„ ๋ชฉ๋ก") - private List imgNameToDelete; + @Schema(description = "์‚ญ์ œํ•  ์ด๋ฏธ์ง€ ํŒŒ์ผ ID๊ฐ’") + @NotBlank(message = "์‚ญ์ œํ•  ์ด๋ฏธ์ง€ ํŒŒ์ผ ID๊ฐ’์€ ํ•„์ˆ˜ ์ž…๋ ฅ ๊ฐ’์ž…๋‹ˆ๋‹ค.") + private List imgIdToDelete; - @Schema(description = "์ด๋ฏธ์ง€ ์—…๋กœ๋“œ ์—ฌ๋ถ€ ", example = "false") - private boolean uploadImage; + @Schema(description = "์ด๋ฏธ์ง€ ์‚ญ์ œ ์—ฌ๋ถ€ ", example = "false") + @NotBlank(message = "์ด๋ฏธ์ง€ ์‚ญ์ œ ์—ฌ๋ถ€๋Š” ํ•„์ˆ˜ ์ž…๋ ฅ ๊ฐ’์ž…๋‹ˆ๋‹ค.") + private boolean isDeleteImages; @Schema(description = "๋ณด์œ  ๊ฒฌ์ข…์˜ ์ด๋ฏธ์ง€๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š” ๋ฆฌ์ŠคํŠธ") + @NotBlank(message = "๋ณด์œ  ๊ฒฌ์ข…์˜ ์ด๋ฏธ์ง€๋Š” ํ•„์ˆ˜ ์ž…๋ ฅ ๊ฐ’์ž…๋‹ˆ๋‹ค.") @JsonSerialize(using = ToStringSerializer.class) private List dogImgFiles; diff --git a/src/main/java/com/example/petree/domain/dog/repository/DogImgFileRepository.java b/src/main/java/com/example/petree/domain/dog/repository/DogImgFileRepository.java index fc29843..d59927f 100644 --- a/src/main/java/com/example/petree/domain/dog/repository/DogImgFileRepository.java +++ b/src/main/java/com/example/petree/domain/dog/repository/DogImgFileRepository.java @@ -11,7 +11,7 @@ public interface DogImgFileRepository extends JpaRepository { List findByDogId(Long id); - void deleteByOriginalFileNameAndDogId(String originalFileName,Long dogId); + void deleteByIdAndDogId(Long fileId,Long dogId); - DogImgFile findByOriginalFileNameAndDogId(String originalFileName,Long dogId); + DogImgFile findByIdAndDogId(Long fileId,Long dogId); } diff --git a/src/main/java/com/example/petree/domain/dog/repository/DogRepository.java b/src/main/java/com/example/petree/domain/dog/repository/DogRepository.java index 5fafe60..c703ae2 100644 --- a/src/main/java/com/example/petree/domain/dog/repository/DogRepository.java +++ b/src/main/java/com/example/petree/domain/dog/repository/DogRepository.java @@ -1,12 +1,16 @@ package com.example.petree.domain.dog.repository; +import com.example.petree.domain.adopter.domain.Review; import com.example.petree.domain.dog.domain.Dog; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; +import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; import java.util.List; import java.util.Optional; @@ -29,4 +33,9 @@ public interface DogRepository extends JpaRepository, JpaSpecificatio Optional findById(Long id); void delete(Dog dog); + + @Transactional + @Modifying + @Query("DELETE FROM DogImgFile WHERE dog = :dog") + void deleteDogImgFilesByDog(@Param("dog") Dog dog); } diff --git a/src/main/java/com/example/petree/domain/dog/service/DogService.java b/src/main/java/com/example/petree/domain/dog/service/DogService.java index fffbdcc..da7b35a 100644 --- a/src/main/java/com/example/petree/domain/dog/service/DogService.java +++ b/src/main/java/com/example/petree/domain/dog/service/DogService.java @@ -1,5 +1,9 @@ package com.example.petree.domain.dog.service; +import com.example.petree.domain.adopter.domain.Adopter; +import com.example.petree.domain.adopter.domain.Review; +import com.example.petree.domain.adopter.domain.ReviewImgFile; +import com.example.petree.domain.adopter.dto.ReviewDto; import com.example.petree.domain.breeder.domain.Breeder; import com.example.petree.domain.dog.controller.PossessionController; import com.example.petree.domain.dog.domain.*; @@ -27,6 +31,7 @@ import org.springframework.web.multipart.MultipartFile; import java.time.LocalDate; +import java.util.ArrayList; import java.util.List; import java.util.UUID; @@ -202,8 +207,13 @@ private void uploadDogImgFiles(Dog dog, PossessionDogDto.CreateDogDto possession String originalFileName = multipartFile.getOriginalFilename(); String fileName = UUID.randomUUID().toString() + "." + fileUtil.extractExt(originalFileName); String fileUrl = s3Util.upload(multipartFile, "dog-img", fileName); - DogImgFile dogImgFile = new DogImgFile(originalFileName, fileName, fileUrl, dog); - dog.addDogImgFile(dogImgFile); + DogImgFile imgFile = DogImgFile.builder() + .originalFileName(multipartFile.getOriginalFilename()) + .fileName(fileName) + .fileUrl(fileUrl) + .dog(dog) + .build(); + dog.addDogImgFile(imgFile); } } @@ -245,104 +255,70 @@ private void deleteDogImgFiles(Dog dog) { } @Transactional - public PossessionDogDto.UpdateDogDto update(Breeder breeder, Long id, PossessionDogDto.UpdateDogDto possessionDogDto) { - Dog dog = dogRepository.findById(id) - .orElseThrow(() -> new IllegalArgumentException("ํ•ด๋‹น ๊ธ€์ด ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.")); - - Long breederId = breeder.getId(); - if(!dog.getBreeder().getId().equals(breederId)){ - throw new IllegalStateException("ํ•ด๋‹น ๊ธ€ ์ˆ˜์ •์— ๋Œ€ํ•œ ๊ถŒํ•œ์ด ์—†์Šต๋‹ˆ๋‹ค."); - } - // possessionDogDto์˜ patch ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ dog์— ์ €์žฅ๋˜์–ด ์žˆ๋Š” ์ •๋ณด๋ฅผ ์ˆ˜์ •ํ•œ๋‹ค. - possessionDogDto.patch(dog); - // ์ˆ˜์ •ํ•œ ์ •๋ณด๋ฅผ ์ €์žฅํ•œ๋‹ค. - dog.updateFromDto(possessionDogDto); - dogRepository.save(dog); - - return new PossessionDogDto.UpdateDogDto(dog); - } - - @Transactional - public PossessionDogDto.UpdateDogDto deleteAndAddImages(Breeder breeder, Long id, PossessionDogDto.UpdateDogDto possessionDogDto) { - Dog dog = dogRepository.findById(id) - .orElseThrow(() -> new IllegalArgumentException("ํ•ด๋‹น ๊ธ€์ด ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.")); - // ์ด๋ฏธ์ง€ ์‚ญ์ œ ์ฒ˜๋ฆฌ - deleteDogImgFileSelect(dog, possessionDogDto); - - int imgFilesCount = dog.getDogImgFiles().size(); - - if(imgFilesCount + possessionDogDto.getDogImgFiles().size()>4){ - throw new IllegalArgumentException("์ด๋ฏธ์ง€๋Š” ์ตœ๋Œ€ 4๊ฐœ๊นŒ์ง€๋งŒ ๋“ฑ๋กํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค."); + public void update(Breeder breeder, Dog dog, PossessionDogDto.UpdateDogDto request) { + // ์ž‘์„ฑ์ž์™€ ๋ฆฌ๋ทฐ์˜ ์ž‘์„ฑ์ž๊ฐ€ ์ผ์น˜ํ•˜๋Š”์ง€ ํ™•์ธ + if (!dog.getBreeder().equals(breeder)) { + throw new IllegalArgumentException("ํ•ด๋‹น ์ž‘์„ฑ์ž๋งŒ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค."); } - addImg(breeder,id,possessionDogDto); + saveDataWithOutImage(dog, request); - List dogImgFiles = dogImgFileRepository.findByDogId(dog.getId()); - if(dogImgFiles.size()<1){ - throw new IllegalArgumentException("์ด๋ฏธ์ง€๋Š” ์ตœ์†Œ 1๊ฐœ์ด์ƒ ๋“ฑ๋ก๋˜์–ด ์žˆ์–ด์•ผํ•ฉ๋‹ˆ๋‹ค."); + if (request.isDeleteImages()) { + deleteReviewImages(dog); } - return new PossessionDogDto.UpdateDogDto(dog); - } - - public void addImg(Breeder breeder, Long id, PossessionDogDto.UpdateDogDto possessionDogDto) { - Dog dog = dogRepository.findById(id) - .orElseThrow(() -> new IllegalArgumentException("ํ•ด๋‹น ๊ธ€์ด ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.")); + updateReviewImages(request.getDogImgFiles(), dog); - if (possessionDogDto.isUploadImage()) { - if (!possessionDogDto.getDogImgFiles().isEmpty()) { - // ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ ์ˆ˜ํ–‰ - possessionDogDto.getDogImgFiles().stream().forEach(multipartFile -> { - String originalFileName = multipartFile.getOriginalFilename(); - String fileName = UUID.randomUUID().toString() + "." + fileUtil.extractExt(originalFileName); - String fileUrl = s3Util.upload(multipartFile, "dog-img", fileName); - DogImgFile dogImgFile = new DogImgFile(originalFileName, fileName, fileUrl, dog); - dog.addDogImgFile(dogImgFile); - }); - } - } + // ๋ฆฌ๋ทฐ ์ €์žฅ dogRepository.save(dog); } + private void saveDataWithOutImage(Dog dog, PossessionDogDto.UpdateDogDto request){ + if (request.getGender() != null) { + dog.setGender(request.getGender()); + } + if (request.getBirthDate() != null) { + dog.setBirthDate(request.getBirthDate()); + } - /** - * @author ์ด์ง€์ˆ˜ - * @date 2023-08-13 - * @description : ์ˆ˜์ • ์‹œ ์ด๋ฏธ์ง€ ๋ฌด์กฐ๊ฑด ์‚ญ์ œ๋˜๋Š” ๊ฒƒ์„ ์„ ํƒ์ ์œผ๋กœ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ๊ฒŒ ์ˆ˜์ • - * ์‚ฌ์šฉ์ž์—๊ฒŒ ์‚ญ์ œํ•  filename์„ ์ž…๋ ฅ๋ฐ›์•„์„œ ์‚ญ์ œ๊ฐ€ ์ด๋ฃจ์–ด์ง - */ + if (request.getName() != null) { + dog.setName(request.getName()); + } - // ํ•ด๋‹น dog์˜ ์ด๋ฏธ์ง€๋ฅผ ์„ ํƒ์ ์œผ๋กœ ์‚ญ์ œํ•จ. ์‚ฌ์šฉ์ž์—๊ฒŒ originalFileName ์ž…๋ ฅ๋ฐ›์•„ ํ•ด๋‹น ์ด๋ฏธ์ง€ ์‚ญ์ œ - public void deleteDogImgFileSelect(Dog dog,PossessionDogDto.UpdateDogDto possessionDogDto) { - List dogImgFiles = dogImgFileRepository.findByDogId(dog.getId()); - if (dogImgFiles.isEmpty()) { - throw new IllegalArgumentException("ํ•ด๋‹น ๊ฐ•์•„์ง€์— ๋Œ€ํ•œ ์ด๋ฏธ์ง€ ํŒŒ์ผ์ด ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."); + if (request.getManagement() != null) { + dog.setManagement(request.getManagement()); } - List fileNamesToDelete = possessionDogDto.getImgNameToDelete(); - if (fileNamesToDelete == null) { - return; + + if (request.getStatus() != null){ + dog.setStatus(request.getStatus()); } + } - for (String originalFileName : fileNamesToDelete) { + private void deleteReviewImages(Dog dog) { + // ๊ธฐ์กด ์ด๋ฏธ์ง€ ์‚ญ์ œ + dogRepository.deleteDogImgFilesByDog(dog); + } - DogImgFile dogImgFile=dogImgFileRepository.findByOriginalFileNameAndDogId(originalFileName, dog.getId()); - String fileName=dogImgFile.getFileName(); - String s3path = "dog-img/" + fileName; - s3Util.delete(s3path); // S3 ์ด๋ฏธ์ง€ ์‚ญ์ œ ๋กœ์ง ํ˜ธ์ถœ - if(dogImgFile == null) { - throw new IllegalArgumentException("์‚ญ์ œ ์š”์ฒญํ•˜์‹  ํŒŒ์ผ์ด ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."); - } - dogImgFileRepository.deleteByOriginalFileNameAndDogId(originalFileName, dog.getId()); + private void updateReviewImages(List newImages, Dog dog) { + // ์ด๋ฏธ์ง€ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ˆ˜์ •ํ•˜๋ ค๋ฉด ์ด์ „ ์ด๋ฏธ์ง€ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ์ƒˆ๋กœ์šด ์ด๋ฏธ์ง€๋ฅผ ์ถ”๊ฐ€ + List images = new ArrayList<>(); + if (newImages != null && !newImages.isEmpty()) { + for (MultipartFile file : newImages) { + String originalFilename = file.getOriginalFilename(); + String fileName = UUID.randomUUID().toString() + "." + fileUtil.extractExt(originalFilename); + String fileUrl = s3Util.upload(file, "dog-img", fileName); + + DogImgFile imgFile = DogImgFile.builder() + .originalFileName(file.getOriginalFilename()) + .fileName(fileName) + .fileUrl(fileUrl) + .dog(dog) + .build(); + + images.add(imgFile); } - dog.getDogImgFiles().removeIf(imgFile -> { - String originalFileName = imgFile.getOriginalFileName(); - Long dogId = imgFile.getDog().getId(); - - // ์ด๋ฏธ์ง€ ํŒŒ์ผ ๊ฐ์ฒด๋ฅผ ์‚ญ์ œํ•  ์กฐ๊ฑด ์„ค์ • - boolean shouldRemove = fileNamesToDelete.contains(originalFileName) && dogId.equals(dog.getId()); - - // ์‚ญ์ œ ์กฐ๊ฑด์ด ์ถฉ์กฑ๋˜๋ฉด true๋ฅผ ๋ฐ˜ํ™˜ํ•˜์—ฌ ์ด๋ฏธ์ง€ ํŒŒ์ผ ๊ฐ์ฒด ์‚ญ์ œ - return shouldRemove; - }); + // ์ด๋ฏธ์ง€ ์ €์žฅ ๋ฐ ๋ฆฌ๋ทฐ์— ์ƒˆ๋กœ์šด ์ด๋ฏธ์ง€ ์„ค์ • + dog.setDogImgFiles(images); + } } }