Skip to content

Commit

Permalink
Merge pull request #171 from mash-up-kr/feature/add-place-images
Browse files Browse the repository at this point in the history
  • Loading branch information
KimDoubleB authored Aug 22, 2024
2 parents bfbac08 + ac4c5ab commit 2247001
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.piikii.application.domain.image

enum class ImageFolderType {
ROOM,
PLACE,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.piikii.application.domain.image

import com.piikii.application.port.input.ImageUploadUseCase
import com.piikii.application.port.output.objectstorage.ObjectStoragePort
import org.springframework.stereotype.Service
import org.springframework.web.multipart.MultipartFile

@Service
class ImageUploadService(
private val objectStoragePort: ObjectStoragePort,
) : ImageUploadUseCase {
override fun upload(
images: List<MultipartFile>?,
imageFolderType: ImageFolderType,
): List<String> {
return images?.let {
objectStoragePort.uploadAll(
imageFolderType = imageFolderType,
multipartFiles = it,
).get()
} ?: emptyList()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package com.piikii.application.domain.place

import com.piikii.application.domain.generic.LongTypeId
import com.piikii.application.domain.generic.UuidTypeId
import com.piikii.application.domain.image.ImageFolderType
import com.piikii.application.port.input.PlaceUseCase
import com.piikii.application.port.input.dto.request.AddPlaceRequest
import com.piikii.application.port.input.dto.request.ModifyPlaceRequest
import com.piikii.application.port.input.dto.response.PlaceResponse
import com.piikii.application.port.input.dto.response.ScheduleTypeGroupResponse
import com.piikii.application.port.output.objectstorage.BucketFolderType
import com.piikii.application.port.output.objectstorage.ObjectStoragePort
import com.piikii.application.port.output.persistence.PlaceCommandPort
import com.piikii.application.port.output.persistence.PlaceQueryPort
Expand All @@ -32,25 +32,16 @@ class PlaceService(
override fun addPlace(
targetRoomUid: UuidTypeId,
addPlaceRequest: AddPlaceRequest,
placeImages: List<MultipartFile>?,
placeImageUrls: List<String>,
): List<PlaceResponse> {
val imageUrls =
placeImages?.let {
objectStoragePort.uploadAll(
bucketFolderType = BUCKET_TYPE,
multipartFiles = it,
).get()
} ?: listOf()

val room = roomQueryPort.findById(targetRoomUid)
val schedules = scheduleQueryPort.findAllByIds(addPlaceRequest.scheduleIds.map(::LongTypeId))

val places =
schedules.map { schedule ->
addPlaceRequest.toDomain(
roomUid = room.roomUid,
scheduleId = schedule.id,
imageUrls = imageUrls,
imageUrls = placeImageUrls,
)
}

Expand Down Expand Up @@ -83,7 +74,7 @@ class PlaceService(
val updatedUrls =
newPlaceImages?.let {
objectStoragePort.updateAllByUrls(
bucketFolderType = BUCKET_TYPE,
imageFolderType = BUCKET_TYPE,
deleteTargetUrls = modifyPlaceRequest.deleteTargetUrls,
newMultipartFiles = it,
).get()
Expand All @@ -107,7 +98,7 @@ class PlaceService(
@Transactional
override fun delete(targetPlaceId: LongTypeId) {
objectStoragePort.deleteAllByUrls(
bucketFolderType = BUCKET_TYPE,
imageFolderType = BUCKET_TYPE,
deleteTargetUrls = isPlaceNullOrGet(targetPlaceId).thumbnailLinks.convertToList,
)
placeCommandPort.delete(targetPlaceId)
Expand All @@ -128,6 +119,6 @@ class PlaceService(
}

companion object {
private val BUCKET_TYPE = BucketFolderType.PLACE
private val BUCKET_TYPE = ImageFolderType.PLACE
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.piikii.application.port.input

import com.piikii.application.domain.image.ImageFolderType
import org.springframework.web.multipart.MultipartFile

interface ImageUploadUseCase {
fun upload(
images: List<MultipartFile>?,
imageFolderType: ImageFolderType,
): List<String>
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface PlaceUseCase {
fun addPlace(
targetRoomUid: UuidTypeId,
addPlaceRequest: AddPlaceRequest,
placeImages: List<MultipartFile>?,
placeImageUrls: List<String>,
): List<PlaceResponse>

fun findAllByRoomUidGroupByPlaceType(roomUid: UuidTypeId): List<ScheduleTypeGroupResponse>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,22 @@ data class AddPlaceRequest(
val openingHours: String?,
@field:Schema(description = "Place 정보 출처", example = "AVOCADO")
val origin: Origin = Origin.MANUAL,
@field:Schema(description = "자동완성에서 가져온 장소 이미지 URL 리스트")
val autoCompletedPlaceImageUrls: List<String> = emptyList(),
) {
fun toDomain(
roomUid: UuidTypeId,
scheduleId: LongTypeId,
imageUrls: List<String>,
): Place {
val thumbnailLinks = ThumbnailLinks(imageUrls + autoCompletedPlaceImageUrls)
return Place(
id = LongTypeId(0L),
roomUid = roomUid,
scheduleId = scheduleId,
name = name,
url = url,
thumbnailLinks = ThumbnailLinks(imageUrls),
thumbnailLinks = thumbnailLinks,
address = address,
phoneNumber = phoneNumber,
starGrade = starGrade,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
package com.piikii.application.port.output.objectstorage

import com.piikii.application.domain.image.ImageFolderType
import org.springframework.web.multipart.MultipartFile
import java.util.concurrent.Future

interface ObjectStoragePort {
fun uploadAll(
bucketFolderType: BucketFolderType,
imageFolderType: ImageFolderType,
multipartFiles: List<MultipartFile>,
): Future<List<String>>

fun updateAllByUrls(
bucketFolderType: BucketFolderType,
imageFolderType: ImageFolderType,
deleteTargetUrls: List<String>,
newMultipartFiles: List<MultipartFile>,
): Future<List<String>>

fun deleteAllByUrls(
bucketFolderType: BucketFolderType,
imageFolderType: ImageFolderType,
deleteTargetUrls: List<String>,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package com.piikii.input.http.controller

import com.piikii.application.domain.generic.LongTypeId
import com.piikii.application.domain.generic.UuidTypeId
import com.piikii.application.domain.image.ImageFolderType
import com.piikii.application.port.input.ImageUploadUseCase
import com.piikii.application.port.input.PlaceUseCase
import com.piikii.application.port.input.dto.request.AddPlaceRequest
import com.piikii.application.port.input.dto.request.ModifyPlaceRequest
Expand Down Expand Up @@ -31,6 +33,7 @@ import java.util.UUID
@RequestMapping("/v1/rooms/{roomUid}/places")
class PlaceApi(
private val placeUseCase: PlaceUseCase,
private val imageUploadUseCase: ImageUploadUseCase,
) : PlaceDocs {
@ResponseStatus(HttpStatus.CREATED)
@PostMapping(consumes = [MediaType.MULTIPART_FORM_DATA_VALUE])
Expand All @@ -39,7 +42,8 @@ class PlaceApi(
@Valid @NotNull @RequestPart addPlaceRequest: AddPlaceRequest,
@RequestPart(required = false) placeImages: List<MultipartFile>?,
): ResponseForm<List<PlaceResponse>> {
return ResponseForm(placeUseCase.addPlace(UuidTypeId(roomUid), addPlaceRequest, placeImages))
val placeImageUrls = imageUploadUseCase.upload(placeImages, ImageFolderType.PLACE)
return ResponseForm(placeUseCase.addPlace(UuidTypeId(roomUid), addPlaceRequest, placeImageUrls))
}

@GetMapping
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.amazonaws.services.s3.model.CannedAccessControlList
import com.amazonaws.services.s3.model.DeleteObjectRequest
import com.amazonaws.services.s3.model.ObjectMetadata
import com.amazonaws.services.s3.model.PutObjectRequest
import com.piikii.application.port.output.objectstorage.BucketFolderType
import com.piikii.application.domain.image.ImageFolderType
import com.piikii.application.port.output.objectstorage.ObjectStoragePort
import com.piikii.output.storage.ncp.config.NcpProperties
import com.piikii.output.storage.ncp.config.StorageConfig
Expand All @@ -22,13 +22,13 @@ class NcpObjectStorageAdapter(
) : ObjectStoragePort {
@Async("AsyncStorageExecutor")
override fun uploadAll(
bucketFolderType: BucketFolderType,
imageFolderType: ImageFolderType,
multipartFiles: List<MultipartFile>,
): Future<List<String>> {
val contentUrls = mutableListOf<String>()

for (multipartFile in multipartFiles) {
val folderName = getFolderName(bucketFolderType)
val folderName = getFolderName(imageFolderType)
val fileName = getFileName(multipartFile)
val objectKey = createObjectKey(folderName, fileName)

Expand All @@ -48,17 +48,17 @@ class NcpObjectStorageAdapter(

@Async("TaskExecutorForExternalStorage")
override fun updateAllByUrls(
bucketFolderType: BucketFolderType,
imageFolderType: ImageFolderType,
deleteTargetUrls: List<String>,
newMultipartFiles: List<MultipartFile>,
): Future<List<String>> {
deleteAllByUrls(bucketFolderType, deleteTargetUrls)
return uploadAll(bucketFolderType, newMultipartFiles)
deleteAllByUrls(imageFolderType, deleteTargetUrls)
return uploadAll(imageFolderType, newMultipartFiles)
}

@Async("TaskExecutorForExternalStorage")
override fun deleteAllByUrls(
bucketFolderType: BucketFolderType,
imageFolderType: ImageFolderType,
deleteTargetUrls: List<String>,
) {
val storageClient = storageConfig.storageClient()
Expand All @@ -71,10 +71,10 @@ class NcpObjectStorageAdapter(

private fun getFileName(multipartFile: MultipartFile) = "${multipartFile.originalFilename}"

private fun getFolderName(bucketFolderType: BucketFolderType): String {
return when (bucketFolderType) {
BucketFolderType.ROOM -> ncpProperties.bucket.folder.roomFolder
BucketFolderType.PLACE -> ncpProperties.bucket.folder.placeFolder
private fun getFolderName(imageFolderType: ImageFolderType): String {
return when (imageFolderType) {
ImageFolderType.ROOM -> ncpProperties.bucket.folder.roomFolder
ImageFolderType.PLACE -> ncpProperties.bucket.folder.placeFolder
}
}

Expand Down

0 comments on commit 2247001

Please sign in to comment.