Skip to content

Commit

Permalink
Merge pull request #81 from whatever-mentoring/feature/goal-proof-ser…
Browse files Browse the repository at this point in the history
…vice-test

Feature/goal proof service test
  • Loading branch information
mkSpace authored Jan 23, 2024
2 parents 54241df + 3f994c0 commit f4cc2e3
Show file tree
Hide file tree
Showing 4 changed files with 551 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,13 @@ class GoalProofApplicationService(
@Transactional
fun create(request: GoalProofCreateServiceRequest): GoalProofCreateUpdateResponse {
val goal = goalService.findById(request.goalId)
val user = userService.loadById(request.userId)

isGoalProofAlreadyExists(request.goalId)
isGoalProofAlreadyExists(goal.id)
validateGoalProofCreateTiming(goal)

val goalProof = goalProofService.create(
user = user,
goal = goal,
userId = request.userId,
goalId = goal.id,
url = URL(request.url),
comment = request.comment
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@ package com.whatever.raisedragon.domain.goalproof

import com.whatever.raisedragon.common.exception.BaseException
import com.whatever.raisedragon.domain.gifticon.URL
import com.whatever.raisedragon.domain.goal.Goal
import com.whatever.raisedragon.domain.goal.GoalRepository
import com.whatever.raisedragon.domain.goal.fromDto
import com.whatever.raisedragon.domain.user.User
import com.whatever.raisedragon.domain.user.UserRepository
import com.whatever.raisedragon.domain.user.fromDto
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
Expand All @@ -25,22 +21,35 @@ class GoalProofService(

@Transactional
fun create(
user: User,
goal: Goal,
userId: Long,
goalId: Long,
url: URL,
comment: Comment
): GoalProof {
val userEntity = userRepository.findById(userId).orElseThrow(notFoundExceptionSupplier)
val goalEntity = goalRepository.findById(goalId).orElseThrow(notFoundExceptionSupplier)
val goalProof = goalProofRepository.save(
GoalProofEntity(
userEntity = user.fromDto(),
goalEntity = goal.fromDto(user.fromDto()),
userEntity = userEntity,
goalEntity = goalEntity,
url = url,
comment = comment,
)
)
return goalProof.toDto()
}

fun findById(id: Long): GoalProof? {
return goalProofRepository.findById(id).orElseThrow(notFoundExceptionSupplier).toDto()
}

fun findAllByGoalIdAndUserId(goalId: Long, userId: Long): List<GoalProof> {
val goalEntity = goalRepository.findById(goalId).orElseThrow(notFoundExceptionSupplier)
val userEntity = userRepository.findById(userId).orElseThrow(notFoundExceptionSupplier)
return goalProofRepository.findAllByUserEntityAndGoalEntity(goalEntity = goalEntity, userEntity = userEntity)
.map { it.toDto() }
}

fun existsGoalIdAndDateTimeBetween(goalId: Long, targetDateTime: LocalDateTime): Boolean {
val todayStartDateTime = LocalDateTime.of(
targetDateTime.year,
Expand All @@ -65,25 +74,14 @@ class GoalProofService(
)
}

fun findById(goalProofId: Long): GoalProof? {
return goalProofRepository.findById(goalProofId).orElseThrow(notFoundExceptionSupplier).toDto()
}

fun countAllByGoalId(goalId: Long): Int {
val goalEntity = goalRepository.findById(goalId).orElseThrow(notFoundExceptionSupplier)
return goalProofRepository.countAllByGoalEntity(goalEntity)
}

fun findAllByGoalIdAndUserId(goalId: Long, userId: Long): List<GoalProof> {
val goalEntity = goalRepository.findById(goalId).orElseThrow(notFoundExceptionSupplier)
val userEntity = userRepository.findById(userId).orElseThrow(notFoundExceptionSupplier)
return goalProofRepository.findAllByUserEntityAndGoalEntity(goalEntity = goalEntity, userEntity = userEntity)
.map { it.toDto() }
}

@Transactional
fun update(goalProofId: Long, url: URL? = null, comment: Comment? = null): GoalProof {
val goalProof = goalProofRepository.findById(goalProofId).orElseThrow(notFoundExceptionSupplier)
fun update(id: Long, url: URL? = null, comment: Comment? = null): GoalProof {
val goalProof = goalProofRepository.findById(id).orElseThrow(notFoundExceptionSupplier)
url?.let { goalProof.url = it }
comment?.let { goalProof.comment = it }
return goalProof.toDto()
Expand Down
Loading

0 comments on commit f4cc2e3

Please sign in to comment.