Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write test codes of GoalProofApplicationService #94

Merged
merged 2 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.whatever.raisedragon.applicationservice.goalproof

import com.whatever.raisedragon.applicationservice.goalproof.dto.*
import com.whatever.raisedragon.applicationservice.goalproof.dto.GoalProofCreateServiceRequest
import com.whatever.raisedragon.applicationservice.goalproof.dto.GoalProofListRetrieveResponse
import com.whatever.raisedragon.applicationservice.goalproof.dto.GoalProofRetrieveResponse
import com.whatever.raisedragon.applicationservice.goalproof.dto.GoalProofUpdateServiceRequest
import com.whatever.raisedragon.common.exception.BaseException
import com.whatever.raisedragon.common.exception.ExceptionCode
import com.whatever.raisedragon.domain.gifticon.URL
Expand All @@ -22,7 +25,7 @@ class GoalProofApplicationService(
) {

@Transactional
fun create(request: GoalProofCreateServiceRequest): GoalProofCreateUpdateResponse {
fun create(request: GoalProofCreateServiceRequest): GoalProofRetrieveResponse {
val goal = goalService.findById(request.goalId)

isGoalProofAlreadyExists(goal.id)
Expand All @@ -31,11 +34,11 @@ class GoalProofApplicationService(
val goalProof = goalProofService.create(
userId = request.userId,
goalId = goal.id,
url = URL(request.url),
url = request.url,
comment = request.comment
)
goalService.increaseThreshold(goal.id)
return GoalProofCreateUpdateResponse(GoalProofRetrieveResponse.of(goalProof))
return GoalProofRetrieveResponse.of(goalProof)
}

private fun isGoalProofAlreadyExists(goalId: Long) {
Expand Down Expand Up @@ -87,15 +90,14 @@ class GoalProofApplicationService(
.also { it.validateOwnerId(request.userId) }
.also { it.validateEndDate() }
return GoalProofRetrieveResponse.of(
goalProofService.update(request.goalProofId, request.url?.let { URL(it) }, request.comment)
goalProofService.update(request.goalProofId, request.url, request.comment)
)
}

private fun validateGoalProofCreateTiming(goal: Goal) {
val now = LocalDateTime.now()
val daysBetween = ChronoUnit.DAYS.between(goal.startDate, now) + 1


if (1 > daysBetween || daysBetween > 7) {
throw BaseException.of(
exceptionCode = ExceptionCode.E400_BAD_REQUEST,
Expand All @@ -108,7 +110,7 @@ class GoalProofApplicationService(
return goalProofService.findById(goalProofId) ?: throw BaseException.of(ExceptionCode.E404_NOT_FOUND)
}

private fun validateUpdatable(url: String?, comment: Comment?) {
private fun validateUpdatable(url: URL?, comment: Comment?) {
if (url == null && comment == null) {
throw BaseException.of(ExceptionCode.E400_BAD_REQUEST)
}
Expand All @@ -121,7 +123,7 @@ class GoalProofApplicationService(
}

private fun GoalProof.validateEndDate() {
if (goalService.findById(goalId).endDate > LocalDateTime.now()) {
if (goalService.findById(goalId).endDate < LocalDateTime.now()) {
throw BaseException.of(ExceptionCode.E400_BAD_REQUEST, "이미 끝난 내기입니다")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,6 @@ package com.whatever.raisedragon.applicationservice.goalproof.dto
import com.whatever.raisedragon.domain.goalproof.GoalProof
import io.swagger.v3.oas.annotations.media.Schema


@Schema(description = "[Response] 인증내역 생성/수정")
data class GoalProofCreateUpdateResponse(
@Schema(description = "GoalProof")
val goalProofRetrieveResponse: GoalProofRetrieveResponse
)

@Schema(description = "[Response] 단건 다짐 인증 조회")
data class GoalProofRetrieveResponse(

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package com.whatever.raisedragon.applicationservice.goalproof.dto

import com.whatever.raisedragon.domain.gifticon.URL
import com.whatever.raisedragon.domain.goalproof.Comment

data class GoalProofCreateServiceRequest(
val userId: Long,
val goalId: Long,
val url: String,
val url: URL,
val comment: Comment
)

data class GoalProofUpdateServiceRequest(
val goalProofId: Long,
val userId: Long,
val url: String? = null,
val url: URL? = null,
val comment: Comment? = null
)
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.whatever.raisedragon.controller.goalproof

import com.whatever.raisedragon.applicationservice.goalproof.GoalProofApplicationService
import com.whatever.raisedragon.applicationservice.goalproof.dto.GoalProofCreateUpdateResponse
import com.whatever.raisedragon.applicationservice.goalproof.dto.GoalProofListRetrieveResponse
import com.whatever.raisedragon.applicationservice.goalproof.dto.GoalProofRetrieveResponse
import com.whatever.raisedragon.common.Response
Expand All @@ -28,7 +27,7 @@ class GoalProofController(
fun create(
@Valid @RequestBody goalProofCreateRequest: GoalProofCreateRequest,
@GetAuth userInfo: UserInfo
): Response<GoalProofCreateUpdateResponse> {
): Response<GoalProofRetrieveResponse> {
return Response.success(
goalProofApplicationService.create(goalProofCreateRequest.toServiceRequest(userInfo.id))
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.whatever.raisedragon.controller.goalproof

import com.whatever.raisedragon.applicationservice.goalproof.dto.GoalProofCreateServiceRequest
import com.whatever.raisedragon.applicationservice.goalproof.dto.GoalProofUpdateServiceRequest
import com.whatever.raisedragon.domain.gifticon.URL
import com.whatever.raisedragon.domain.goalproof.Comment
import io.swagger.v3.oas.annotations.media.Schema

Expand All @@ -11,7 +12,7 @@ data class GoalProofCreateRequest(
val goalId: Long,

@Schema(description = "다짐 인증에 사용한 이미지 url")
val url: String?,
val url: String,

@Schema(description = "다짐 인증에 대한 부연설명")
val comment: String
Expand All @@ -22,7 +23,7 @@ fun GoalProofCreateRequest.toServiceRequest(
): GoalProofCreateServiceRequest = GoalProofCreateServiceRequest(
userId = userId,
goalId = goalId,
url = url ?: "",
url = URL(url),
comment = Comment(comment)
)

Expand All @@ -41,6 +42,6 @@ fun GoalProofUpdateRequest.toServiceRequest(
): GoalProofUpdateServiceRequest = GoalProofUpdateServiceRequest(
userId = userId,
goalProofId = goalProofId,
url = url,
url = url?.let { URL(it) },
comment = comment?.let { Comment(it) }
)
Loading
Loading