Skip to content

Commit

Permalink
feat: 질문세트 좋아요/싫어요 api
Browse files Browse the repository at this point in the history
  • Loading branch information
rlaisqls authored Nov 2, 2023
2 parents 194bc54 + 2103e88 commit 65a661a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ class QuestionController(
return questionService.dislikeAnswer(answerId)
}

@PostMapping("/set/{question-set-id}/like")
fun likeQuestionSet(@PathVariable("question-set-id") questionSetId: Long): LikeResponse {
return questionService.likeQuestionSet(questionSetId)
}

@PostMapping("/set/{question-set-id}/dislike")
fun dislikeQuestionSet(@PathVariable("question-set-id") questionSetId: Long): DislikeResponse {
return questionService.dislikeQuestionSet(questionSetId)
}

@PostMapping("/set")
fun registerQuestionSets(@RequestBody request: QuestionSetsRequest): RegisterQuestionSetsResponse{
return questionService.registerQuestionSet(request)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ package kr.hs.dsm.inq.domain.question.service

import kr.hs.dsm.inq.common.util.SecurityUtil
import kr.hs.dsm.inq.common.util.defaultPage
import kr.hs.dsm.inq.domain.question.exception.AlreadyDislikedPostException
import kr.hs.dsm.inq.domain.question.exception.AlreadyLikedPostException
import kr.hs.dsm.inq.domain.question.exception.AnswerNotFoundException
import kr.hs.dsm.inq.domain.question.exception.QuestionNotFoundException
import kr.hs.dsm.inq.domain.question.exception.QuestionSetNotFoundException
import kr.hs.dsm.inq.domain.question.exception.*
import kr.hs.dsm.inq.domain.question.persistence.*
import kr.hs.dsm.inq.domain.question.persistence.dto.AnswersDto
import kr.hs.dsm.inq.domain.question.persistence.dto.CategoriesDto
import kr.hs.dsm.inq.domain.question.persistence.repository.*
import kr.hs.dsm.inq.domain.question.presentation.dto.*
import kr.hs.dsm.inq.domain.user.persistence.User
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
Expand Down Expand Up @@ -219,66 +225,88 @@ class QuestionService(
}

fun likeAnswer(answerId: Long): LikeResponse {

val user = SecurityUtil.getCurrentUser()
val answer = answersRepository.findByIdOrNull(answerId) ?: throw AnswerNotFoundException
return toggleLike(answer.post, user)
}

val likeId = LikeId(answer.post.id, user.id)
val like = likeRepository.findByIdOrNull(likeId)
fun likeQuestionSet(questionSetId: Long): LikeResponse {
val user = SecurityUtil.getCurrentUser()
val questionSet = questionSetsRepository.findByIdOrNull(questionSetId) ?: throw QuestionSetNotFoundException
return toggleLike(questionSet.post, user)
}

if (like == null) {
private fun toggleLike(
post: Post,
user: User
): LikeResponse {
val likeId = LikeId(post.id, user.id)
val like = likeRepository.findByIdOrNull(likeId)
return if (like == null) {
postRepository.save(
answer.post.apply { addLikeCount() }
post.apply { addLikeCount() }
)
likeRepository.save(
Like(
id = likeId,
post = answer.post,
post = post,
user = user,
isLiked = true
)
)
return LikeResponse(isLiked = true)
LikeResponse(isLiked = true)
} else if (!like.isLiked) {
throw AlreadyDislikedPostException
} else {
postRepository.save(
answer.post.apply { reduceLikeCount() }
post.apply { reduceLikeCount() }
)
likeRepository.deleteById(likeId)
return LikeResponse(isLiked = false)
LikeResponse(isLiked = false)
}
}

fun dislikeAnswer(answerId: Long): DislikeResponse {

val user = SecurityUtil.getCurrentUser()
val answer = answersRepository.findByIdOrNull(answerId) ?: throw AnswerNotFoundException
return toggleDislike(answer.post, user)
}

val likeId = LikeId(answer.post.id, user.id)
val like = likeRepository.findByIdOrNull(likeId)
fun dislikeQuestionSet(questionSetId: Long): DislikeResponse {
val user = SecurityUtil.getCurrentUser()
val questionSet = questionSetsRepository.findByIdOrNull(questionSetId) ?: throw QuestionSetNotFoundException
return toggleDislike(questionSet.post, user)
}

if (like == null) {
private fun toggleDislike(
post: Post,
user: User
): DislikeResponse {
val likeId = LikeId(post.id, user.id)
val like = likeRepository.findByIdOrNull(likeId)
return if (like == null) {
// doDislike
postRepository.save(
answer.post.apply { addDislikeCount() }
post.apply { addDislikeCount() }
)
likeRepository.save(
Like(
id = likeId,
post = answer.post,
post = post,
user = user,
isLiked = true
)
)
return DislikeResponse(isDisliked = true)
DislikeResponse(isDisliked = true)
} else if (like.isLiked) {
throw AlreadyLikedPostException
} else {
// cancelDislike
postRepository.save(
answer.post.apply { reduceDislikeCount() }
post.apply { reduceDislikeCount() }
)
likeRepository.deleteById(likeId)
return DislikeResponse(isDisliked = false)
DislikeResponse(isDisliked = false)
}
}

Expand Down Expand Up @@ -383,7 +411,7 @@ class QuestionService(
fun answerQuestionSet(questionSetId: Long){
val user = SecurityUtil.getCurrentUser()

val questionSet = questionSetsRepository.findByIdOrNull(questionSetId)?: throw QuestionNotFoundException
val questionSet = questionSetsRepository.findByIdOrNull(questionSetId)?: throw QuestionSetNotFoundException

questionSolvingHistoryRepository.save(
QuestionSolvingHistory(
Expand Down
8 changes: 4 additions & 4 deletions src/main/kotlin/kr/hs/dsm/inq/global/error/ErrorCode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ enum class DomainErrorCode(

USER_NOT_FOUND(ErrorStatus.NOT_FOUND, "User Not Found", 1),
QUESTION_NOT_FOUND(ErrorStatus.NOT_FOUND, "Question Not Found", 2),
QUESTION_SET_NOT_FOUND(ErrorStatus.NOT_FOUND, "Question Set Not Found", 3),
ANSWER_NOT_FOUND(ErrorStatus.NOT_FOUND, "Answer Not Found", 4),
TAG_NOT_FOUND(ErrorStatus.NOT_FOUND, "Tag Not Found", 5),
ATTENDANCE_NOT_FOUND(ErrorStatus.NOT_FOUND, "Attendance Not Found", 6),
ANSWER_NOT_FOUND(ErrorStatus.NOT_FOUND, "Answer Not Found", 3),
TAG_NOT_FOUND(ErrorStatus.NOT_FOUND, "Tag Not Found", 4),
ATTENDANCE_NOT_FOUND(ErrorStatus.NOT_FOUND, "Attendance Not Found", 5),
QUESTION_SET_NOT_FOUND(ErrorStatus.NOT_FOUND, "Question Not Found", 6),

ALREADY_LIKED_POST(ErrorStatus.CONFLICT, "Already liked post", 1),
ALREADY_DISLIKED_POST(ErrorStatus.CONFLICT, "Already disliked post", 2),
Expand Down

0 comments on commit 65a661a

Please sign in to comment.