Skip to content

Commit

Permalink
Merge pull request #136 from SW13-Monstera/dev
Browse files Browse the repository at this point in the history
Release
  • Loading branch information
kshired authored Nov 25, 2023
2 parents f61e5db + 6662057 commit 5f0fb34
Show file tree
Hide file tree
Showing 26 changed files with 258 additions and 133 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.csbroker.apiserver.common.enums

enum class LikeType {
POST, COMMENT
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ class AuthController(

@GetMapping("/info")
fun getUserInfo(@LoginUser loginUser: User): ApiResponse<UserInfoResponseDto> {
val userInfo = authService.getUserInfo(loginUser.username)
return ApiResponse.success(UserInfoResponseDto(userInfo))
return ApiResponse.success(UserInfoResponseDto(loginUser))
}

@PostMapping("/login")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class CommentController(
@LoginUser user: User,
@PathVariable id: Long,
): ApiResponse<Unit> {
commentService.deleteById(id, user.username)
commentService.deleteById(id, user)
return ApiResponse.success()
}

Expand All @@ -32,8 +32,17 @@ class CommentController(
val postId = commentService.create(
commentCreateRequestDto.postId,
commentCreateRequestDto.content,
loginUser.username,
loginUser,
)
return ApiResponse.success(postId)
}

@PostMapping("/api/v1/comments/{id}/like")
fun likeComment(
@LoginUser loginUser: User,
@PathVariable("id") id: Long,
): ApiResponse<Unit> {
commentService.like(id, loginUser)
return ApiResponse.success()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class PostController(
val postId = postService.create(
postCreateRequestDto.problemId,
postCreateRequestDto.content,
loginUser.username,
loginUser,
)
return ApiResponse.success(postId)
}
Expand All @@ -36,24 +36,24 @@ class PostController(
@LoginUser loginUser: User,
@PathVariable("id") id: Long,
): ApiResponse<Unit> {
postService.deleteById(id, loginUser.username)
postService.deleteById(id, loginUser)
return ApiResponse.success()
}

@GetMapping("/api/v1/problems/{problemId}/posts")
fun findAllByProblemId(
@PathVariable("problemId") id: Long,
): ApiResponse<List<PostResponseDto>> {
val email = getEmailFromSecurityContextHolder()
return ApiResponse.success(postService.findByProblemId(id, email))
val nullableEmail = getEmailFromSecurityContextHolder()
return ApiResponse.success(postService.findByProblemId(id, nullableEmail))
}

@PostMapping("/api/v1/posts/{id}/like")
fun likePost(
@LoginUser loginUser: User,
@PathVariable("id") id: Long,
): ApiResponse<Unit> {
postService.like(id, loginUser.username)
postService.like(id, loginUser)
return ApiResponse.success()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.csbroker.apiserver.controller.v1.post.response

import io.csbroker.apiserver.model.Comment
import java.time.LocalDateTime

data class CommentResponseDto(
val id: Long,
val content: String,
val username: String,
val likeCount: Long,
val isLiked: Boolean,
val createdAt: LocalDateTime,
) {
constructor(comment: Comment, likeCount: Long, isLiked: Boolean) : this(
id = comment.id,
content = comment.content,
username = comment.user.username,
likeCount = likeCount,
isLiked = isLiked,
createdAt = comment.createdAt!!,
)
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
package io.csbroker.apiserver.controller.v1.post.response

import io.csbroker.apiserver.model.Comment
import io.csbroker.apiserver.model.Post
import java.time.LocalDateTime

data class PostResponseDto(
val id: Long,
val content: String,
val username: String,
val likeCount: Long,
val likeCount: Int,
val isLiked: Boolean,
val comments: List<CommentResponseDto>,
) {
constructor(post: Post, likeCount: Long, isLiked: Boolean, comments: List<CommentResponseDto>) : this(
constructor(post: Post, likeCount: Int, isLiked: Boolean, comments: List<CommentResponseDto>) : this(
id = post.id,
content = post.content,
username = post.user.username,
Expand All @@ -21,17 +19,3 @@ data class PostResponseDto(
comments = comments,
)
}

data class CommentResponseDto(
val id: Long,
val content: String,
val username: String,
val createdAt: LocalDateTime,
) {
constructor(comment: Comment) : this(
id = comment.id,
content = comment.content,
username = comment.user.username,
createdAt = comment.createdAt!!,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,9 @@ class LongProblemController(
@PathVariable("id") problemId: Long,
@RequestBody answerDto: LongProblemAnswerDto,
): ApiResponse<SubmitLongProblemResponseDto> {
val submitRequestDto = SubmitLongProblemDto(loginUser.email, problemId, answerDto.answer)
val submitRequestDto = SubmitLongProblemDto(loginUser, problemId, answerDto.answer)
val submitResponseDto = longProblemService.submitProblem(submitRequestDto)
postService.create(
problemId = submitRequestDto.problemId,
content = answerDto.answer,
email = loginUser.email,
)
postService.create(submitRequestDto.problemId, answerDto.answer, loginUser) // Todo : LongProblemService 내부로 보내기
return ApiResponse.success(submitResponseDto)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class ProblemControllerV2(
@LoginUser loginUser: User,
@PathVariable("id") id: Long,
): ApiResponse<Unit> {
commonProblemService.likeProblem(loginUser.username, id)
commonProblemService.likeProblem(loginUser, id)
return ApiResponse.success()
}

Expand All @@ -44,7 +44,7 @@ class ProblemControllerV2(
@LoginUser loginUser: User,
@PathVariable("id") id: Long,
): ApiResponse<Unit> {
commonProblemService.bookmarkProblem(loginUser.username, id)
commonProblemService.bookmarkProblem(loginUser, id)
return ApiResponse.success()
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package io.csbroker.apiserver.controller.v2.problem.request

import io.csbroker.apiserver.model.User

data class SubmitLongProblemDto(
val email: String,
val user: User,
val problemId: Long,
val answer: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.csbroker.apiserver.dto.user

import com.fasterxml.jackson.annotation.JsonInclude
import io.csbroker.apiserver.common.enums.Role
import io.csbroker.apiserver.model.User
import java.util.UUID

@JsonInclude(JsonInclude.Include.NON_NULL)
Expand All @@ -19,4 +20,12 @@ data class UserInfoResponseDto(
userInfoDto.role,
userInfoDto.accessToken,
)

constructor(user: User, accessToken: String? = null) : this(
user.id!!,
user.username,
user.email,
user.role,
accessToken,
)
}
35 changes: 35 additions & 0 deletions src/main/kotlin/io/csbroker/apiserver/model/Like.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.csbroker.apiserver.model

import io.csbroker.apiserver.common.enums.LikeType
import javax.persistence.Column
import javax.persistence.Entity
import javax.persistence.EnumType
import javax.persistence.Enumerated
import javax.persistence.FetchType
import javax.persistence.GeneratedValue
import javax.persistence.GenerationType
import javax.persistence.Id
import javax.persistence.JoinColumn
import javax.persistence.ManyToOne
import javax.persistence.Table

@Entity
@Table(name = "like")
class Like(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "like_id")
val id: Long = 0,

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

@Enumerated(EnumType.STRING)
@Column(name = "like_type")
val type: LikeType,

@Column(name = "target_id")
val targetId: Long,

) : BaseEntity()
5 changes: 5 additions & 0 deletions src/main/kotlin/io/csbroker/apiserver/model/Post.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import javax.persistence.GenerationType
import javax.persistence.Id
import javax.persistence.JoinColumn
import javax.persistence.ManyToOne
import javax.persistence.OneToMany
import javax.persistence.Table

@Entity
Expand All @@ -28,4 +29,8 @@ class Post(

@Column(name = "content", columnDefinition = "VARCHAR(300)")
val content: String,

@OneToMany(mappedBy = "post", fetch = FetchType.LAZY)
val comments: List<Comment> = listOf(),

) : BaseEntity()
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.csbroker.apiserver.repository.post

import io.csbroker.apiserver.common.enums.LikeType
import io.csbroker.apiserver.model.Like
import io.csbroker.apiserver.model.User
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query

interface LikeRepository : JpaRepository<Like, Long> {

@Query(
"""
select l from Like l
where l.type = :type and l.targetId in :targetIds
""",
)
fun findAllByTargetIdIn(type: LikeType, targetIds: List<Long>): List<Like>

@Query(
"""
select l from Like l
where l.type = :type and l.targetId = :targetId and l.user = :user
""",
)
fun findByTargetIdAndUser(type: LikeType, targetId: Long, user: User): Like?
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,20 @@ interface PostRepository : JpaRepository<Post, Long> {
@Query(
"""
select p from Post p
join fetch p.problem
join fetch p.user
join fetch p.comments
where p.problem = :problem
""",
)
fun findAllByProblem(problem: Problem): List<Post>

@Query(
"""
select p from Post p
join fetch p.user
join fetch p.comments
where p.id = :id
""",
)
fun findByIdOrNull(id: Long): Post?
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package io.csbroker.apiserver.service.post

import io.csbroker.apiserver.model.User

interface CommentService {
fun create(postId: Long, content: String, email: String): Long
fun deleteById(id: Long, email: String)
fun create(postId: Long, content: String, user: User): Long
fun deleteById(id: Long, user: User)
fun like(id: Long, user: User)
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package io.csbroker.apiserver.service.post

import io.csbroker.apiserver.common.enums.ErrorCode
import io.csbroker.apiserver.common.enums.LikeType
import io.csbroker.apiserver.common.exception.UnAuthorizedException
import io.csbroker.apiserver.model.Comment
import io.csbroker.apiserver.model.Like
import io.csbroker.apiserver.model.User
import io.csbroker.apiserver.repository.post.CommentRepository
import io.csbroker.apiserver.repository.post.LikeRepository
import io.csbroker.apiserver.repository.post.PostRepository
import io.csbroker.apiserver.repository.user.UserRepository
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
Expand All @@ -16,11 +19,10 @@ import javax.persistence.EntityNotFoundException
class CommentServiceImpl(
private val commentRepository: CommentRepository,
private val postRepository: PostRepository,
private val userRepository: UserRepository,
private val likeRepository: LikeRepository,
) : CommentService {
@Transactional
override fun create(postId: Long, content: String, email: String): Long {
val user = userRepository.findByEmail(email) ?: throw EntityNotFoundException("$email 을 가진 유저는 존재하지 않습니다.")
override fun create(postId: Long, content: String, user: User): Long {
val post = postRepository.findByIdOrNull(postId) ?: throw EntityNotFoundException(
"${postId}번 답변은 존재하지 않는 답변입니다",
)
Expand All @@ -29,13 +31,21 @@ class CommentServiceImpl(
}

@Transactional
override fun deleteById(id: Long, email: String) {
val user = userRepository.findByEmail(email) ?: throw EntityNotFoundException("$email 을 가진 유저는 존재하지 않습니다.")
override fun deleteById(id: Long, user: User) {
val comment = commentRepository.findByIdOrNull(id)
?: throw EntityNotFoundException("${id}번 답변은 존재하지 않는 답변입니다")
if (comment.user != user) {
throw UnAuthorizedException(ErrorCode.FORBIDDEN, "해당 답변을 삭제할 권한이 없습니다")
}
commentRepository.delete(comment)
}

override fun like(id: Long, user: User) {
val comment = commentRepository.findByIdOrNull(id)
?: throw EntityNotFoundException("${id}번 답변은 존재하지 않는 답변입니다")

likeRepository.findByTargetIdAndUser(LikeType.COMMENT, comment.id, user)
?.let { likeRepository.delete(it) }
?: likeRepository.save(Like(user = user, type = LikeType.COMMENT, targetId = comment.id))
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package io.csbroker.apiserver.service.post

import io.csbroker.apiserver.controller.v1.post.response.PostResponseDto
import io.csbroker.apiserver.model.User

interface PostService {
fun findByProblemId(problemId: Long, email: String?): List<PostResponseDto>
fun create(problemId: Long, content: String, email: String): Long
fun like(id: Long, email: String)
fun deleteById(id: Long, email: String)
fun findByProblemId(problemId: Long, emailIfLogin: String?): List<PostResponseDto>
fun findByPostId(postId: Long, emailIfLogin: String?): PostResponseDto
fun create(problemId: Long, content: String, user: User): Long
fun like(id: Long, user: User)
fun deleteById(id: Long, user: User)
}
Loading

0 comments on commit 5f0fb34

Please sign in to comment.