Skip to content

Commit

Permalink
Merge pull request #45 from MEOGO-DSM/44-bookmark
Browse files Browse the repository at this point in the history
🏄 :: (Meogo-44) bookmark
  • Loading branch information
meltapplee authored Oct 3, 2024
2 parents 5f035ca + c016579 commit d887bbb
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 12 deletions.
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

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)
}
}
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

0 comments on commit d887bbb

Please sign in to comment.