Skip to content

#28,29 - 유저는 투표 결과를 조회할 수 있다. #38

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

Merged
merged 23 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
5b0a774
refactor: VoteUseCase 네이밍 변경
kpeel5839 Jul 22, 2024
3bd48f5
feat: VoteRank Service 정의
kpeel5839 Jul 22, 2024
9b10811
feat: API 정의
kpeel5839 Jul 22, 2024
bb765fa
feat: DTO 정의
kpeel5839 Jul 22, 2024
a501dcf
feat: 오늘의 질문지를 감싸는 객체 정의
kpeel5839 Jul 22, 2024
853840e
feat: VoteMetrics, VoteRecord 정의
kpeel5839 Jul 22, 2024
d7b1317
feat: Domain 로직 정의
kpeel5839 Jul 22, 2024
058a95c
refactor: 조회에는 Transactional 어노테이션 제거
kpeel5839 Jul 22, 2024
0d70fa1
feat: 순위 반환 서비스 로직 작성
kpeel5839 Jul 22, 2024
84175b5
refactor: SavedVoteService에서 Helper를 사용할 수 있도록 수정
kpeel5839 Jul 22, 2024
3735400
refactor: Response 명 변경
kpeel5839 Jul 22, 2024
033fb6d
refactor: Security Context에서 UserId를 뽑아낼 수 있도록 수정
kpeel5839 Jul 22, 2024
0bd40f4
refactor: 순환해서 질문지를 가져오지 않도록 수정
kpeel5839 Jul 22, 2024
a924c93
chore: submodule 가져오는 과정 ci에 추가
kpeel5839 Jul 22, 2024
0a206fd
test: 오늘의 질문지 테스트 작성
kpeel5839 Jul 22, 2024
2393cd7
test: VoteMetrics 테스트 작성
kpeel5839 Jul 22, 2024
9faf8c0
test: VoteRecord 테스트 작성
kpeel5839 Jul 22, 2024
b20251c
test: BallotsAggregator 테스트 작성
kpeel5839 Jul 22, 2024
1a175af
test: Vote 테스트 작성
kpeel5839 Jul 22, 2024
dec455b
test: VoteRankServiceTest 작성
kpeel5839 Jul 22, 2024
43e1841
chore: CustomUrlFilter에 valid url 추가
kpeel5839 Jul 22, 2024
36ad537
refactor: groupNumber->classNumber로 변경
kpeel5839 Jul 22, 2024
0f571bb
refactor: groupNumber->classNumber로 변경
kpeel5839 Jul 22, 2024
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
10 changes: 7 additions & 3 deletions .github/workflows/opened-pr-notification.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
with:
submodules: recursive
token: ${{ secrets.SUBMODULE_ACCESS_TOKEN }}
- uses: actions/checkout@v3
- name: set up JDK 17
uses: actions/setup-java@v3
Expand All @@ -36,7 +40,7 @@ jobs:
DISCORD_AVATAR: https://www.notion.so/image/https%3A%2F%2Fi.imgur.com%2FqIqJ0GA.png?table=block&id=232c40c1-598a-4163-bb8d-acf1151cd026&spaceId=4263d120-003b-49f8-8cb5-37585d6fb45d&width=2000&userId=c0125a99-ca81-4e9f-9c32-096f12a6698c&cache=v2
DISCORD_EMBEDS: |
[
{
{
"author": {
"name": "${{ github.event.pull_request.user.login }}",
"url": "https://github.com/pknu-wap/WAPP/blob/main/image/icon.png?raw=true",
Expand Down Expand Up @@ -69,7 +73,7 @@ jobs:
DISCORD_AVATAR: https://www.notion.so/image/https%3A%2F%2Fi.imgur.com%2FqIqJ0GA.png?table=block&id=232c40c1-598a-4163-bb8d-acf1151cd026&spaceId=4263d120-003b-49f8-8cb5-37585d6fb45d&width=2000&userId=c0125a99-ca81-4e9f-9c32-096f12a6698c&cache=v2
DISCORD_EMBEDS: |
[
{
{
"author": {
"name": "${{ github.event.pull_request.user.login }}",
"url": "https://github.com/pknu-wap/WAPP/blob/main/image/icon.png?raw=true",
Expand All @@ -91,4 +95,4 @@ jobs:
}
]
}
]
]
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class CustomUrlFilter(
"/api/v1/users/me",
"/api/v1/users/backgrounds",
"/api/v1/users/characters",
"/v1/votes/options",
"/v1/votes",
"/v1/votes/tops",
)

override fun doFilterInternal(
Expand Down
35 changes: 29 additions & 6 deletions app/src/main/kotlin/com/wespot/vote/VoteController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,58 @@ package com.wespot.vote
import com.wespot.vote.dto.request.VoteRequests
import com.wespot.vote.dto.response.SaveVoteResponse
import com.wespot.vote.dto.response.VoteItems
import com.wespot.vote.port.`in`.VoteUseCase
import com.wespot.vote.dto.response.top1.VoteResultResponsesOfTop1
import com.wespot.vote.dto.response.top5.VoteResultResponsesOfTop5
import com.wespot.vote.port.`in`.SavedVoteUseCase
import com.wespot.vote.port.`in`.VoteRankUseCase
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
import java.time.LocalDate

@RestController
@RequestMapping("/v1/votes")
class VoteController(
private val voteUseCase: VoteUseCase
private val savedVoteUseCase: SavedVoteUseCase,
private val voteRankUseCase: VoteRankUseCase
) {

@GetMapping("/options")
fun getVoteOptions(userId: Long): ResponseEntity<VoteItems> {
val responses = voteUseCase.getVoteOptions(userId)
fun getVoteOptions(): ResponseEntity<VoteItems> {
val responses = savedVoteUseCase.getVoteOptions()
return ResponseEntity.ok(responses)
}

@PostMapping
fun createVote(
userId: Long,
@RequestBody requests: VoteRequests
): ResponseEntity<SaveVoteResponse> {
val savedId: SaveVoteResponse = voteUseCase.saveVote(userId, requests)
val savedId: SaveVoteResponse = savedVoteUseCase.saveVote(requests)
return ResponseEntity.status(HttpStatus.CREATED)
.body(savedId)
}

@GetMapping
fun getTop5VoteResults(
@RequestParam date: LocalDate
): ResponseEntity<VoteResultResponsesOfTop5> {
val responses: VoteResultResponsesOfTop5 = voteRankUseCase.getVoteResultsOfTop5(date)

return ResponseEntity.ok(responses)
}

@GetMapping("/tops")
fun getTop1VoteResults(
@RequestParam date: LocalDate
): ResponseEntity<VoteResultResponsesOfTop1> {
val responses: VoteResultResponsesOfTop1 = voteRankUseCase.getVoteResultsOfTop1(date)

return ResponseEntity.ok(responses)
}

}
46 changes: 44 additions & 2 deletions app/src/test/kotlin/com/wespot/user/fixture/UserFixture.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@ package com.wespot.user.fixture

import com.wespot.auth.PrincipalDetails
import com.wespot.auth.dto.request.ProfileRequest
import com.wespot.user.*
import com.wespot.user.ConsentType
import com.wespot.user.FCM
import com.wespot.user.Profile
import com.wespot.user.Role
import com.wespot.user.Setting
import com.wespot.user.Social
import com.wespot.user.SocialType
import com.wespot.user.User
import com.wespot.user.UserConsent
import com.wespot.user.dto.request.UpdateProfileRequest
import org.springframework.security.authentication.TestingAuthenticationToken
import org.springframework.security.core.context.SecurityContextHolder
Expand Down Expand Up @@ -43,8 +51,42 @@ object UserFixture {
withdrawAt = LocalDateTime.now(),
)

fun createWithIdAndEmail(
id: Long,
email: String,
) = User(
id = id,
email = email,
password = "TestPassword",
role = Role.USER,
name = "TestUser",
introduction = "hello",
gender = "male",
schoolId = 1L,
grade = 1,
classNumber = 1,
setting = Setting(),
profile = Profile(0, "black", "image.png"),
fcm = FCM(0, "token", LocalDateTime.now()),
social = Social(
socialType = SocialType.KAKAO,
socialId = "1123123",
socialEmail = null,
socialRefreshToken = "refreshToken"
),
userConsent = UserConsent(
id = 0,
consentType = ConsentType.MARKETING,
consentValue = true,
consentedAt = LocalDateTime.now()
),
createdAt = LocalDateTime.now(),
updatedAt = null,
withdrawAt = LocalDateTime.now(),
)

// SecurityContextHolder를 사용하여 테스트를 위한 User를 설정
fun setSecurityContextUser(user : User){
fun setSecurityContextUser(user: User) {
val mockUserDetail = PrincipalDetails(user)

val authentication = TestingAuthenticationToken(mockUserDetail, null)
Expand Down
113 changes: 113 additions & 0 deletions app/src/test/kotlin/com/wespot/vote/domain/BallotsAggregatorTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package com.wespot.vote.domain

import com.wespot.vote.BallotsAggregator
import com.wespot.vote.fixture.BallotFixture
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.style.BehaviorSpec
import io.kotest.matchers.shouldBe
import io.kotest.matchers.throwable.shouldHaveMessage
import java.time.LocalDateTime

class BallotsAggregatorTest : BehaviorSpec({

given("투표지 집계기를 생성할 때") {
`when`("서로 다른 선택지에 대한 결과가 섞여 있으면") {
then("예외가 발생한다.") {
val shouldThrow = shouldThrow<IllegalArgumentException> {
BallotsAggregator.of(
1L,
listOf(BallotFixture.createByVoteAndVoteOptionAndSenderAndReceiver(1L, 2L, 1L, 2L))
)
}
shouldThrow shouldHaveMessage "서로 다른 질문지에 대한 결과가 섞였습니다."
}
}
}

given("투표지 집계기를 통해") {
val calculateRankByBallotSize = listOf(
BallotFixture.createByVoteAndVoteOptionAndSenderAndReceiver(1L, 1L, 1L, 2L),
BallotFixture.createByVoteAndVoteOptionAndSenderAndReceiver(1L, 1L, 2L, 3L),
BallotFixture.createByVoteAndVoteOptionAndSenderAndReceiver(1L, 1L, 3L, 2L),
BallotFixture.createByVoteAndVoteOptionAndSenderAndReceiver(1L, 1L, 4L, 3L),
BallotFixture.createByVoteAndVoteOptionAndSenderAndReceiver(1L, 1L, 5L, 2L),
BallotFixture.createByVoteAndVoteOptionAndSenderAndReceiver(1L, 1L, 6L, 4L),
)
`when`("득표수를 토대로") {
val ballotsAggregator = BallotsAggregator.of(1L, calculateRankByBallotSize)
val rankedResults = ballotsAggregator.getRankResults()
then("정렬된 순서를 반환받는다.") {
rankedResults[0].userId shouldBe 2
rankedResults[0].voteCount shouldBe 3
rankedResults[1].userId shouldBe 3
rankedResults[1].voteCount shouldBe 2
rankedResults[2].userId shouldBe 4
rankedResults[2].voteCount shouldBe 1
}
}
val calculateRankByBallotSizeAndReceivedAt = listOf(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

변수명 엄청 기네요😭

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ㅋㅋㅋㅋㅋㅋㅋㅋ 어쩌다보니.. 테스트니까 이해해주세요오옹 ♡♡

BallotFixture.createByVoteAndVoteOptionAndSenderAndReceiverAndCreatedAt(
1L,
1L,
1L,
2L,
LocalDateTime.now().minusHours(10)
),
BallotFixture.createByVoteAndVoteOptionAndSenderAndReceiverAndCreatedAt(
1L,
1L,
3L,
2L,
LocalDateTime.now().minusHours(10)
),
BallotFixture.createByVoteAndVoteOptionAndSenderAndReceiverAndCreatedAt(
1L,
1L,
5L,
2L,
LocalDateTime.now().minusHours(10)
),
BallotFixture.createByVoteAndVoteOptionAndSenderAndReceiverAndCreatedAt(
1L,
1L,
2L,
3L,
LocalDateTime.now().minusHours(10)
),
BallotFixture.createByVoteAndVoteOptionAndSenderAndReceiverAndCreatedAt(
1L,
1L,
4L,
3L,
LocalDateTime.now().minusHours(1)
),
BallotFixture.createByVoteAndVoteOptionAndSenderAndReceiverAndCreatedAt(
1L,
1L,
6L,
4L,
LocalDateTime.now().minusHours(2)
),
BallotFixture.createByVoteAndVoteOptionAndSenderAndReceiverAndCreatedAt(
1L,
1L,
7L,
4L,
LocalDateTime.now().minusHours(2)
),
)
`when`("득표수가 같다면, 가장 최근에 받은 투푠을 기준으로") {
val ballotsAggregator = BallotsAggregator.of(1L, calculateRankByBallotSizeAndReceivedAt)
val rankedResults = ballotsAggregator.getRankResults()
then("정렬된 순서를 반환받는다.") {
rankedResults[0].userId shouldBe 2
rankedResults[0].voteCount shouldBe 3
rankedResults[1].userId shouldBe 3
rankedResults[1].voteCount shouldBe 2
rankedResults[2].userId shouldBe 4
rankedResults[2].voteCount shouldBe 2
}
}
}

})
36 changes: 36 additions & 0 deletions app/src/test/kotlin/com/wespot/vote/domain/RateTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.wespot.vote.domain

import com.wespot.vote.Rate
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.style.BehaviorSpec
import io.kotest.matchers.shouldBe
import io.kotest.matchers.throwable.shouldHaveMessage

class RateTest : BehaviorSpec({

given("Rate를 만들 때") {
val validRate = 1
`when`("0 이하의 등수가 입력되면") {
then("예외가 발생한다.") {
val shouldThrow = shouldThrow<IllegalArgumentException> { Rate.from(0) }
shouldThrow shouldHaveMessage "등수는 0 이하일 수 없습니다."
}
}
`when`("정상적인 값이 입력되면") {
val rate = Rate.from(validRate)
then("정상적으로 생성된다.") {
rate.value shouldBe validRate
}
}
}

given("의미없는") {
`when`("Rate를 만들면") {
val Rate = Rate.createMeaningLessRate()
then("등수가 Int MAX VALUE로 설정되게 된다.") {
Rate.value shouldBe Int.MAX_VALUE
}
}
}

})
50 changes: 50 additions & 0 deletions app/src/test/kotlin/com/wespot/vote/domain/VoteMetricsTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.wespot.vote.domain

import com.wespot.vote.VoteMetrics
import com.wespot.vote.fixture.BallotFixture
import io.kotest.core.spec.style.BehaviorSpec
import io.kotest.matchers.shouldBe
import java.time.LocalDateTime

class VoteMetricsTest : BehaviorSpec({

given("초기의 VoteMetrics를") {
val userId = 1L
`when`("생성하면") {
val voteMetrics = VoteMetrics.createInitialState(userId)
then("정상적으로 생성된다.") {
voteMetrics.userId shouldBe userId
voteMetrics.lastVotedDateTime shouldBe LocalDateTime.MIN
voteMetrics.voteCount shouldBe 0
voteMetrics.isReceiverRead shouldBe true
}
}
}

given("기존의 VoteMetrics에") {
val userId = 1L
val voteMetrics = VoteMetrics.createInitialState(userId)
val ballot = BallotFixture.create()
`when`("최근의 Ballot을 추가하면") {
ballot.receiverRead()
val resultVoteMetrics = voteMetrics.recordBallot(ballot)
then("최근으로 갱신된다.") {
resultVoteMetrics.userId shouldBe userId
resultVoteMetrics.lastVotedDateTime shouldBe ballot.createdAt
resultVoteMetrics.voteCount shouldBe 1
resultVoteMetrics.isReceiverRead shouldBe true
}
}
`when`("수신자가 읽지 않은 Ballot을 추가하면") {
val noReceiverReadBallot = BallotFixture.create()
val resultVoteMetrics = voteMetrics.recordBallot(noReceiverReadBallot)
then("읽지 않음으로 갱신된다.") {
resultVoteMetrics.userId shouldBe userId
resultVoteMetrics.lastVotedDateTime shouldBe noReceiverReadBallot.createdAt
resultVoteMetrics.voteCount shouldBe 1
resultVoteMetrics.isReceiverRead shouldBe false
}
}
}

})
Loading