Skip to content

Commit

Permalink
Merge pull request #73 from mash-up-kr/feature/vote-request-finish
Browse files Browse the repository at this point in the history
feat: Vote 마감일 설정 시, 패스워드 검증로직 추가
  • Loading branch information
KimDoubleB authored Jun 29, 2024
2 parents 715cc59 + 1e4752c commit 8942a7f
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,18 @@ data class Room(
val address: String,
val meetDay: LocalDate,
val thumbnailLink: String,
val password: Short,
val password: Password,
val voteDeadline: LocalDateTime?,
val roomId: UUID,
) {
fun isVoteUnavailable(): Boolean {
return this.voteDeadline == null || this.voteDeadline.isBefore(LocalDateTime.now())
}

fun isPasswordValid(password: Password): Boolean {
return this.password == password
}
}

@JvmInline
value class Password(val value: String)
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import com.piikii.application.port.input.room.dto.response.RoomGetResponseForm
import com.piikii.application.port.input.room.dto.response.RoomSaveResponseForm
import com.piikii.application.port.output.persistence.RoomCommandPort
import com.piikii.application.port.output.persistence.RoomQueryPort
import com.piikii.common.exception.ExceptionCode
import com.piikii.common.exception.PiikiiException
import org.springframework.stereotype.Service
import java.time.LocalDateTime
import java.util.UUID
Expand Down Expand Up @@ -36,9 +38,21 @@ class RoomService(

override fun changeVoteDeadline(
roomId: UUID,
password: Password,
voteDeadline: LocalDateTime,
) {
val room = roomQueryPort.retrieve(roomId).copy(voteDeadline = voteDeadline)
roomCommandPort.update(room)
roomQueryPort.retrieve(roomId).let { room ->
verifyPassword(room, password)
roomCommandPort.update(room.copy(voteDeadline = voteDeadline))
}
}

private fun verifyPassword(
room: Room,
password: Password,
) {
require(room.isPasswordValid(password)) {
throw PiikiiException(ExceptionCode.ROOM_PASSWORD_INVALID)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.piikii.application.port.input.room

import com.piikii.application.domain.room.Password
import com.piikii.application.port.input.room.dto.request.RoomSaveRequestForm
import com.piikii.application.port.input.room.dto.request.RoomUpdateRequestForm
import com.piikii.application.port.input.room.dto.response.RoomGetResponseForm
Expand All @@ -18,6 +19,7 @@ interface RoomUseCase {

fun changeVoteDeadline(
roomId: UUID,
password: Password,
voteDeadline: LocalDateTime,
)
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.piikii.application.port.input.room.dto.request

import com.fasterxml.jackson.annotation.JsonInclude
import com.piikii.application.domain.room.Password
import com.piikii.application.domain.room.Room
import io.swagger.v3.oas.annotations.media.Schema
import jakarta.validation.constraints.Size
import java.time.LocalDate
import java.util.UUID

Expand All @@ -17,7 +19,8 @@ data class RoomSaveRequestForm(
@Schema(description = "썸네일 이미지")
val thumbnailLink: String,
@Schema(description = "모임 비밀번호")
val password: Short,
@Size(max = 4, message = "Data must be 4 characters or less")
val password: Password,
@Schema(description = "모임 날짜")
val meetDay: LocalDate,
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.piikii.application.port.input.room.dto.request

import com.piikii.application.domain.room.Password
import com.piikii.application.domain.room.Room
import io.swagger.v3.oas.annotations.media.Schema
import jakarta.validation.constraints.Size
import java.time.LocalDate
import java.time.LocalDateTime
import java.util.UUID
Expand All @@ -16,7 +18,8 @@ data class RoomUpdateRequestForm(
@Schema(description = "썸네일 이미지")
val thumbnailLink: String,
@Schema(description = "모임 비밀번호")
val password: Short,
@Size(max = 4, message = "Data must be 4 characters or less")
val password: Password,
@Schema(description = "모임 날짜")
val meetDay: LocalDate,
@Schema(description = "투표 마감일")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package com.piikii.application.port.input.room.dto.request

import com.piikii.application.domain.room.Password
import io.swagger.v3.oas.annotations.media.Schema
import jakarta.validation.constraints.Size
import java.time.LocalDateTime

data class VoteDeadlineSetRequest(
@Schema(description = "투표 마감일")
val voteDeadline: LocalDateTime,
@Schema(description = "방 패스워드")
@Size(max = 4, message = "Data must be 4 characters or less")
val password: Password,
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ enum class ExceptionCode(
// 400
ILLEGAL_ARGUMENT_EXCEPTION(400, "요청 값이 올바르지 않습니다."),
UNAUTHORIZED(401, "인증된 토큰으로부터의 요청이 아닙니다."),
ROOM_PASSWORD_INVALID(401, "방 패스워드가 틀립니다."),
ACCESS_DENIED(403, "해당 리소스에 접근할 수 없습니다."),
NOT_FOUNDED(404, "해당 리소스를 찾을 수 없습니다."),
CONFLICT(409, "해당 리소스가 중복됩니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.piikii.input.http.advice
import com.piikii.common.exception.PiikiiException
import com.piikii.common.logutil.SystemLogger.logger
import com.piikii.input.http.dto.response.ExceptionResponse
import jakarta.validation.ConstraintDeclarationException
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.ExceptionHandler
Expand All @@ -24,6 +25,22 @@ class ExceptionAdvice {
)
}

@ExceptionHandler(ConstraintDeclarationException::class)
fun handleConstraintDeclarationException(
exception: ConstraintDeclarationException,
): ResponseEntity<ExceptionResponse> {
logger.error(exception) { "RequestBody Validation Failure" }
return ResponseEntity
.status(HttpStatus.BAD_REQUEST)
.body(
ExceptionResponse(
message = "요청 형식이 잘못됬습니다 (Request Validation Failure)",
cause = exception.message,
timestamp = System.currentTimeMillis(),
),
)
}

@ExceptionHandler(Exception::class)
fun handleException(exception: Exception): ResponseEntity<ExceptionResponse> {
logger.error(exception) { "Occurred Exception" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import com.piikii.application.port.input.room.dto.response.RoomSaveResponseForm
import com.piikii.input.http.docs.RoomApiDocs
import com.piikii.input.http.dto.ResponseForm
import com.piikii.input.http.dto.RoomMessage
import jakarta.validation.Valid
import org.springframework.http.HttpStatus
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.DeleteMapping
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
Expand All @@ -20,6 +22,7 @@ import org.springframework.web.bind.annotation.ResponseStatus
import org.springframework.web.bind.annotation.RestController
import java.util.UUID

@Validated
@RestController
@RequestMapping("/v1/rooms")
class RoomApi(
Expand All @@ -28,7 +31,7 @@ class RoomApi(
@ResponseStatus(HttpStatus.CREATED)
@PostMapping
override fun generate(
@RequestBody request: RoomSaveRequestForm,
@Valid @RequestBody request: RoomSaveRequestForm,
): ResponseForm<RoomSaveResponseForm> {
return ResponseForm(
data = roomUseCase.generate(request),
Expand All @@ -39,7 +42,7 @@ class RoomApi(
@ResponseStatus(HttpStatus.OK)
@PutMapping
override fun modifyInformation(
@RequestBody request: RoomUpdateRequestForm,
@Valid @RequestBody request: RoomUpdateRequestForm,
): ResponseForm<Unit> {
roomUseCase.modify(request)
return ResponseForm(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import com.piikii.input.http.docs.VoteApiDocs
import com.piikii.input.http.dto.ResponseForm
import com.piikii.input.http.dto.request.VoteRequest
import com.piikii.input.http.dto.response.VoteStatusResponse
import jakarta.validation.Valid
import org.springframework.http.HttpStatus
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PatchMapping
import org.springframework.web.bind.annotation.PathVariable
Expand All @@ -18,6 +20,7 @@ import org.springframework.web.bind.annotation.ResponseStatus
import org.springframework.web.bind.annotation.RestController
import java.util.UUID

@Validated
@RestController
@RequestMapping("/room/{roomId}/votes")
class VoteApi(
Expand All @@ -28,9 +31,9 @@ class VoteApi(
@PatchMapping("/deadline")
override fun changeVoteDeadline(
@PathVariable roomId: UUID,
@RequestBody request: VoteDeadlineSetRequest,
@Valid @RequestBody request: VoteDeadlineSetRequest,
): ResponseForm<Unit> {
roomUseCase.changeVoteDeadline(roomId, request.voteDeadline)
roomUseCase.changeVoteDeadline(roomId, request.password, request.voteDeadline)
return ResponseForm.EMPTY_RESPONSE
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import com.fasterxml.jackson.annotation.JsonInclude

@JsonInclude(JsonInclude.Include.NON_NULL)
class ExceptionResponse(
val message: String,
val message: String?,
val cause: String?,
val timestamp: Long,
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.piikii.output.persistence.postgresql.persistence.entity

import com.piikii.application.domain.room.Password
import com.piikii.application.domain.room.Room
import com.piikii.output.persistence.postgresql.persistence.common.BaseEntity
import jakarta.persistence.Column
Expand All @@ -22,8 +23,8 @@ class RoomEntity(
var meetDay: LocalDate,
@Column(name = "thumbnail_link", length = 255)
var thumbnailLink: String,
@Column(name = "password", nullable = false)
var password: Short,
@Column(name = "password", nullable = false, length = 4)
var password: Password,
@Column(name = "vote_deadline")
var voteDeadline: LocalDateTime?,
@Column(name = "room_id", nullable = false, unique = true)
Expand Down

0 comments on commit 8942a7f

Please sign in to comment.