Skip to content

Commit 8624713

Browse files
authored
merge: (#813) 투표 결과 조회 api 생성
2 parents e143228 + e0882e9 commit 8624713

File tree

8 files changed

+188
-12
lines changed

8 files changed

+188
-12
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package team.aliens.dms.domain.vote.dto.response
2+
3+
import java.util.UUID
4+
5+
data class VoteResponse(
6+
val id: UUID,
7+
val name: String,
8+
val votes: Int,
9+
val classNumber: String?
10+
) {
11+
companion object {
12+
fun of(
13+
id: UUID,
14+
name: String,
15+
votes: Int,
16+
classNumber: String?
17+
) = VoteResponse(
18+
id = id,
19+
name = name,
20+
votes = votes,
21+
classNumber = classNumber
22+
)
23+
}
24+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package team.aliens.dms.domain.vote.dto.response
2+
3+
data class VotesResponse(
4+
val votes: List<VoteResponse>
5+
) {
6+
companion object {
7+
fun of(
8+
votes: List<VoteResponse>
9+
) = VotesResponse(
10+
votes = votes
11+
)
12+
}
13+
}

dms-core/src/main/kotlin/team/aliens/dms/domain/vote/usecase/CreateVoteUseCase.kt

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import team.aliens.dms.domain.student.service.StudentService
55
import team.aliens.dms.domain.vote.exception.AlreadyVotedException
66
import team.aliens.dms.domain.vote.model.Vote
77
import team.aliens.dms.domain.vote.model.VoteType
8-
import team.aliens.dms.domain.vote.model.VotingOption
98
import team.aliens.dms.domain.vote.model.VotingTopic
109
import team.aliens.dms.domain.vote.service.VoteService
1110
import java.time.LocalDateTime
@@ -26,7 +25,7 @@ class CreateVoteUseCase(
2625
}
2726

2827
val voteType: VoteType = votingTopic.voteType
29-
28+
3029
val selectedOptionId = if (voteType == VoteType.OPTION_VOTE || voteType == VoteType.APPROVAL_VOTE) {
3130
voteService.getVotingOptionById(selectedId).id
3231
} else null
@@ -35,13 +34,13 @@ class CreateVoteUseCase(
3534
} else null
3635

3736
voteService.createVote(
38-
Vote(
39-
studentId = student.id,
40-
votingTopicId = votingTopic.id,
41-
votedAt = LocalDateTime.now(),
42-
selectedOptionId = selectedOptionId,
43-
selectedStudentId = selectedStudentId
44-
)
37+
Vote(
38+
studentId = student.id,
39+
votingTopicId = votingTopic.id,
40+
votedAt = LocalDateTime.now(),
41+
selectedOptionId = selectedOptionId,
42+
selectedStudentId = selectedStudentId
43+
)
4544
)
4645
}
4746
}

dms-core/src/main/kotlin/team/aliens/dms/domain/vote/usecase/RemoveVoteUseCase.kt renamed to dms-core/src/main/kotlin/team/aliens/dms/domain/vote/usecase/DeleteVoteUseCase.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import team.aliens.dms.domain.vote.service.VoteService
99
import java.util.UUID
1010

1111
@UseCase
12-
class RemoveVoteUseCase(
12+
class DeleteVoteUseCase(
1313
private val voteService: VoteService,
1414
private val studentService: StudentService
1515
) {

dms-core/src/main/kotlin/team/aliens/dms/domain/vote/usecase/RemoveVotingOptionUseCase.kt renamed to dms-core/src/main/kotlin/team/aliens/dms/domain/vote/usecase/DeleteVotingOptionUseCase.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import team.aliens.dms.domain.vote.service.VoteService
66
import java.util.UUID
77

88
@UseCase
9-
class RemoveVotingOptionUseCase(
9+
class DeleteVotingOptionUseCase(
1010
private val voteService: VoteService
1111
) {
1212

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package team.aliens.dms.domain.vote.usecase
2+
3+
import team.aliens.dms.common.annotation.UseCase
4+
import team.aliens.dms.domain.student.model.Student
5+
import team.aliens.dms.domain.student.service.StudentService
6+
import team.aliens.dms.domain.vote.dto.response.VoteResponse
7+
import team.aliens.dms.domain.vote.dto.response.VotesResponse
8+
import team.aliens.dms.domain.vote.model.VoteType
9+
import team.aliens.dms.domain.vote.service.VoteService
10+
import team.aliens.dms.domain.vote.spi.vo.OptionVotingResultVO
11+
import team.aliens.dms.domain.vote.spi.vo.StudentVotingResultVO
12+
import java.util.UUID
13+
14+
@UseCase
15+
class QueryVotesUseCase(
16+
private val voteService: VoteService,
17+
private val studentService: StudentService
18+
) {
19+
20+
fun execute(votingTopicId: UUID): VotesResponse {
21+
val votingTopic = voteService.getVotingTopicById(votingTopicId)
22+
23+
return when (votingTopic.voteType) {
24+
VoteType.STUDENT_VOTE, VoteType.MODEL_STUDENT_VOTE -> queryStudentVotingVotes(votingTopicId)
25+
VoteType.OPTION_VOTE, VoteType.APPROVAL_VOTE -> queryOptionVotingVotes(votingTopicId)
26+
}
27+
}
28+
29+
private fun queryOptionVotingVotes(votingTopicId: UUID): VotesResponse {
30+
val result: List<OptionVotingResultVO> = voteService.getVotesInOptionVotingByVotingTopicId(votingTopicId)
31+
return VotesResponse.of(
32+
votes = result.map { votingResult ->
33+
VoteResponse(
34+
id = votingResult.id,
35+
name = votingResult.name,
36+
votes = votingResult.votes,
37+
classNumber = null
38+
)
39+
}
40+
)
41+
}
42+
43+
private fun queryStudentVotingVotes(votingTopicId: UUID): VotesResponse {
44+
return VotesResponse.of(
45+
listOf(1, 2, 3).flatMap { grade ->
46+
val result: List<StudentVotingResultVO> = voteService.getVotesInStudentVotingByVotingTopicId(votingTopicId, grade)
47+
result.map { votingResult ->
48+
val student = studentService.getStudentById(votingResult.id)
49+
val classNumber = Student.processGcn(
50+
grade = student.grade,
51+
classRoom = student.classRoom,
52+
number = student.number
53+
)
54+
55+
VoteResponse.of(
56+
id = votingResult.id,
57+
name = votingResult.name,
58+
votes = votingResult.votes,
59+
classNumber = classNumber
60+
)
61+
}
62+
}
63+
)
64+
}
65+
}

dms-presentation/src/main/kotlin/team/aliens/dms/domain/vote/VoteWebAdapter.kt

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,34 @@ package team.aliens.dms.domain.vote
22

33
import jakarta.validation.Valid
44
import org.jetbrains.annotations.NotNull
5+
import org.springframework.http.HttpStatus
56
import org.springframework.web.bind.annotation.DeleteMapping
67
import org.springframework.web.bind.annotation.GetMapping
78
import org.springframework.web.bind.annotation.PatchMapping
89
import org.springframework.web.bind.annotation.PathVariable
910
import org.springframework.web.bind.annotation.PostMapping
1011
import org.springframework.web.bind.annotation.RequestBody
1112
import org.springframework.web.bind.annotation.RequestMapping
13+
import org.springframework.web.bind.annotation.RequestParam
14+
import org.springframework.web.bind.annotation.ResponseStatus
1215
import org.springframework.web.bind.annotation.RestController
1316
import team.aliens.dms.domain.vote.dto.reponse.VotingTopicsResponse
1417
import team.aliens.dms.domain.vote.dto.request.CreateVoteTopicRequest
18+
import team.aliens.dms.domain.vote.dto.request.CreateVotingOptionRequest
19+
import team.aliens.dms.domain.vote.dto.request.CreateVotingOptionWebRequest
1520
import team.aliens.dms.domain.vote.dto.request.CreateVotingTopicWebRequest
1621
import team.aliens.dms.domain.vote.dto.request.UpdateVotingTopicRequest
1722
import team.aliens.dms.domain.vote.dto.request.UpdateVotingTopicWebRequest
23+
import team.aliens.dms.domain.vote.dto.response.VotesResponse
24+
import team.aliens.dms.domain.vote.dto.response.VotingOptionsResponse
25+
import team.aliens.dms.domain.vote.usecase.CreateVoteUseCase
26+
import team.aliens.dms.domain.vote.usecase.CreateVotingOptionUseCase
1827
import team.aliens.dms.domain.vote.usecase.CreateVotingTopicUseCase
28+
import team.aliens.dms.domain.vote.usecase.DeleteVoteUseCase
1929
import team.aliens.dms.domain.vote.usecase.DeleteVotingTopicUseCase
2030
import team.aliens.dms.domain.vote.usecase.QueryAllVotingTopicUseCase
31+
import team.aliens.dms.domain.vote.usecase.QueryVotesUseCase
32+
import team.aliens.dms.domain.vote.usecase.QueryVotingOptionsUseCase
2133
import team.aliens.dms.domain.vote.usecase.UpdateVotingTopicUseCase
2234
import java.util.UUID
2335

@@ -27,8 +39,15 @@ class VoteWebAdapter(
2739
private val createVotingTopicUseCase: CreateVotingTopicUseCase,
2840
private val deleteVotingTopicUseCase: DeleteVotingTopicUseCase,
2941
private val queryAllVotingTopicUseCase: QueryAllVotingTopicUseCase,
30-
private val updateVotingTopicUseCase: UpdateVotingTopicUseCase
42+
private val updateVotingTopicUseCase: UpdateVotingTopicUseCase,
43+
private val createVoteUseCase: CreateVoteUseCase,
44+
private val createVotingOptionUseCase: CreateVotingOptionUseCase,
45+
private val queryVotesUseCase: QueryVotesUseCase,
46+
private val queryVotingOptionsUseCase: QueryVotingOptionsUseCase,
47+
private val deleteVoteUseCase: DeleteVoteUseCase,
48+
private val removeVotingOptionsUseCase: QueryVotingOptionsUseCase
3149
) {
50+
3251
@PostMapping
3352
fun saveVotingTopic(@RequestBody @Valid request: CreateVotingTopicWebRequest) {
3453
createVotingTopicUseCase.execute(
@@ -68,4 +87,48 @@ class VoteWebAdapter(
6887
fun getAllVotingTopic(): VotingTopicsResponse {
6988
return queryAllVotingTopicUseCase.execute()
7089
}
90+
91+
@ResponseStatus(HttpStatus.CREATED)
92+
@PostMapping("/option")
93+
fun createVotingOption(@RequestBody request: CreateVotingOptionWebRequest) {
94+
createVotingOptionUseCase.execute(
95+
CreateVotingOptionRequest.of(
96+
request.votingTopicId,
97+
request.optionName
98+
)
99+
)
100+
}
101+
102+
@ResponseStatus(HttpStatus.OK)
103+
@GetMapping("/option/{voting-topic-id}")
104+
fun getVotingOptions(@PathVariable("voting-topic-id") votingTopicId: UUID): VotingOptionsResponse {
105+
return queryVotingOptionsUseCase.execute(votingTopicId)
106+
}
107+
108+
@ResponseStatus(HttpStatus.NO_CONTENT)
109+
@DeleteMapping("/option/{voting-option-id}")
110+
fun removeVotingOption(@PathVariable("voting-option-id") votingOptionId: UUID) {
111+
removeVotingOptionsUseCase.execute(votingOptionId)
112+
}
113+
114+
@ResponseStatus(HttpStatus.CREATED)
115+
@PostMapping("/student/{voting-topic-id}")
116+
fun createVote(
117+
@PathVariable("voting-topic-id") votingTopicId: UUID,
118+
@RequestParam(name = "selected-id") selectedId: UUID
119+
) {
120+
createVoteUseCase.execute(selectedId, votingTopicId)
121+
}
122+
123+
@ResponseStatus(HttpStatus.NO_CONTENT)
124+
@DeleteMapping("/student/{voting-topic-id}")
125+
fun removeVote(@PathVariable("voting-topic-id") votingTopicId: UUID) {
126+
deleteVoteUseCase.execute(votingTopicId)
127+
}
128+
129+
@ResponseStatus(HttpStatus.OK)
130+
@GetMapping("/result/{voting-topic-id}")
131+
fun getVoteResults(@PathVariable("voting-topic-id") votingTopicId: UUID): VotesResponse {
132+
return queryVotesUseCase.execute(votingTopicId)
133+
}
71134
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package team.aliens.dms.domain.vote.dto.request
2+
3+
import jakarta.validation.constraints.NotBlank
4+
import java.util.UUID
5+
6+
data class CreateVotingOptionWebRequest(
7+
@field:NotBlank
8+
val votingTopicId: UUID,
9+
10+
@field:NotBlank
11+
val optionName: String
12+
)

0 commit comments

Comments
 (0)