|
| 1 | +package com.sumte.image.controller; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | +import java.util.stream.Collectors; |
| 5 | + |
| 6 | +import org.springframework.http.HttpStatus; |
| 7 | +import org.springframework.http.ResponseEntity; |
| 8 | +import org.springframework.web.bind.annotation.GetMapping; |
| 9 | +import org.springframework.web.bind.annotation.PathVariable; |
| 10 | +import org.springframework.web.bind.annotation.PostMapping; |
| 11 | +import org.springframework.web.bind.annotation.PutMapping; |
| 12 | +import org.springframework.web.bind.annotation.RequestBody; |
| 13 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 14 | +import org.springframework.web.bind.annotation.RequestParam; |
| 15 | +import org.springframework.web.bind.annotation.RestController; |
| 16 | + |
| 17 | +import com.sumte.image.dto.ImageRequestDTO; |
| 18 | +import com.sumte.image.dto.ImageResponseDTO; |
| 19 | +import com.sumte.image.dto.ReplaceImageRequestDTO; |
| 20 | +import com.sumte.image.entity.OwnerType; |
| 21 | +import com.sumte.image.service.ImageService; |
| 22 | + |
| 23 | +import io.swagger.v3.oas.annotations.Operation; |
| 24 | +import io.swagger.v3.oas.annotations.Parameter; |
| 25 | +import io.swagger.v3.oas.annotations.enums.ParameterIn; |
| 26 | +import io.swagger.v3.oas.annotations.media.ArraySchema; |
| 27 | +import io.swagger.v3.oas.annotations.media.Content; |
| 28 | +import io.swagger.v3.oas.annotations.media.ExampleObject; |
| 29 | +import io.swagger.v3.oas.annotations.media.Schema; |
| 30 | +import io.swagger.v3.oas.annotations.tags.Tag; |
| 31 | +import jakarta.validation.Valid; |
| 32 | +import lombok.RequiredArgsConstructor; |
| 33 | + |
| 34 | +@RestController |
| 35 | +@RequestMapping("/images") |
| 36 | +@RequiredArgsConstructor |
| 37 | +@Tag(name = "이미지 API", description = "이미지 메타데이터 저장·조회·교체 API 및 S3 PresignedUrl 발급 API") |
| 38 | +public class ImageController { |
| 39 | + private final ImageService imageService; |
| 40 | + |
| 41 | + @Operation(summary = "이미지 일괄 저장", |
| 42 | + description = """ |
| 43 | + - 여러 이미지 메타데이터를 한 번에 저장합니다. |
| 44 | + - 이미지가 등록되는 파트에 대한 정보 OwnerType(GUESTHOUSE, ROOM 등)과 |
| 45 | + OwnerId(해당 파트의 ID)를 함께 전달해야 합니다. |
| 46 | + - 요청 리스트 순서대로 서버에서 sortOrder(이미지 순서)가 1부터 자동 부여됩니다. |
| 47 | + """) |
| 48 | + @io.swagger.v3.oas.annotations.parameters.RequestBody( |
| 49 | + description = "등록할 이미지 리스트를 전달합니다. 서버가 순서를 1부터 자동 부여합니다.", |
| 50 | + required = true, |
| 51 | + content = @Content( |
| 52 | + mediaType = "application/json", |
| 53 | + array = @ArraySchema( |
| 54 | + schema = @Schema(implementation = ImageRequestDTO.class) |
| 55 | + ), |
| 56 | + examples = { |
| 57 | + @ExampleObject( |
| 58 | + name = "Image Batch Upload Example", |
| 59 | + value = """ |
| 60 | + [ |
| 61 | + { |
| 62 | + "ownerType": "GUESTHOUSE", |
| 63 | + "ownerId": 1, |
| 64 | + "url": "https://sumte-file.s3.ap-northeast-2.amazonaws.com/sumte1.png" |
| 65 | + }, |
| 66 | + { |
| 67 | + "ownerType": "GUESTHOUSE", |
| 68 | + "ownerId": 1, |
| 69 | + "url": "https://sumte-file.s3.ap-northeast-2.amazonaws.com/sumte2.png" |
| 70 | + }, |
| 71 | + { |
| 72 | + "ownerType": "GUESTHOUSE", |
| 73 | + "ownerId": 1, |
| 74 | + "url": "https://sumte-file.s3.ap-northeast-2.amazonaws.com/sumte3.png" |
| 75 | + } |
| 76 | + ] |
| 77 | + """ |
| 78 | + ) |
| 79 | + } |
| 80 | + ) |
| 81 | + ) |
| 82 | + @PostMapping |
| 83 | + public ResponseEntity<List<ImageResponseDTO>> saveImagesBatch( |
| 84 | + |
| 85 | + @Valid @RequestBody List<ImageRequestDTO> dtos) { |
| 86 | + |
| 87 | + var savedList = imageService.saveAllImages(dtos); |
| 88 | + var respList = savedList.stream() |
| 89 | + .map(img -> new ImageResponseDTO( |
| 90 | + img.getId(), img.getUrl(), img.getSortOrder(), img.getOwnerType(), img.getOwnerId())) |
| 91 | + .collect(Collectors.toList()); |
| 92 | + |
| 93 | + return ResponseEntity |
| 94 | + .status(HttpStatus.CREATED) |
| 95 | + .body(respList); |
| 96 | + } |
| 97 | + |
| 98 | + @Operation( |
| 99 | + summary = "이미지 전체 교체", |
| 100 | + description = """ |
| 101 | + 주어진 ownerType/ownerId 에 등록된 모든 이미지를 삭제하고, |
| 102 | + 요청 리스트 순서대로 새 이미지를 1부터 순차 저장합니다. |
| 103 | + - S3 객체도 함께 삭제됩니다. |
| 104 | + """) |
| 105 | + @PutMapping("/{ownerType}/{ownerId}") |
| 106 | + public ResponseEntity<List<ImageResponseDTO>> replaceImages( |
| 107 | + @PathVariable OwnerType ownerType, |
| 108 | + @PathVariable Long ownerId, |
| 109 | + @io.swagger.v3.oas.annotations.parameters.RequestBody( |
| 110 | + description = "전체 교체할 이미지 리스트를 전달합니다.", |
| 111 | + required = true, |
| 112 | + content = @Content( |
| 113 | + mediaType = "application/json", |
| 114 | + array = @ArraySchema(schema = @Schema(implementation = ReplaceImageRequestDTO.class)), |
| 115 | + examples = { |
| 116 | + @ExampleObject( |
| 117 | + name = "Replace Example", |
| 118 | + value = """ |
| 119 | + [ |
| 120 | + { "url":"https://sumte-file.s3.ap-northeast-2.amazonaws.com/sumte1.png" }, |
| 121 | + { "url":"https://sumte-file.s3.ap-northeast-2.amazonaws.com/sumte2.png" }, |
| 122 | + { "url":"https://sumte-file.s3.ap-northeast-2.amazonaws.com/sumte3.png" } |
| 123 | + ] |
| 124 | + """ |
| 125 | + ) |
| 126 | + } |
| 127 | + ) |
| 128 | + ) |
| 129 | + @Valid @RequestBody List<ReplaceImageRequestDTO> replaceImageDtos |
| 130 | + ) { |
| 131 | + List<ImageRequestDTO> requests = replaceImageDtos.stream() |
| 132 | + .map(r -> new ImageRequestDTO(ownerType, ownerId, r.getUrl())) |
| 133 | + .collect(Collectors.toList()); |
| 134 | + |
| 135 | + var saved = imageService.replaceImages(ownerType, ownerId, requests); |
| 136 | + var resp = saved.stream() |
| 137 | + .map(img -> new ImageResponseDTO( |
| 138 | + img.getId(), |
| 139 | + img.getUrl(), |
| 140 | + img.getSortOrder(), |
| 141 | + img.getOwnerType(), |
| 142 | + img.getOwnerId() |
| 143 | + )) |
| 144 | + .collect(Collectors.toList()); |
| 145 | + |
| 146 | + return ResponseEntity.ok(resp); |
| 147 | + } |
| 148 | + |
| 149 | + @Operation(summary = "이미지 목록 조회", description = "주어진 ownerType, ownerId 에 매핑된 이미지 리스트를 정렬순으로 조회합니다.") |
| 150 | + @GetMapping |
| 151 | + public ResponseEntity<List<ImageResponseDTO>> getImages( |
| 152 | + @Parameter( |
| 153 | + name = "ownerType", |
| 154 | + description = "이미지 소유자 타입", |
| 155 | + example = "ROOM", |
| 156 | + required = true, |
| 157 | + in = ParameterIn.QUERY |
| 158 | + ) @RequestParam OwnerType ownerType, |
| 159 | + @Parameter( |
| 160 | + name = "ownerId", |
| 161 | + description = "소유자 ID", |
| 162 | + example = "1", |
| 163 | + required = true, |
| 164 | + in = ParameterIn.QUERY |
| 165 | + ) |
| 166 | + @RequestParam Long ownerId) { |
| 167 | + var list = imageService.getImagesByOwner(ownerType, ownerId); |
| 168 | + var dtos = list.stream() |
| 169 | + .map(img -> new ImageResponseDTO( |
| 170 | + img.getId(), img.getUrl(), img.getSortOrder(), img.getOwnerType(), img.getOwnerId())) |
| 171 | + .collect(Collectors.toList()); |
| 172 | + return ResponseEntity.ok(dtos); |
| 173 | + } |
| 174 | +} |
0 commit comments