Skip to content

Commit

Permalink
fix: 주거환경 다중 파일 업로드 시 요청파라미터 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
Suxxxxhyun committed Jan 8, 2024
1 parent a69048f commit c024573
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.security.Principal;
import java.util.List;
Expand Down Expand Up @@ -77,7 +78,7 @@ public ResponseEntity<?> getEnvironments(Principal principal) {
}


@PutMapping(value = "/residential-environments", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@PutMapping(value = "/residential-environments", consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.MULTIPART_FORM_DATA_VALUE})
@Operation(
summary = "분양 희망자의 주거 환경 이미지 업데이트(추가/수정/삭제)",
description = "분양 희망자가 전송한 주거환경 이미지로 업데이트 한다."
Expand Down Expand Up @@ -105,14 +106,17 @@ public ResponseEntity<?> getEnvironments(Principal principal) {
)
})
public ResponseEntity<?> addOrUpdateEnvironments(Principal principal,
@ModelAttribute @RequestPart( required = false ) ResidentialEnvDto.EnvRequestDto form) {
@RequestPart( required = false, name="deletedImgsId") ResidentialEnvDto.EnvRequestDto form,
@RequestPart(required = false, name="livingRoomImg") MultipartFile liv,
@RequestPart(required = false, name="bathRoomImg") MultipartFile bath,
@RequestPart(required = false, name="yardImg") MultipartFile yard) {

if (principal == null) {
throw new MissingPrincipalException();
}

Adopter adopter = adopterRepository.findByEmail(principal.getName()).orElse(null);
adopterService.updatePhotos(adopter, form);
adopterService.updatePhotos(adopter, form, liv, bath, yard);

return response.success(HttpStatus.OK);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,29 @@ public static class EnvRequestDto {
@Schema(description = "삭제한 이미지의 식별자 id. 없으면 빈 배열로 보내주면 됨", example = "[1, 2]")
private List<Long> deletedImgsId;

@Schema(description = "추가한 거실 이미지 객체. 없으면 빈 값으로 보내주면 됨")
@JsonSerialize(using = ToStringSerializer.class)
private MultipartFile livingRoomImg;

@Schema(description = "추가한 화장실 이미지 객체. 없으면 빈 값으로 보내주면 됨")
@JsonSerialize(using = ToStringSerializer.class)
private MultipartFile bathRoomImg;
}

@Schema(description = "추가한 마당 이미지 객체. 없으면 빈 값으로 보내주면 됨")
@JsonSerialize(using = ToStringSerializer.class)
private MultipartFile yardImg;
// @Data
// public static class LivingRoomImgRequestDto{
// @Schema(description = "추가한 거실 이미지 객체. 없으면 빈 값으로 보내주면 됨")
// @JsonSerialize(using = ToStringSerializer.class)
// private MultipartFile livingRoomImg;
// }
//
// @Data
// public static class BathRoomImgRequestDto{
// @Schema(description = "추가한 화장실 이미지 객체. 없으면 빈 값으로 보내주면 됨")
// @JsonSerialize(using = ToStringSerializer.class)
// private MultipartFile bathRoomImg;
// }
//
// @Data
// public static class YardImgRequestDto{
// @Schema(description = "추가한 마당 이미지 객체. 없으면 빈 값으로 보내주면 됨")
// @JsonSerialize(using = ToStringSerializer.class)
// private MultipartFile yardImg;
// }

}
@Getter
@NoArgsConstructor
public static class EnvResponseDto {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,11 @@ public List<Object> getEnvironments(Adopter adopter) {
* @param form 삭제하거나 추가해야할 주거환경 정보가 들어있는 dto
*/
@Transactional
public void updatePhotos(Adopter adopter, ResidentialEnvDto.EnvRequestDto form) {
public void updatePhotos(Adopter adopter,
ResidentialEnvDto.EnvRequestDto form,
MultipartFile liv,
MultipartFile bath,
MultipartFile yard) {
// 삭제할 이미지 ID 리스트를 가져옴
List<Long> deletedImgsId = form.getDeletedImgsId();

Expand All @@ -150,14 +154,14 @@ public void updatePhotos(Adopter adopter, ResidentialEnvDto.EnvRequestDto form)


// 거실 이미지를 가져오기
MultipartFile livingRoomImg = form.getLivingRoomImg();
MultipartFile livingRoomImg = liv;
// 이미지가 담겨있는지와, 거실 이미지가 이미 등록되어 있는지 확인. 참이라면 중복 등록이므로 예외 발생
log.info("거실이미지 확인 : " + livingRoomImg.getContentType());
if (livingRoomImg.getContentType() != null && savedSpaceTypes.contains(SpaceType.LIVING_ROOM)) {
throw new BusinessException(ErrorCode.HAS_MORE_THAN_ONE_SPACE_TYPE);
}
//log.info("거실이미지 확인 : " + livingRoomImg.getContentType());
// if (livingRoomImg.getContentType() != null && savedSpaceTypes.contains(SpaceType.LIVING_ROOM)) {
// throw new BusinessException(ErrorCode.HAS_MORE_THAN_ONE_SPACE_TYPE);
// }
// 이미지가 담겨있는지 확인하고 참이면 해당 이미지 등록
if (livingRoomImg.getContentType() != null) {
if (livingRoomImg != null) {
String originalFilename = livingRoomImg.getOriginalFilename();

// 주거환경 이미지 객체를 생성하고 정보를 설정
Expand All @@ -172,11 +176,11 @@ public void updatePhotos(Adopter adopter, ResidentialEnvDto.EnvRequestDto form)
}

// 화장실 이미지도 거실 이미지와 동일한 로직으로 진행
MultipartFile bathRoomImg = form.getBathRoomImg();
if (bathRoomImg.getContentType() != null && savedSpaceTypes.contains(SpaceType.BATH_ROOM)) {
throw new BusinessException(ErrorCode.HAS_MORE_THAN_ONE_SPACE_TYPE);
}
if (bathRoomImg.getContentType() != null) {
MultipartFile bathRoomImg = bath;
// if (bathRoomImg.getContentType() != null && savedSpaceTypes.contains(SpaceType.BATH_ROOM)) {
// throw new BusinessException(ErrorCode.HAS_MORE_THAN_ONE_SPACE_TYPE);
// }
if (bathRoomImg != null) {
String originalFilename = bathRoomImg.getOriginalFilename();

// 주거환경 이미지 객체를 생성하고 정보를 설정
Expand All @@ -191,11 +195,11 @@ public void updatePhotos(Adopter adopter, ResidentialEnvDto.EnvRequestDto form)
}

// 화장실 이미지도 거실 이미지와 동일한 로직으로 진행
MultipartFile yardImg = form.getYardImg();
if (yardImg.getContentType() != null && savedSpaceTypes.contains(SpaceType.YARD)) {
throw new BusinessException(ErrorCode.HAS_MORE_THAN_ONE_SPACE_TYPE);
}
if (yardImg.getContentType() != null) {
MultipartFile yardImg = yard;
// if (yardImg.getContentType() != null && savedSpaceTypes.contains(SpaceType.YARD)) {
// throw new BusinessException(ErrorCode.HAS_MORE_THAN_ONE_SPACE_TYPE);
// }
if (yardImg != null) {
String originalFilename = yardImg.getOriginalFilename();

// 주거환경 이미지 객체를 생성하고 정보를 설정
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* DATE AUTHOR NOTE
* -----------------------------------------------------------
* 2023-07-11 박수현 최초 생성
*/
*/
@Configuration
public class WebConfig implements WebMvcConfigurer{

Expand Down

0 comments on commit c024573

Please sign in to comment.