From ab0b0fa6884fed7f78b9e1681d429ddb14f73c13 Mon Sep 17 00:00:00 2001 From: soohyeon Date: Thu, 3 Oct 2024 22:26:47 +0900 Subject: [PATCH 1/3] add :: exception --- .../meogo/domain/good/exception/AlreadyGoodException.kt | 8 ++++++++ .../meogo/domain/good/exception/GoodNotFoundException.kt | 8 ++++++++ .../kotlin/org/meogo/global/error/exception/ErrorCode.kt | 2 ++ 3 files changed, 18 insertions(+) create mode 100644 src/main/kotlin/org/meogo/domain/good/exception/AlreadyGoodException.kt create mode 100644 src/main/kotlin/org/meogo/domain/good/exception/GoodNotFoundException.kt diff --git a/src/main/kotlin/org/meogo/domain/good/exception/AlreadyGoodException.kt b/src/main/kotlin/org/meogo/domain/good/exception/AlreadyGoodException.kt new file mode 100644 index 0000000..280e5d3 --- /dev/null +++ b/src/main/kotlin/org/meogo/domain/good/exception/AlreadyGoodException.kt @@ -0,0 +1,8 @@ +package org.meogo.domain.good.exception + +import org.meogo.global.error.exception.ErrorCode +import org.meogo.global.error.exception.MeogoException + +object AlreadyGoodException : MeogoException( + ErrorCode.ALREADY_GOOD_EXCEPTION +) diff --git a/src/main/kotlin/org/meogo/domain/good/exception/GoodNotFoundException.kt b/src/main/kotlin/org/meogo/domain/good/exception/GoodNotFoundException.kt new file mode 100644 index 0000000..6720eb3 --- /dev/null +++ b/src/main/kotlin/org/meogo/domain/good/exception/GoodNotFoundException.kt @@ -0,0 +1,8 @@ +package org.meogo.domain.good.exception + +import org.meogo.global.error.exception.ErrorCode +import org.meogo.global.error.exception.MeogoException + +object GoodNotFoundException : MeogoException( + ErrorCode.GOOD_NOT_FOUND +) diff --git a/src/main/kotlin/org/meogo/global/error/exception/ErrorCode.kt b/src/main/kotlin/org/meogo/global/error/exception/ErrorCode.kt index 9c7b105..740e863 100644 --- a/src/main/kotlin/org/meogo/global/error/exception/ErrorCode.kt +++ b/src/main/kotlin/org/meogo/global/error/exception/ErrorCode.kt @@ -19,8 +19,10 @@ enum class ErrorCode( KEYWORD_NOT_FOUND(404, "Keyword not found"), QUESTION_NOT_FOUND(404, "Question not found"), BOOKMARK_NOT_FOUND(404, "Bookmark not found"), + GOOD_NOT_FOUND(404, "Good Not found"), ALREADY_WRITE_EXCEPTION(409, "You have already submitted a review"), + ALREADY_GOOD_EXCEPTION(409, "You have already booked"), // Internal Server Error INTERNAL_SERVER_ERROR(500, "Internal Server Error") From 308734218ce82c46a1005404666c09ce81c63b43 Mon Sep 17 00:00:00 2001 From: soohyeon Date: Thu, 3 Oct 2024 22:27:00 +0900 Subject: [PATCH 2/3] modify :: domain --- src/main/kotlin/org/meogo/domain/good/domain/Good.kt | 2 +- src/main/kotlin/org/meogo/domain/post/domain/Post.kt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/org/meogo/domain/good/domain/Good.kt b/src/main/kotlin/org/meogo/domain/good/domain/Good.kt index b2e9406..b88b07a 100644 --- a/src/main/kotlin/org/meogo/domain/good/domain/Good.kt +++ b/src/main/kotlin/org/meogo/domain/good/domain/Good.kt @@ -15,7 +15,7 @@ class Good( @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "post_id", nullable = true) - val post: Post? = null, + val post: Post, @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "user_id", nullable = false) 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 7ee3071..499dbb7 100644 --- a/src/main/kotlin/org/meogo/domain/post/domain/Post.kt +++ b/src/main/kotlin/org/meogo/domain/post/domain/Post.kt @@ -63,7 +63,7 @@ class Post( } fun deleteGood() { - this.good -= 1 + if (good > 0) this.good -= 1 } fun format(date: LocalDateTime) = date.format(DateTimeFormatter.ofPattern("yy.MM.dd HH:mm"))!! From 63498bcbab9552ec1561cf7ea262445d4a7b52ce Mon Sep 17 00:00:00 2001 From: soohyeon Date: Thu, 3 Oct 2024 22:27:14 +0900 Subject: [PATCH 3/3] add :: queryMyGood api --- .../domain/good/domain/GoodRepository.kt | 7 +++++- .../good/presentation/GoodController.kt | 7 ++++-- .../dto/response/MyGoodResponse.kt | 10 ++++++++ .../meogo/domain/good/service/GoodService.kt | 23 ++++++++++++++++++- 4 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 src/main/kotlin/org/meogo/domain/good/presentation/dto/response/MyGoodResponse.kt diff --git a/src/main/kotlin/org/meogo/domain/good/domain/GoodRepository.kt b/src/main/kotlin/org/meogo/domain/good/domain/GoodRepository.kt index d5e08dc..3690bf1 100644 --- a/src/main/kotlin/org/meogo/domain/good/domain/GoodRepository.kt +++ b/src/main/kotlin/org/meogo/domain/good/domain/GoodRepository.kt @@ -6,5 +6,10 @@ import org.springframework.data.jpa.repository.JpaRepository import java.util.UUID interface GoodRepository : JpaRepository { - fun findByUserAndPost(user: User, post: Post): Good + + fun findAllByUser(user: User): List? + + fun findByUserAndPost(user: User, post: Post): Good? + + fun existsByUserAndPost(user: User, post: Post): Boolean } diff --git a/src/main/kotlin/org/meogo/domain/good/presentation/GoodController.kt b/src/main/kotlin/org/meogo/domain/good/presentation/GoodController.kt index a608086..106292f 100644 --- a/src/main/kotlin/org/meogo/domain/good/presentation/GoodController.kt +++ b/src/main/kotlin/org/meogo/domain/good/presentation/GoodController.kt @@ -1,16 +1,15 @@ package org.meogo.domain.good.presentation -import lombok.RequiredArgsConstructor import org.meogo.domain.good.service.GoodService 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.PostMapping import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RequestParam import org.springframework.web.bind.annotation.ResponseStatus import org.springframework.web.bind.annotation.RestController -@RequiredArgsConstructor @RestController @RequestMapping("/good") class GoodController( @@ -25,4 +24,8 @@ class GoodController( @DeleteMapping fun deleteGood(@RequestParam(name = "post_id")postId: Long) = goodService.deleteGood(postId) + + @GetMapping("/query/my") + fun myGoods() = + goodService.queryMyGoods() } diff --git a/src/main/kotlin/org/meogo/domain/good/presentation/dto/response/MyGoodResponse.kt b/src/main/kotlin/org/meogo/domain/good/presentation/dto/response/MyGoodResponse.kt new file mode 100644 index 0000000..a0ff436 --- /dev/null +++ b/src/main/kotlin/org/meogo/domain/good/presentation/dto/response/MyGoodResponse.kt @@ -0,0 +1,10 @@ +package org.meogo.domain.good.presentation.dto.response + +data class MyGoodResponse( + val postId: Long, + val title: String, + val name: String, + val good: Int, + val date: String, + val schoolId: Int? +) diff --git a/src/main/kotlin/org/meogo/domain/good/service/GoodService.kt b/src/main/kotlin/org/meogo/domain/good/service/GoodService.kt index d1c0399..9260f37 100644 --- a/src/main/kotlin/org/meogo/domain/good/service/GoodService.kt +++ b/src/main/kotlin/org/meogo/domain/good/service/GoodService.kt @@ -2,6 +2,9 @@ package org.meogo.domain.good.service import org.meogo.domain.good.domain.Good import org.meogo.domain.good.domain.GoodRepository +import org.meogo.domain.good.exception.AlreadyGoodException +import org.meogo.domain.good.exception.GoodNotFoundException +import org.meogo.domain.good.presentation.dto.response.MyGoodResponse import org.meogo.domain.post.domain.PostRepository import org.meogo.domain.user.exception.UserNotFoundException import org.meogo.domain.user.facade.UserFacade @@ -19,6 +22,7 @@ class GoodService( fun addGood(postId: Long) { val user = userFacade.currentUser() ?: throw UserNotFoundException val post = postRepository.findById(postId) + if (goodRepository.existsByUserAndPost(user, post)) throw AlreadyGoodException post.addGood() goodRepository.save( @@ -33,9 +37,26 @@ class GoodService( fun deleteGood(postId: Long) { val user = userFacade.currentUser() ?: throw UserNotFoundException val post = postRepository.findById(postId) - val good = goodRepository.findByUserAndPost(user, post) + val good = goodRepository.findByUserAndPost(user, post) ?: throw GoodNotFoundException post.deleteGood() goodRepository.delete(good) } + + fun queryMyGoods(): List { + val user = userFacade.currentUser() ?: throw UserNotFoundException + val posts = goodRepository.findAllByUser(user) ?: return emptyList() + + return posts.map { it -> + val post = postRepository.findById(it.post.id) + MyGoodResponse( + postId = post.id, + title = post.title, + name = "익명", + good = post.good, + date = post.format(post.date), + schoolId = post.schoolId + ) + } + } }