From 988f5360cbf4efdf303afcff79f76bc008ce650e Mon Sep 17 00:00:00 2001 From: soohyeon Date: Sat, 14 Sep 2024 22:56:23 +0900 Subject: [PATCH 1/2] refactor :: presentation --- .../post/{present => presentation}/dto/request/PostRequest.kt | 2 +- .../post/{present => presentation}/dto/response/PostResponse.kt | 2 +- .../kotlin/org/meogo/domain/post/service/CreatePostService.kt | 2 +- .../kotlin/org/meogo/domain/post/service/QueryAllPostService.kt | 2 +- .../org/meogo/domain/post/service/QuerySchoolPostService.kt | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) rename src/main/kotlin/org/meogo/domain/post/{present => presentation}/dto/request/PostRequest.kt (81%) rename src/main/kotlin/org/meogo/domain/post/{present => presentation}/dto/response/PostResponse.kt (92%) diff --git a/src/main/kotlin/org/meogo/domain/post/present/dto/request/PostRequest.kt b/src/main/kotlin/org/meogo/domain/post/presentation/dto/request/PostRequest.kt similarity index 81% rename from src/main/kotlin/org/meogo/domain/post/present/dto/request/PostRequest.kt rename to src/main/kotlin/org/meogo/domain/post/presentation/dto/request/PostRequest.kt index 0455f33..5eb33c6 100644 --- a/src/main/kotlin/org/meogo/domain/post/present/dto/request/PostRequest.kt +++ b/src/main/kotlin/org/meogo/domain/post/presentation/dto/request/PostRequest.kt @@ -1,4 +1,4 @@ -package org.meogo.domain.post.present.dto.request +package org.meogo.domain.post.presentation.dto.request import javax.validation.constraints.Size diff --git a/src/main/kotlin/org/meogo/domain/post/present/dto/response/PostResponse.kt b/src/main/kotlin/org/meogo/domain/post/presentation/dto/response/PostResponse.kt similarity index 92% rename from src/main/kotlin/org/meogo/domain/post/present/dto/response/PostResponse.kt rename to src/main/kotlin/org/meogo/domain/post/presentation/dto/response/PostResponse.kt index 9a556e4..2c784f7 100644 --- a/src/main/kotlin/org/meogo/domain/post/present/dto/response/PostResponse.kt +++ b/src/main/kotlin/org/meogo/domain/post/presentation/dto/response/PostResponse.kt @@ -1,4 +1,4 @@ -package org.meogo.domain.post.present.dto.response +package org.meogo.domain.post.presentation.dto.response import org.meogo.domain.post.domain.Post import org.meogo.global.s3.FileUtil diff --git a/src/main/kotlin/org/meogo/domain/post/service/CreatePostService.kt b/src/main/kotlin/org/meogo/domain/post/service/CreatePostService.kt index 816a72c..d3100e4 100644 --- a/src/main/kotlin/org/meogo/domain/post/service/CreatePostService.kt +++ b/src/main/kotlin/org/meogo/domain/post/service/CreatePostService.kt @@ -2,7 +2,7 @@ package org.meogo.domain.post.service import org.meogo.domain.post.domain.Post import org.meogo.domain.post.domain.PostRepository -import org.meogo.domain.post.present.dto.request.PostRequest +import org.meogo.domain.post.presentation.dto.request.PostRequest import org.meogo.domain.user.exception.UserNotFoundException import org.meogo.domain.user.facade.UserFacade import org.meogo.global.s3.FileUtil diff --git a/src/main/kotlin/org/meogo/domain/post/service/QueryAllPostService.kt b/src/main/kotlin/org/meogo/domain/post/service/QueryAllPostService.kt index b605a56..3f74c99 100644 --- a/src/main/kotlin/org/meogo/domain/post/service/QueryAllPostService.kt +++ b/src/main/kotlin/org/meogo/domain/post/service/QueryAllPostService.kt @@ -1,7 +1,7 @@ package org.meogo.domain.post.service import org.meogo.domain.post.domain.PostRepository -import org.meogo.domain.post.present.dto.response.PostResponse +import org.meogo.domain.post.presentation.dto.response.PostResponse import org.meogo.global.s3.FileUtil import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional diff --git a/src/main/kotlin/org/meogo/domain/post/service/QuerySchoolPostService.kt b/src/main/kotlin/org/meogo/domain/post/service/QuerySchoolPostService.kt index a258abb..142e7b5 100644 --- a/src/main/kotlin/org/meogo/domain/post/service/QuerySchoolPostService.kt +++ b/src/main/kotlin/org/meogo/domain/post/service/QuerySchoolPostService.kt @@ -1,7 +1,7 @@ package org.meogo.domain.post.service import org.meogo.domain.post.domain.PostRepository -import org.meogo.domain.post.present.dto.response.PostResponse +import org.meogo.domain.post.presentation.dto.response.PostResponse import org.meogo.global.s3.FileUtil import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional From 3b31882ac8a87072bc60768c3af2d44c9244d037 Mon Sep 17 00:00:00 2001 From: soohyeon Date: Sat, 14 Sep 2024 22:56:41 +0900 Subject: [PATCH 2/2] add :: post modify api --- .../org/meogo/domain/post/domain/Post.kt | 19 ++++++--- .../PostController.kt | 18 ++++++-- .../domain/post/service/ModifyPostService.kt | 42 +++++++++++++++++++ 3 files changed, 71 insertions(+), 8 deletions(-) rename src/main/kotlin/org/meogo/domain/post/{present => presentation}/PostController.kt (74%) create mode 100644 src/main/kotlin/org/meogo/domain/post/service/ModifyPostService.kt diff --git a/src/main/kotlin/org/meogo/domain/post/domain/Post.kt b/src/main/kotlin/org/meogo/domain/post/domain/Post.kt index e6638e1..1d2ee10 100644 --- a/src/main/kotlin/org/meogo/domain/post/domain/Post.kt +++ b/src/main/kotlin/org/meogo/domain/post/domain/Post.kt @@ -17,10 +17,10 @@ class Post( val id: Long = 0, @Column(nullable = false) - val title: String, + var title: String, @Column(nullable = false) - val content: String, + var content: String, @Column(name = "user_id", columnDefinition = "BINARY(16)") val userId: UUID, @@ -32,13 +32,22 @@ class Post( val date: LocalDateTime, @Column(name = "school_id") - val schoolId: Int?, + var schoolId: Int?, @Column(name = "key_word") - val keyWord: String?, + var keyWord: String?, - val image: String? + var image: String? ) { + fun update(title: String, content: String, schoolId: Int? = null, keyWord: String? = null, image: String?): Post { + this.title = title + this.content = content + this.schoolId = schoolId + this.keyWord = keyWord + this.image = image + return this + } + fun addGood() { this.good += 1 } diff --git a/src/main/kotlin/org/meogo/domain/post/present/PostController.kt b/src/main/kotlin/org/meogo/domain/post/presentation/PostController.kt similarity index 74% rename from src/main/kotlin/org/meogo/domain/post/present/PostController.kt rename to src/main/kotlin/org/meogo/domain/post/presentation/PostController.kt index 9e174a7..87307c1 100644 --- a/src/main/kotlin/org/meogo/domain/post/present/PostController.kt +++ b/src/main/kotlin/org/meogo/domain/post/presentation/PostController.kt @@ -1,14 +1,16 @@ -package org.meogo.domain.post.present +package org.meogo.domain.post.presentation import lombok.RequiredArgsConstructor -import org.meogo.domain.post.present.dto.request.PostRequest +import org.meogo.domain.post.presentation.dto.request.PostRequest import org.meogo.domain.post.service.CreatePostService import org.meogo.domain.post.service.DeletePostService +import org.meogo.domain.post.service.ModifyPostService import org.meogo.domain.post.service.QueryAllPostService import org.meogo.domain.post.service.QuerySchoolPostService import org.springframework.http.HttpStatus import org.springframework.web.bind.annotation.DeleteMapping import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.PatchMapping import org.springframework.web.bind.annotation.PostMapping import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RequestParam @@ -25,7 +27,8 @@ class PostController( private val createPostService: CreatePostService, private val queryAllPostService: QueryAllPostService, private val querySchoolPostService: QuerySchoolPostService, - private val deletePostService: DeletePostService + private val deletePostService: DeletePostService, + private val modifyPostService: ModifyPostService ) { @PostMapping @@ -43,6 +46,15 @@ class PostController( fun delete(@RequestParam("post_id") id: Long) = deletePostService.execute(id) + @PatchMapping("/modify") + fun modify( + @RequestParam("post_id") id: Long, + @Valid + @RequestPart("request") + request: PostRequest, + @RequestPart("image") image: MultipartFile? + ) = modifyPostService.execute(id, request, image) + @GetMapping("/query/all") fun queryAll() = queryAllPostService.execute() diff --git a/src/main/kotlin/org/meogo/domain/post/service/ModifyPostService.kt b/src/main/kotlin/org/meogo/domain/post/service/ModifyPostService.kt new file mode 100644 index 0000000..eb33a63 --- /dev/null +++ b/src/main/kotlin/org/meogo/domain/post/service/ModifyPostService.kt @@ -0,0 +1,42 @@ +package org.meogo.domain.post.service + +import org.meogo.domain.post.domain.PostRepository +import org.meogo.domain.post.exception.PostNotFoundException +import org.meogo.domain.post.presentation.dto.request.PostRequest +import org.meogo.domain.user.exception.UserMisMatchException +import org.meogo.domain.user.facade.UserFacade +import org.meogo.global.s3.FileUtil +import org.meogo.global.s3.Path +import org.springframework.stereotype.Service +import org.springframework.transaction.annotation.Transactional +import org.springframework.web.multipart.MultipartFile + +@Service +class ModifyPostService( + private val postRepository: PostRepository, + private val userFacade: UserFacade, + private val fileUtil: FileUtil +) { + @Transactional + fun execute(postId: Long, request: PostRequest, image: MultipartFile?) { + val user = userFacade.currentUser() + val post = postRepository.findById(postId) ?: throw PostNotFoundException + + if (user!!.id != post.userId) throw UserMisMatchException + + val updateImage = handleImage(post.image, image) + val schoolId = if (!request.isOk || user.enrolledSchool == null) null else user.enrolledSchool + val keyWord = request.keyWord?.joinToString(separator = ",") + + postRepository.save(post.update(request.title, request.content, schoolId, keyWord, updateImage)) + } + + private fun handleImage(image: String?, newImage: MultipartFile?): String? { + image?.let { fileUtil.delete(it, Path.COMMUNITY) } + return if (newImage == null) { + null + } else { + fileUtil.upload(newImage, Path.COMMUNITY) + } + } +}