-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43 from MEOGO-DSM/42-post-good
🏄 :: (Meogo-42) post good
- Loading branch information
Showing
8 changed files
with
164 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
src/main/kotlin/org/meogo/domain/bookmark/BookmarkRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
42
src/main/kotlin/org/meogo/domain/bookmark/BookmarkService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
14 changes: 6 additions & 8 deletions
14
.../kotlin/org/meogo/domain/good/Bookmark.kt → ...tlin/org/meogo/domain/good/domain/Good.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
10
src/main/kotlin/org/meogo/domain/good/domain/GoodRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/kotlin/org/meogo/domain/good/presentation/GoodController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
41
src/main/kotlin/org/meogo/domain/good/service/GoodService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters