Skip to content

Commit

Permalink
Merge pull request #43 from MEOGO-DSM/42-post-good
Browse files Browse the repository at this point in the history
🏄 :: (Meogo-42) post good
  • Loading branch information
meltapplee authored Oct 2, 2024
2 parents 774f1c3 + 8d7635e commit 5f035ca
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 8 deletions.
20 changes: 20 additions & 0 deletions src/main/kotlin/org/meogo/domain/bookmark/Bookmark.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.meogo.domain.bookmark

import org.meogo.domain.user.domain.User
import org.meogo.global.base.BaseUUIDEntity
import java.util.UUID
import javax.persistence.Entity
import javax.persistence.FetchType
import javax.persistence.JoinColumn
import javax.persistence.ManyToOne

@Entity
class Bookmark(
id: UUID? = null,

val schoolId: Int? = 0,

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false)
val user: User
) : BaseUUIDEntity(id)
13 changes: 13 additions & 0 deletions src/main/kotlin/org/meogo/domain/bookmark/BookmarkRepository.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.meogo.domain.bookmark

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 existsBySchoolIdAndUser(schoolId: Int, user: User): Boolean

fun deleteBySchoolIdAndUser(schoolId: Int, user: User)
}
42 changes: 42 additions & 0 deletions src/main/kotlin/org/meogo/domain/bookmark/BookmarkService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.meogo.domain.bookmark

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

@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

bookmarkRepository.save(
Bookmark(
schoolId = schoolId,
user = user
)
)
}

fun queryBookmarkedPost(schoolId: Int): Int {
val posts = bookmarkRepository.findAllBySchoolId(schoolId)
return posts?.size ?: 0
}

fun queryIsBookmarked(schoolId: Int): Boolean {
val user = userFacade.currentUser() ?: throw UserNotFoundException
return bookmarkRepository.existsBySchoolIdAndUser(schoolId, user)
}

fun deleteBookmark(schoolId: Int) {
val user = userFacade.currentUser() ?: throw UserNotFoundException
bookmarkRepository.deleteBySchoolIdAndUser(schoolId, user)
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
package org.meogo.domain.good
package org.meogo.domain.good.domain

import org.meogo.domain.post.domain.Post
import org.meogo.domain.user.domain.User
import org.meogo.global.base.BaseUUIDEntity
import java.util.UUID
import javax.persistence.Entity
import javax.persistence.FetchType
import javax.persistence.JoinColumn
import javax.persistence.ManyToOne
import org.meogo.domain.post.domain.Post
import org.meogo.domain.user.domain.User
import org.meogo.global.base.BaseUUIDEntity

@Entity
class Bookmark (
class Good(
id: UUID? = null,


@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "post_id", nullable = true)
val post: Post? = null,


@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false)
val user: User

) : BaseUUIDEntity(id)
) : BaseUUIDEntity(id)
10 changes: 10 additions & 0 deletions src/main/kotlin/org/meogo/domain/good/domain/GoodRepository.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.meogo.domain.good.domain

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

interface GoodRepository : JpaRepository<Good, UUID> {
fun findByUserAndPost(user: User, post: Post): Good
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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.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(
private val goodService: GoodService
) {
@ResponseStatus(HttpStatus.CREATED)
@PostMapping
fun addGood(@RequestParam(name = "post_id")postId: Long) =
goodService.addGood(postId)

@ResponseStatus(HttpStatus.NO_CONTENT)
@DeleteMapping
fun deleteGood(@RequestParam(name = "post_id")postId: Long) =
goodService.deleteGood(postId)
}
41 changes: 41 additions & 0 deletions src/main/kotlin/org/meogo/domain/good/service/GoodService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.meogo.domain.good.service

import org.meogo.domain.good.domain.Good
import org.meogo.domain.good.domain.GoodRepository
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

@Service
class GoodService(
private val goodRepository: GoodRepository,
private val userFacade: UserFacade,
private val postRepository: PostRepository
) {

@Transactional
fun addGood(postId: Long) {
val user = userFacade.currentUser() ?: throw UserNotFoundException
val post = postRepository.findById(postId)

post.addGood()
goodRepository.save(
Good(
post = post,
user = user
)
)
}

@Transactional
fun deleteGood(postId: Long) {
val user = userFacade.currentUser() ?: throw UserNotFoundException
val post = postRepository.findById(postId)
val good = goodRepository.findByUserAndPost(user, post)

post.deleteGood()
goodRepository.delete(good)
}
}
4 changes: 4 additions & 0 deletions src/main/kotlin/org/meogo/domain/post/domain/Post.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,9 @@ class Post(
this.good += 1
}

fun deleteGood() {
this.good -= 1
}

fun format(date: LocalDateTime) = date.format(DateTimeFormatter.ofPattern("yy.MM.dd HH:mm"))!!
}

0 comments on commit 5f035ca

Please sign in to comment.