Skip to content

Commit

Permalink
Merge pull request #47 from MEOGO-DSM/46-query-my-good
Browse files Browse the repository at this point in the history
🏄 :: (Meogo-46) query my good
  • Loading branch information
meltapplee authored Oct 3, 2024
2 parents d887bbb + 63498bc commit 8d0f4d3
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/org/meogo/domain/good/domain/Good.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@ import org.springframework.data.jpa.repository.JpaRepository
import java.util.UUID

interface GoodRepository : JpaRepository<Good, UUID> {
fun findByUserAndPost(user: User, post: Post): Good

fun findAllByUser(user: User): List<Good>?

fun findByUserAndPost(user: User, post: Post): Good?

fun existsByUserAndPost(user: User, post: Post): Boolean
}
Original file line number Diff line number Diff line change
@@ -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
)
Original file line number Diff line number Diff line change
@@ -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
)
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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()
}
Original file line number Diff line number Diff line change
@@ -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?
)
23 changes: 22 additions & 1 deletion src/main/kotlin/org/meogo/domain/good/service/GoodService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -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<MyGoodResponse> {
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
)
}
}
}
2 changes: 1 addition & 1 deletion src/main/kotlin/org/meogo/domain/post/domain/Post.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"))!!
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/org/meogo/global/error/exception/ErrorCode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 8d0f4d3

Please sign in to comment.