Skip to content

Commit 8c248a2

Browse files
committed
✨ feat(certificate) : 챌린지 얼리기 기능 구현
1 parent 875f574 commit 8c248a2

File tree

9 files changed

+375
-85
lines changed

9 files changed

+375
-85
lines changed

server/src/main/kotlin/com/app/server/challenge_certification/application/service/CertificationFacadeService.kt

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class CertificationFacadeService(
2121
private val certificationService: CertificationService,
2222
private val certificationInfraService: CertificationInfraService,
2323
private val userChallengeService: UserChallengeService
24-
) : CertificationUseCase, UsingIceUseCase {
24+
) : CertificationUseCase {
2525

2626
override fun certificateChallengeWithDate(
2727
certificationRequestDto: CertificationRequestDto,
@@ -51,12 +51,4 @@ class CertificationFacadeService(
5151
return certificationService.processAfterCertificateSuccess(userChallenge, certificationFacadeToServiceDto)
5252
}
5353

54-
override fun execute(
55-
userChallengeIceRequestDto: UserChallengeIceRequestDto,
56-
certificationDate: LocalDate
57-
): UserChallenge {
58-
59-
return certificationService.processAfterUserChallengeIce(userChallengeIceRequestDto, certificationDate)
60-
}
61-
6254
}

server/src/main/kotlin/com/app/server/challenge_certification/application/service/CertificationService.kt

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package com.app.server.challenge_certification.application.service
22

33
import com.app.server.challenge_certification.application.dto.CertificationFacadeToServiceDto
44
import com.app.server.challenge_certification.application.service.constant.EConsecutiveState
5-
import com.app.server.challenge_certification.ui.dto.CertificationRequestDto
65
import com.app.server.challenge_certification.ui.dto.UserChallengeIceRequestDto
6+
import com.app.server.challenge_certification.usecase.UsingIceUseCase
77
import com.app.server.common.exception.InternalServerErrorException
88
import com.app.server.user_challenge.application.service.UserChallengeService
99
import com.app.server.user_challenge.domain.enums.EUserChallengeCertificationStatus
@@ -15,8 +15,8 @@ import java.time.LocalDate
1515

1616
@Service
1717
class CertificationService(
18-
private val userChallengeService: UserChallengeService
19-
) {
18+
private val userChallengeService: UserChallengeService,
19+
) : UsingIceUseCase {
2020

2121
fun processAfterCertificateSuccess(
2222
userChallenge: UserChallenge, certificationDto: CertificationFacadeToServiceDto
@@ -39,6 +39,22 @@ class CertificationService(
3939
return userChallenge
4040
}
4141

42+
override fun processAfterCertificateIce(
43+
iceDto: UserChallengeIceRequestDto,
44+
certificationDate: LocalDate
45+
): UserChallenge {
46+
val userChallenge = userChallengeService.findById(userChallengeId = iceDto.userChallengeId)
47+
48+
// 얼리기 가능 여부 판단
49+
userChallenge.validateCanIceAndUse()
50+
// 날짜 인증 상태 Ice로 변경
51+
userChallenge.updateCertificationStateIsIce(certificationDate)
52+
// 연속 참여 일수 증가
53+
readyForValidateCanIncreaseConsecutiveParticipantDays(userChallenge, certificationDate)
54+
55+
return userChallenge
56+
}
57+
4258
private fun readyForValidateCanIncreaseConsecutiveParticipantDays(
4359
userChallenge: UserChallenge,
4460
certificationDate: LocalDate
@@ -119,18 +135,4 @@ class CertificationService(
119135
}
120136
}
121137

122-
fun processAfterUserChallengeIce(
123-
userChallengeIceRequestDto: UserChallengeIceRequestDto, certificationDate: LocalDate
124-
): UserChallenge {
125-
val userChallenge = userChallengeService.findById(userChallengeId = userChallengeIceRequestDto.userChallengeId)
126-
127-
// 얼리기 가능 여부 판단
128-
userChallenge.validateCanIceAndUse()
129-
// 날짜 인증 상태 Ice로 변경
130-
userChallenge.updateCertificationStateIsIce(certificationDate)
131-
// 연속 참여 일수 증가
132-
readyForValidateCanIncreaseConsecutiveParticipantDays(userChallenge, certificationDate)
133-
134-
return userChallenge
135-
}
136138
}
Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
11
package com.app.server.challenge_certification.ui
22

33
import com.app.server.challenge_certification.ui.dto.CertificationRequestDto
4+
import com.app.server.challenge_certification.ui.dto.UserChallengeIceRequestDto
45
import com.app.server.challenge_certification.usecase.CertificationUseCase
6+
import com.app.server.challenge_certification.usecase.UsingIceUseCase
57
import com.app.server.common.enums.CommonResultCode
68
import com.app.server.common.enums.ResultCode
79
import com.app.server.common.response.ApiResponse
10+
import org.springframework.web.bind.annotation.PathVariable
811
import org.springframework.web.bind.annotation.PostMapping
912
import org.springframework.web.bind.annotation.RequestBody
1013
import org.springframework.web.bind.annotation.RequestMapping
1114
import org.springframework.web.bind.annotation.RestController
1215
import java.time.LocalDate
1316

1417
@RestController
15-
@RequestMapping("/api/v1")
16-
class CertificationController (
17-
private val certificationUseCase: CertificationUseCase
18-
){
18+
@RequestMapping("/api/v1/challenge/user/{userChallengeId}")
19+
class CertificationController(
20+
private val certificationUseCase: CertificationUseCase,
21+
private val usingIceUseCase: UsingIceUseCase
22+
) {
1923

20-
@PostMapping("/challenge/user/{userChallengeId}/certification")
24+
@PostMapping("/certification")
2125
fun certificateDailyUserChallenge(
2226
@RequestBody certificationRequestDto: CertificationRequestDto
23-
) : ApiResponse<ResultCode> {
27+
): ApiResponse<ResultCode> {
2428
certificationUseCase.certificateChallengeWithDate(
2529
certificationRequestDto = certificationRequestDto,
2630
certificationDate = LocalDate.now()
@@ -29,4 +33,19 @@ class CertificationController (
2933
return ApiResponse.success(CommonResultCode.SUCCESS)
3034
}
3135

36+
@PostMapping("/ice")
37+
fun iceDailyUserChallenge(
38+
@PathVariable userChallengeId: Long
39+
): ApiResponse<ResultCode> {
40+
val iceRequest = UserChallengeIceRequestDto(
41+
userChallengeId = userChallengeId
42+
)
43+
usingIceUseCase.processAfterCertificateIce(
44+
iceRequestDto = iceRequest,
45+
certificationDate = LocalDate.now()
46+
)
47+
48+
return ApiResponse.success(CommonResultCode.SUCCESS)
49+
}
50+
3251
}

server/src/main/kotlin/com/app/server/challenge_certification/usecase/UsingIceUseCase.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ import java.time.LocalDate
66

77
interface UsingIceUseCase {
88

9-
fun execute(iceRequestDto: UserChallengeIceRequestDto, certificationDate: LocalDate) : UserChallenge
9+
fun processAfterCertificateIce(iceRequestDto: UserChallengeIceRequestDto, certificationDate: LocalDate) : UserChallenge
1010
}

server/src/main/kotlin/com/app/server/user_challenge/domain/model/UserChallenge.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ class UserChallenge(
125125

126126
fun validateCanIceAndUse(): Boolean {
127127
if (this.iceCount > 0) {
128-
this.iceCount.minus(1)
128+
this.iceCount -= 1
129129
return true
130130
}
131131
throw BadRequestException(UserChallengeException.CANNOT_USE_ICE)

server/src/test/kotlin/com/app/server/IntegrationTestContainer.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ abstract class IntegrationTestContainer {
4242
protected val participationDays: Int = 7
4343
protected val participantsStartDate: LocalDate = LocalDate.now()
4444
protected val testEmail = "testEmail@email.com"
45+
protected val imageUrl = "testImageUrl"
46+
protected val challengeTitle = "유휴 전원 / 대기 전력 OFF"
47+
protected val challengeDescription = "사용하지 않는 전자기기의 전원을 끄거나, 플러그를 뽑는 챌린지입니다. TV, 컴퓨터 등의 대기 전력을 차단함으로써 불필요한 전기 낭비를 줄이고 온실가스 배출을 저감할 수 있습니다."
4548
protected val accessTokenValidationTime = 360000L
4649
protected val token : String by lazy {
4750
jwtUtil.createToken(userId, testEmail, "USER", accessTokenValidationTime)

server/src/test/kotlin/com/app/server/challenge_certification/application/usecase/CertificationUseCaseTest.kt

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,30 +49,25 @@ class CertificationUseCaseTest : IntegrationTestContainer() {
4949
@MockitoBean
5050
private lateinit var certificationInfraService: CertificationInfraService
5151

52-
val imageUrl = "testImageUrl"
53-
val challengeTitle = "유휴 전원 / 대기 전력 OFF"
54-
val challengeDescription = "사용하지 않는 전자기기의 전원을 끄거나, 플러그를 뽑는 챌린지입니다. TV, 컴퓨터 등의 대기 전력을 차단함으로써 불필요한 전기 낭비를 줄이고 온실가스 배출을 저감할 수 있습니다."
5552
var certificationRequestDto = CertificationRequestDto(
5653
userChallengeId = userChallengeId,
5754
imageUrl = imageUrl,
5855
)
59-
var pastCertificationRequestDto = CertificationRequestDto(
60-
userChallengeId = userChallengeId - 1,
61-
imageUrl = imageUrl
62-
)
56+
6357
var sendToCertificationServerRequestDto = SendToCertificationServerRequestDto(
6458
imageUrl = imageUrl,
6559
challengeId = challengeId,
6660
challengeName = challengeTitle,
6761
challengeDescription = challengeDescription
6862
)
63+
var savedUserChallenge: UserChallenge? = null
6964

7065
@BeforeEach
71-
fun setUpChallengeFixture() {
72-
val savedUserChallenge = makeUserChallengeAndHistory(participantsStartDate)
66+
fun setUp() {
67+
savedUserChallenge = makeUserChallengeAndHistory(participantsStartDate)
7368

7469
certificationRequestDto = CertificationRequestDto(
75-
userChallengeId = savedUserChallenge.id!!,
70+
userChallengeId = savedUserChallenge!!.id!!,
7671
imageUrl = imageUrl,
7772
)
7873
val challenge = challengeService.findById(challengeId)
@@ -193,15 +188,16 @@ class CertificationUseCaseTest : IntegrationTestContainer() {
193188
given(certificationInfraService.certificate(sendToCertificationServerRequestDto)).willReturn(
194189
EUserCertificatedResultCode.SUCCESS_CERTIFICATED
195190
)
196-
val pastUserChallenge : UserChallenge? = makeUserChallengeAndHistory(participantsStartDate.minusDays(1))
191+
val pastUserChallengeNowConsecutiveParticipationDayCount = savedUserChallenge!!.nowConsecutiveParticipationDayCount
192+
val pastUserChallengeMaxConsecutiveParticipationDayCount = savedUserChallenge!!.maxConsecutiveParticipationDayCount
197193

198194
// when
199195
val todayUserChallenge : UserChallenge =
200196
certificationUseCase.certificateChallengeWithDate(certificationRequestDto, participantsStartDate)
201197
// then
202-
assertThat(todayUserChallenge.nowConsecutiveParticipationDayCount).isGreaterThan(pastUserChallenge!!.nowConsecutiveParticipationDayCount)
203-
assertThat(todayUserChallenge.nowConsecutiveParticipationDayCount - pastUserChallenge.nowConsecutiveParticipationDayCount).isOne
204-
assertThat(todayUserChallenge.maxConsecutiveParticipationDayCount).isEqualTo(todayUserChallenge.nowConsecutiveParticipationDayCount)
198+
assertThat(todayUserChallenge.nowConsecutiveParticipationDayCount).isGreaterThan(pastUserChallengeNowConsecutiveParticipationDayCount)
199+
assertThat(todayUserChallenge.nowConsecutiveParticipationDayCount - pastUserChallengeNowConsecutiveParticipationDayCount).isOne
200+
assertThat(todayUserChallenge.maxConsecutiveParticipationDayCount).isEqualTo(pastUserChallengeMaxConsecutiveParticipationDayCount)
205201
}
206202

207203
@Test

0 commit comments

Comments
 (0)