-
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.
Browse files
Browse the repository at this point in the history
[피드] 댓글, 공유하기, 좋아요 CRUD 작성
- Loading branch information
Showing
23 changed files
with
406 additions
and
18 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
sns_service/src/main/kotlin/joryu/sns_service/comment/controller/CommentController.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,46 @@ | ||
package joryu.sns_service.comment.controller | ||
|
||
import joryu.sns_service.comment.dto.request.CommentCreateRequest | ||
import joryu.sns_service.comment.dto.request.CommentUpdateRequest | ||
import joryu.sns_service.comment.dto.response.CommentResponse | ||
import joryu.sns_service.comment.service.CommentService | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.web.bind.annotation.DeleteMapping | ||
import org.springframework.web.bind.annotation.GetMapping | ||
import org.springframework.web.bind.annotation.PathVariable | ||
import org.springframework.web.bind.annotation.PostMapping | ||
import org.springframework.web.bind.annotation.PutMapping | ||
import org.springframework.web.bind.annotation.RequestBody | ||
import org.springframework.web.bind.annotation.RequestMapping | ||
import org.springframework.web.bind.annotation.RestController | ||
import java.net.URI | ||
|
||
@RestController | ||
@RequestMapping("/comments") | ||
class CommentController( | ||
private val commentService: CommentService | ||
) { | ||
@PostMapping | ||
fun createComment(@RequestBody req: CommentCreateRequest): ResponseEntity<Void> { | ||
val commentId = commentService.create(req.postId, req.parentCommentId, req.content) | ||
return ResponseEntity.created(URI.create("/comments/${commentId}")).build() | ||
} | ||
|
||
@GetMapping("/{id}") | ||
fun findComment(@PathVariable id: Long): ResponseEntity<CommentResponse> { | ||
val comment = commentService.findOneById(id) | ||
return ResponseEntity.ok(CommentResponse(comment)) | ||
} | ||
|
||
@PutMapping("/{id}") | ||
fun updateComment(@PathVariable id: Long, @RequestBody req: CommentUpdateRequest): ResponseEntity<Void> { | ||
commentService.update(id, req.content) | ||
return ResponseEntity.noContent().build() | ||
} | ||
|
||
@DeleteMapping("/{id}") | ||
fun deleteComment(@PathVariable id: Long): ResponseEntity<Void> { | ||
commentService.delete(id) | ||
return ResponseEntity.noContent().build() | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
sns_service/src/main/kotlin/joryu/sns_service/comment/dto/request/CommentCreateRequest.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,7 @@ | ||
package joryu.sns_service.comment.dto.request | ||
|
||
data class CommentCreateRequest( | ||
val postId: Long, | ||
val parentCommentId: Long?, | ||
val content: String | ||
) |
5 changes: 5 additions & 0 deletions
5
sns_service/src/main/kotlin/joryu/sns_service/comment/dto/request/CommentUpdateRequest.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,5 @@ | ||
package joryu.sns_service.comment.dto.request | ||
|
||
data class CommentUpdateRequest( | ||
val content: String | ||
) |
17 changes: 17 additions & 0 deletions
17
sns_service/src/main/kotlin/joryu/sns_service/comment/dto/response/CommentResponse.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,17 @@ | ||
package joryu.sns_service.comment.dto.response | ||
|
||
import joryu.sns_service.comment.entity.Comment | ||
|
||
data class CommentResponse( | ||
val commentId: Long, | ||
val postId: Long, | ||
val parentCommentId: Long?, | ||
val content: String, | ||
) { | ||
constructor(comment: Comment) : this( | ||
commentId = comment.id, | ||
postId = comment.post.id, | ||
parentCommentId = comment.parent?.id, | ||
content = comment.content, | ||
) | ||
} |
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
36 changes: 36 additions & 0 deletions
36
sns_service/src/main/kotlin/joryu/sns_service/comment/service/CommentService.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,36 @@ | ||
package joryu.sns_service.comment.service | ||
|
||
import joryu.sns_service.comment.entity.Comment | ||
import joryu.sns_service.comment.repository.CommentRepository | ||
import joryu.sns_service.post.repository.PostRepository | ||
import org.springframework.stereotype.Service | ||
import org.springframework.transaction.annotation.Transactional | ||
|
||
@Service | ||
@Transactional(readOnly = true) | ||
class CommentService( | ||
private val postRepository: PostRepository, | ||
private val commentRepository: CommentRepository, | ||
) { | ||
@Transactional | ||
fun create(postId: Long, parentCommentId: Long?, content: String): Long { | ||
val post = postRepository.findById(postId).orElseThrow() | ||
val parentComment = parentCommentId?.let { commentRepository.findById(it).orElseThrow() } | ||
return commentRepository.save(Comment(post, parentComment, content)).id | ||
} | ||
|
||
fun findOneById(id: Long): Comment { | ||
return commentRepository.findById(id).orElseThrow() | ||
} | ||
|
||
@Transactional | ||
fun update(id: Long, newContent: String) { | ||
val commentForUpdate = commentRepository.findById(id).orElseThrow() | ||
commentForUpdate.changeContent(newContent) | ||
} | ||
|
||
@Transactional | ||
fun delete(id: Long) { | ||
commentRepository.deleteById(id) | ||
} | ||
} |
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
29 changes: 29 additions & 0 deletions
29
sns_service/src/main/kotlin/joryu/sns_service/post/controller/PostLikeController.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,29 @@ | ||
package joryu.sns_service.post.controller | ||
|
||
import joryu.sns_service.post.dto.response.PostLikeCountResponse | ||
import joryu.sns_service.post.service.PostLikeService | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.web.bind.annotation.DeleteMapping | ||
import org.springframework.web.bind.annotation.GetMapping | ||
import org.springframework.web.bind.annotation.PathVariable | ||
import org.springframework.web.bind.annotation.PostMapping | ||
import org.springframework.web.bind.annotation.RequestMapping | ||
import org.springframework.web.bind.annotation.RestController | ||
|
||
@RestController | ||
@RequestMapping("/like") | ||
class PostLikeController( | ||
private val postLikeService: PostLikeService | ||
) { | ||
@PostMapping("/posts/{postId}/member/{memberId}") | ||
fun like(@PathVariable postId: Long, @PathVariable memberId: Long): ResponseEntity<Void> { | ||
postLikeService.like(postId, memberId) | ||
return ResponseEntity.noContent().build() | ||
} | ||
|
||
@DeleteMapping("/posts/{postId}/member/{memberId}") | ||
fun cancelLike(@PathVariable postId: Long, @PathVariable memberId: Long): ResponseEntity<Void> { | ||
postLikeService.cancelLike(postId, memberId) | ||
return ResponseEntity.noContent().build() | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
sns_service/src/main/kotlin/joryu/sns_service/post/controller/ShareController.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,35 @@ | ||
package joryu.sns_service.post.controller | ||
|
||
import joryu.sns_service.post.dto.response.PostResponse | ||
import joryu.sns_service.post.service.ShareService | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.web.bind.annotation.DeleteMapping | ||
import org.springframework.web.bind.annotation.GetMapping | ||
import org.springframework.web.bind.annotation.PathVariable | ||
import org.springframework.web.bind.annotation.PostMapping | ||
import org.springframework.web.bind.annotation.RequestMapping | ||
import org.springframework.web.bind.annotation.RestController | ||
|
||
@RestController | ||
@RequestMapping("/share") | ||
class ShareController( | ||
private val shareService: ShareService | ||
) { | ||
@PostMapping("/posts/{postId}/member/{memberId}") | ||
fun share(@PathVariable postId: Long, @PathVariable memberId: Long): ResponseEntity<Void> { | ||
shareService.share(postId, memberId) | ||
return ResponseEntity.noContent().build() | ||
} | ||
|
||
@GetMapping("/posts/member/{memberId}") | ||
fun findAllSharePosts(@PathVariable memberId: Long): ResponseEntity<List<PostResponse>> { | ||
val shares = shareService.findAllSharePosts(memberId) | ||
return ResponseEntity.ok(shares.map { share -> PostResponse(share.post) }) | ||
} | ||
|
||
@DeleteMapping("/posts/{postId}/member/{memberId}") | ||
fun cancelLike(@PathVariable postId: Long, @PathVariable memberId: Long): ResponseEntity<Void> { | ||
shareService.cancelShare(postId, memberId) | ||
return ResponseEntity.noContent().build() | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...service/post/dto/req/PostCreateRequest.kt → ...ice/post/dto/request/PostCreateRequest.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
2 changes: 1 addition & 1 deletion
2
...service/post/dto/req/PostUpdateRequest.kt → ...ice/post/dto/request/PostUpdateRequest.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
5 changes: 5 additions & 0 deletions
5
sns_service/src/main/kotlin/joryu/sns_service/post/dto/response/PostLikeCountResponse.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,5 @@ | ||
package joryu.sns_service.post.dto.response | ||
|
||
data class PostLikeCountResponse( | ||
val likeCount: Long, | ||
) |
6 changes: 4 additions & 2 deletions
6
...sns_service/post/dto/resp/PostResponse.kt → ...service/post/dto/response/PostResponse.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,15 +1,17 @@ | ||
package joryu.sns_service.post.dto.resp | ||
package joryu.sns_service.post.dto.response | ||
|
||
import joryu.sns_service.post.entity.Post | ||
|
||
data class PostResponse( | ||
val id: Long, | ||
val content: String, | ||
val viewCount: Long, | ||
val likeCount: Long, | ||
) { | ||
constructor(post: Post) : this( | ||
id = post.id, | ||
content = post.content, | ||
viewCount = post.viewCount | ||
viewCount = post.getViewCount(), | ||
likeCount = post.getLikeCount(), | ||
) | ||
} |
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
32 changes: 32 additions & 0 deletions
32
sns_service/src/main/kotlin/joryu/sns_service/post/entity/PostView.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,32 @@ | ||
package joryu.sns_service.post.entity | ||
|
||
import jakarta.persistence.Entity | ||
import jakarta.persistence.FetchType | ||
import jakarta.persistence.GeneratedValue | ||
import jakarta.persistence.GenerationType | ||
import jakarta.persistence.Id | ||
import jakarta.persistence.Index | ||
import jakarta.persistence.JoinColumn | ||
import jakarta.persistence.ManyToOne | ||
import jakarta.persistence.Table | ||
import joryu.sns_service.common.entity.BaseEntity | ||
|
||
@Table( | ||
name = "post_view", indexes = [ | ||
Index(name = "idx_post_view", columnList = "post_id, ip", unique = true) | ||
] | ||
) | ||
@Entity | ||
class PostView( | ||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "post_id", nullable = false) | ||
val post: Post, | ||
|
||
val ip: String, | ||
) : BaseEntity() { | ||
constructor() : this(Post(), "") | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
val id: Long = 0 | ||
} |
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
7 changes: 6 additions & 1 deletion
7
sns_service/src/main/kotlin/joryu/sns_service/post/repository/PostLikeRepository.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,6 +1,11 @@ | ||
package joryu.sns_service.post.repository | ||
|
||
import joryu.sns_service.post.entity.Post | ||
import joryu.sns_service.post.entity.PostLike | ||
import joryu.sns_service.profile.entity.Profile | ||
import org.springframework.data.jpa.repository.JpaRepository | ||
|
||
interface PostLikeRepository : JpaRepository<PostLike, Long> | ||
interface PostLikeRepository : JpaRepository<PostLike, Long> { | ||
fun countByPost(post: Post): Long | ||
fun deleteByLikeMemberAndPost(likeMember: Profile, post: Post): Void | ||
} |
9 changes: 9 additions & 0 deletions
9
sns_service/src/main/kotlin/joryu/sns_service/post/repository/PostViewRepository.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,9 @@ | ||
package joryu.sns_service.post.repository | ||
|
||
import joryu.sns_service.post.entity.Post | ||
import joryu.sns_service.post.entity.PostView | ||
import org.springframework.data.jpa.repository.JpaRepository | ||
|
||
interface PostViewRepository : JpaRepository<PostView, Long> { | ||
fun existsByPostAndIp(post: Post, ip: String): Boolean | ||
} |
7 changes: 6 additions & 1 deletion
7
sns_service/src/main/kotlin/joryu/sns_service/post/repository/ShareRepository.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,6 +1,11 @@ | ||
package joryu.sns_service.post.repository | ||
|
||
import joryu.sns_service.post.entity.Post | ||
import joryu.sns_service.post.entity.Share | ||
import joryu.sns_service.profile.entity.Profile | ||
import org.springframework.data.jpa.repository.JpaRepository | ||
|
||
interface ShareRepository : JpaRepository<Share, Long> | ||
interface ShareRepository : JpaRepository<Share, Long> { | ||
fun findAllByShareMember(shareMember: Profile): List<Share> | ||
fun deleteByShareMemberAndPost(shareMember: Profile, post: Post): Void | ||
} |
Oops, something went wrong.