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

feat: identity #5

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open
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
@@ -0,0 +1,38 @@
package uoslife.springaccount.app.identity.controller

import org.springframework.http.ResponseEntity
import org.springframework.security.core.annotation.AuthenticationPrincipal
import org.springframework.security.core.userdetails.UserDetails
import org.springframework.web.bind.annotation.*
import uoslife.springaccount.app.identity.dto.response.IdentityDto
import uoslife.springaccount.app.identity.service.IdentityService


@RestController
@RequestMapping("/v2/identities")
class IdentityController(private val identityService: IdentityService) {
@GetMapping
fun getMyIdentities(
@AuthenticationPrincipal userDetails: UserDetails
): ResponseEntity<List<IdentityDto.IdentityResponse>> {
val userId = userDetails.username.toLong();

return ResponseEntity.ok(identityService.getIdentityList(userId))
}

@PatchMapping("/{identityId}")
fun selectRepresentativeIdentity(@AuthenticationPrincipal userDetails: UserDetails, @PathVariable identityId: String
) :ResponseEntity<List<IdentityDto.IdentityResponse>> {
val userId = userDetails.username.toLong();

val result = identityService.selectRepresentativeIdentityFromList(userId)

return ResponseEntity.ok(result)
//TODO: identityList๊ฐ€ 1๊ฐœ์ธ ๊ฒฝ์šฐ, ์‘๋‹ต๊ฐ’ ๋ฐ˜ํ™˜์— ๋Œ€ํ•œ ๊ณ ๋ฏผ ํ•„์š”.
// return if (result == null) {
// ResponseEntity.ok("์ˆ˜์ •ํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.");
// } else {
// ResponseEntity.ok(result)
// }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package uoslife.springaccount.app.identity.domain.repository.jpa

import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Modifying
import org.springframework.data.jpa.repository.Query
import uoslife.springaccount.app.identity.domain.entity.Identity

interface IdentityRepository : JpaRepository<Identity, String> {
fun findIdentitiesByUserId(userId: Long): List<Identity>?

fun findByUserId(userId: Long): List<Identity>?

// ์ถ”ํ›„ queryDsl๋กœ ๋ณ€๊ฒฝ ์˜ˆ์ •.
@Modifying
@Query("UPDATE Identity i SET i.isActive = CASE WHEN i.id = :identityId THEN true ELSE false END WHERE i.user.id = :userId")
fun updateActiveIdentityList(identityId: String, userId: Long)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package uoslife.springaccount.app.identity.dto.response

import uoslife.springaccount.app.identity.domain.entity.Identity

class IdentityDto {
data class IdentityResponse (
val type: String,
val status: String,
val idNumber: String,
val university: String? = null,
val department: String? = null,
val major: String? = null, // null ๊ฐ€๋Šฅ์„ฑ ๊ณ ๋ ค
val isActive: Boolean
)

companion object {
fun toResponse(identity : Identity) : IdentityResponse{
return IdentityResponse(
type = identity.type,
status = identity.status,
idNumber = identity.idNumber,
university = identity.university,
department = identity.department,
major = identity.major,
isActive = identity.isActive,
)
}

fun toResponseIdentityList (identityList: List<Identity>?): List<IdentityDto.IdentityResponse>? {
return identityList?.map { toResponse(it) }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package uoslife.springaccount.app.identity.service

import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import uoslife.springaccount.app.identity.domain.entity.Identity
import uoslife.springaccount.app.identity.domain.repository.jpa.IdentityRepository
import uoslife.springaccount.app.identity.dto.response.IdentityDto

@Service
@Transactional(readOnly = true)
class IdentityService(
private val identityRepository : IdentityRepository,
) {

// ์œ ์ €์˜ ์‹ ๋ถ„ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.
// ๋ฆฌ๋นŒ๋“œ ์ด์ „์˜ ์‹œ๋Œ€์ƒ ์œ ์ €์ธ ๊ฒฝ์šฐ, ๋ณต์ˆ˜ ์‹ ๋ถ„์ด ์กด์žฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
fun getIdentityList(userId: Long):List<IdentityDto.IdentityResponse>? {
val identityList = identityRepository.findByUserId(userId);

return IdentityDto.toResponseIdentityList(identityList);
}

// ์—ฌ๋Ÿฌ ์‹ ๋ถ„ ์ค‘ ๋Œ€ํ‘œ ์‹ ๋ถ„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
fun selectRepresentativeIdentityFromList(userId: Long) :List<IdentityDto.IdentityResponse>?{
val identityList = identityRepository.findByUserId(userId);

// ์‹ ๋ถ„์˜ ๊ฐœ์ˆ˜๊ฐ€ 1๊ฐœ์ธ ๊ฒฝ์šฐ ์—…๋ฐ์ดํŠธ ๋กœ์ง์„ ์‹คํ–‰ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
if (identityList?.size == 1) return null;
Copy link
Member

Choose a reason for hiding this comment

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

์‹ ๋ถ„์ด ํ•˜๋‚˜์ธ ๊ฒฝ์šฐ ์˜ˆ์™ธ๋ฅผ ๋˜์ง€๋Š” ๊ฒƒ๋„ ํ•˜๋‚˜์˜ ๋ฐฉ๋ฒ•์ด ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.


// ์—ฌ๋Ÿฌ ์‹ ๋ถ„ ์ค‘ ๋Œ€ํ‘œ ์‹ ๋ถ„์œผ๋กœ ์„ค์ •ํ•˜๋Š” ๋กœ์ง์ž…๋‹ˆ๋‹ค.
identityList?.forEach { identity: Identity ->
identityRepository.updateActiveIdentityList(identity.id, userId)
}
Comment on lines +31 to +33
Copy link
Member

Choose a reason for hiding this comment

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

IdentityRepository ๋‚ด๋ถ€์— ๋Œ€ํ‘œ ์‹ ๋ถ„์„ ์„ค์ •ํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•ด์„œ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง๊ณผ ๋ถ„๋ฆฌํ•˜๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.


return IdentityDto.toResponseIdentityList(identityList);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package uoslife.springaccount.app.identity.common

import io.mockk.MockKAnnotations
import io.mockk.every
import io.mockk.impl.annotations.MockK
import io.mockk.mockk
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.BeforeEach
import org.redisson.api.RedissonClient
import org.springframework.boot.test.context.SpringBootTest
import uoslife.springaccount.app.identity.domain.entity.Identity
import uoslife.springaccount.app.identity.domain.repository.jpa.IdentityRepository
import uoslife.springaccount.app.identity.service.IdentityService
import uoslife.springaccount.app.user.domain.entity.User

@SpringBootTest
abstract class IdentityTest {

val identityService = mockk<IdentityService>()
val identityRepository = mockk<IdentityRepository>()
val redissonClient = mockk<RedissonClient>()
// @MockK
// protected lateinit var identityService: IdentityService
//
// @MockK
// protected lateinit var identityRepository: IdentityRepository

protected val userDummyData1 = User(
name = "๊น€์˜์ฐฌ",
phoneNumber = "010-2064-6347",
email = "[email protected]",
nickname = "kyc",
birthday = "1999-12-03",
avatarUrl = null,
)

protected val identityDummyData1 = Identity(
user = userDummyData1,
type = "Student",
status = "Active",
idNumber = "123456789",
university = "Seoul National University",
universityCode = "SNU",
department = "Computer Science",
departmentCode = "CS",
major = null,
majorCode = null,
isActive = false
)

protected val identityDummyData2 = Identity(
user = userDummyData1,
type = "Alumnus",
status = "Inactive",
idNumber = "987654321",
university = "Korea University",
universityCode = "KU",
department = "Business Administration",
departmentCode = "BA",
major = "Marketing",
majorCode = "MK",
isActive = false
)

protected val identityDummyData3 = Identity(
user = userDummyData1,
type = "Student",
status = "Active",
idNumber = "111223344",
university = "Yonsei University",
universityCode = "YU",
department = "Electrical Engineering",
departmentCode = "EE",
major = "Robotics",
majorCode = "RO",
isActive = false
)

companion object {
@BeforeAll
@JvmStatic
fun init() {
MockKAnnotations.init(this)
}
}

@BeforeEach
fun setUp() {
// Mocking repository์˜ ๋™์ž‘ ์ •์˜
every { userDummyData1.id?.let { identityRepository.findIdentitiesByUserId(it) } } returns listOf(identityDummyData1, identityDummyData2, identityDummyData3)

// every { userService.findUser(any()) } answers {
// val userId = firstArg<UUID>()
// if (userId == user1.id) {
// user1
// } else {
// user2
// }
// }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package uoslife.springaccount.app.identity.service

import io.mockk.every
import io.mockk.verify
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import uoslife.springaccount.app.identity.common.IdentityTest

class getIdentityListTest : IdentityTest() {

@Test
fun `getIdentityList should return identity list for given userId`() {
// Given
init();
val userId = userDummyData1.id ?: throw IllegalArgumentException("User ID cannot be null")

// When
val result = identityService.getIdentityList(userId)

// Then
assertEquals(3, result?.size) // ๋”๋ฏธ ๋ฐ์ดํ„ฐ์—์„œ 3๊ฐœ์˜ ์‹ ๋ถ„ ์ •๋ณด๊ฐ€ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
assertEquals(identityDummyData1.type, result?.get(0)?.type)
assertEquals(identityDummyData2.type, result?.get(1)?.type)
assertEquals(identityDummyData3.type, result?.get(2)?.type)

// identityRepository์˜ ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋˜์—ˆ๋Š”์ง€ ํ™•์ธ
verify { identityRepository.findIdentitiesByUserId(userId) }
}
}