Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

๐Ÿ„ :: (Meogo-44) bookmark #45

Merged
merged 3 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.meogo.domain.bookmark
package org.meogo.domain.bookmark.domain

import org.meogo.domain.user.domain.User
import org.meogo.global.base.BaseUUIDEntity
Expand All @@ -12,7 +12,7 @@ import javax.persistence.ManyToOne
class Bookmark(
id: UUID? = null,

val schoolId: Int? = 0,
val schoolId: Int,

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package org.meogo.domain.bookmark
package org.meogo.domain.bookmark.domain

import org.meogo.domain.user.domain.User
import org.springframework.data.jpa.repository.JpaRepository
import java.util.UUID

interface BookmarkRepository : JpaRepository<Bookmark, UUID> {
fun findAllBySchoolId(schoolId: Int): List<Bookmark>?
fun findAllByUser(user: User): List<Bookmark>?

fun existsBySchoolIdAndUser(schoolId: Int, user: User): Boolean
fun findBySchoolIdAndUser(schoolId: Int, user: User): Bookmark?

fun deleteBySchoolIdAndUser(schoolId: Int, user: User)
fun existsBySchoolIdAndUser(schoolId: Int, user: User): Boolean
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.meogo.domain.bookmark.exception

import org.meogo.global.error.exception.ErrorCode
import org.meogo.global.error.exception.MeogoException

object BookmarkNotFoundException : MeogoException(
ErrorCode.BOOKMARK_NOT_FOUND
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.meogo.domain.bookmark.presentation

import org.meogo.domain.bookmark.service.BookmarkService
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

@RestController
@RequestMapping("/bookmark")
class BookmarkController(
private val bookmarkService: BookmarkService
) {

@ResponseStatus(HttpStatus.OK)
@PostMapping
fun create(@RequestParam(name = "school_id")schoolId: Int) =
bookmarkService.execute(schoolId)

@GetMapping("/query/my")
fun myBookmarks() =
bookmarkService.queryBookmarkedSchool()

@GetMapping("/query")
fun isBookmarked(@RequestParam(name = "school_id")schoolId: Int) =
bookmarkService.queryIsBookmarked(schoolId)

@ResponseStatus(HttpStatus.NO_CONTENT)
@DeleteMapping
fun delete(@RequestParam(name = "school_id")schoolId: Int) =
bookmarkService.deleteBookmark(schoolId)
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package org.meogo.domain.bookmark
package org.meogo.domain.bookmark.service

import org.meogo.domain.bookmark.domain.Bookmark
import org.meogo.domain.bookmark.domain.BookmarkRepository
import org.meogo.domain.bookmark.exception.BookmarkNotFoundException
import org.meogo.domain.post.domain.PostRepository
import org.meogo.domain.user.exception.UserNotFoundException
import org.meogo.domain.user.facade.UserFacade
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Transactional
@Service
class BookmarkService(
private val bookmarkRepository: BookmarkRepository,
private val userFacade: UserFacade,
private val postRepository: PostRepository
) {

@Transactional
fun execute(schoolId: Int) {
val user = userFacade.currentUser() ?: throw UserNotFoundException

Expand All @@ -25,9 +28,11 @@ class BookmarkService(
)
}

fun queryBookmarkedPost(schoolId: Int): Int {
val posts = bookmarkRepository.findAllBySchoolId(schoolId)
return posts?.size ?: 0
fun queryBookmarkedSchool(): List<Int> {
val user = userFacade.currentUser() ?: throw UserNotFoundException

Copy link

@coderabbitai coderabbitai bot Oct 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

โš ๏ธ Potential issue

์˜ˆ์™ธ ์ธ์Šคํ„ด์Šคํ™” ์‹œ ๊ด„ํ˜ธ ์ถ”๊ฐ€ ํ•„์š”

throw UserNotFoundException ๋Œ€์‹  throw UserNotFoundException()์ฒ˜๋Ÿผ ์˜ˆ์™ธ๋ฅผ ์ธ์Šคํ„ด์Šคํ™”ํ•  ๋•Œ ๊ด„ํ˜ธ๋ฅผ ์ถ”๊ฐ€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ ์šฉ ๊ฐ€๋Šฅํ•œ ์ˆ˜์ • ์‚ฌํ•ญ:

-val user = userFacade.currentUser() ?: throw UserNotFoundException
+val user = userFacade.currentUser() ?: throw UserNotFoundException()
๐Ÿ“ Committable suggestion

โ€ผ๏ธ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
val user = userFacade.currentUser() ?: throw UserNotFoundException
val user = userFacade.currentUser() ?: throw UserNotFoundException()

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UserNotFoundException๊ฐ€ object๋กœ ๋˜์–ด์žˆ์–ด์„œ ๊ดœ์ฐฎใ„ด์„ ๊ฒƒ ๊ฐ™์•„

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@meltapplee, ์•Œ๊ฒ ์Šต๋‹ˆ๋‹ค. UserNotFoundException์ด ๊ฐ์ฒด๋กœ ์ •์˜๋˜์–ด ์žˆ์–ด์„œ ๊ด„ํ˜ธ ์—†์ด๋„ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๊ตฐ์š”. ์•Œ๋ ค์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

๐Ÿฐโ“


โœ๏ธ Learnings added
Learnt from: meltapplee
PR: MEOGO-DSM/MEOGO_Backend_V3#45
File: src/main/kotlin/org/meogo/domain/bookmark/service/BookmarkService.kt:32-32
Timestamp: 2024-10-03T12:41:49.733Z
Learning: UserNotFoundException is defined as an object, so it can be thrown without parentheses.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.

val posts = bookmarkRepository.findAllByUser(user)
return posts?.map { it.schoolId } ?: emptyList()
}

fun queryIsBookmarked(schoolId: Int): Boolean {
Expand All @@ -37,6 +42,7 @@ class BookmarkService(

fun deleteBookmark(schoolId: Int) {
val user = userFacade.currentUser() ?: throw UserNotFoundException
bookmarkRepository.deleteBySchoolIdAndUser(schoolId, user)
val bookmark = bookmarkRepository.findBySchoolIdAndUser(schoolId, user) ?: throw BookmarkNotFoundException
bookmarkRepository.delete(bookmark)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

โš ๏ธ Potential issue

์˜ˆ์™ธ ์ธ์Šคํ„ด์Šคํ™” ์‹œ ๊ด„ํ˜ธ ์ถ”๊ฐ€ ํ•„์š”

throw BookmarkNotFoundException ๋Œ€์‹  throw BookmarkNotFoundException()์ฒ˜๋Ÿผ ์˜ˆ์™ธ๋ฅผ ์ธ์Šคํ„ด์Šคํ™”ํ•  ๋•Œ ๊ด„ํ˜ธ๋ฅผ ์ถ”๊ฐ€ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ ์šฉ ๊ฐ€๋Šฅํ•œ ์ˆ˜์ • ์‚ฌํ•ญ:

-val bookmark = bookmarkRepository.findBySchoolIdAndUser(schoolId, user) ?: throw BookmarkNotFoundException
+val bookmark = bookmarkRepository.findBySchoolIdAndUser(schoolId, user) ?: throw BookmarkNotFoundException()
๐Ÿ“ Committable suggestion

โ€ผ๏ธ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
val bookmark = bookmarkRepository.findBySchoolIdAndUser(schoolId, user) ?: throw BookmarkNotFoundException
val bookmark = bookmarkRepository.findBySchoolIdAndUser(schoolId, user) ?: throw BookmarkNotFoundException()

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ enum class ErrorCode(
COMMENT_NOT_FOUND(404, "Comment not found"),
KEYWORD_NOT_FOUND(404, "Keyword not found"),
QUESTION_NOT_FOUND(404, "Question not found"),
BOOKMARK_NOT_FOUND(404, "Bookmark not found"),

ALREADY_WRITE_EXCEPTION(409, "You have already submitted a review"),

Expand Down
Loading