Skip to content

Commit

Permalink
#19 account-service 통합
Browse files Browse the repository at this point in the history
  • Loading branch information
chs98412 committed Oct 29, 2024
1 parent 926e05e commit 020b6ff
Show file tree
Hide file tree
Showing 40 changed files with 794 additions and 59 deletions.
7 changes: 3 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ plugins {
kotlin("jvm") version "1.9.24"
kotlin("plugin.spring") version "1.9.24"
kotlin("plugin.jpa") version "1.9.24"
id("com.epages.restdocs-api-spec") version "0.17.1"
}

group = "com.yedongsoon"
Expand All @@ -31,13 +30,13 @@ dependencies {
testImplementation("io.projectreactor:reactor-test")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
implementation("org.springframework.boot:spring-boot-starter-validation")
testImplementation("com.epages:restdocs-api-spec:0.17.1")
testImplementation("io.mockk:mockk:1.13.8")
testImplementation("org.springframework.restdocs:spring-restdocs-webtestclient:3.0.0")
testImplementation("com.ninja-squad:springmockk:4.0.2")
testImplementation("com.epages:restdocs-api-spec-openapi3-generator:0.17.1")
implementation("io.netty:netty-resolver-dns-native-macos:4.1.97.Final:osx-aarch_64")

implementation("org.springdoc:springdoc-openapi-starter-webflux-ui:2.1.0")
implementation("org.springframework.boot:spring-boot-starter-data-redis-reactive")
implementation("io.projectreactor.kotlin:reactor-kotlin-extensions")
}

tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.yedongsoon.example_project.application.couple

import com.yedongsoon.example_project.application.exception.CoupleExistException
import com.yedongsoon.example_project.application.exception.CoupleNotFoundException
import com.yedongsoon.example_project.application.exception.InvalidInviteCodeException
import com.yedongsoon.example_project.domain.couple.Couple
import com.yedongsoon.example_project.domain.couple.CoupleRepository
import com.yedongsoon.example_project.domain.extension.encodeDtoToBase64
import com.yedongsoon.example_project.domain.member.model.CoupleCreateCommand
import com.yedongsoon.example_project.domain.member.model.CoupleInfoCreateCommand
import com.yedongsoon.example_project.domain.member.model.CoupleInfoModifyCommand
import org.springframework.data.redis.core.ReactiveRedisTemplate
import org.springframework.data.redis.core.getAndAwait
import org.springframework.data.redis.core.setAndAwait
import org.springframework.stereotype.Service
import java.time.Duration
import java.time.LocalDateTime

@Service
class CoupleCommandService(
private val coupleRepository: CoupleRepository,
private val redisTemplate: ReactiveRedisTemplate<String, String>
) {
fun createCoupleInfo(command: CoupleInfoCreateCommand) {
coupleRepository.findByAccountNoAOrAccountNoB(command.memberNo, command.memberNo)?.let {
it.createInfo(command)
coupleRepository.save(it)
} ?: throw CoupleNotFoundException("커플 정보가 존재하지 않습니다")
}

suspend fun createCouple(command: CoupleCreateCommand) {
val memberNo = redisTemplate.opsForValue().getAndAwait(command.inviteCode)?.toInt()
?: throw InvalidInviteCodeException("유효하지 않은 초대코드입니다.")
coupleRepository.findByAccountNoAOrAccountNoB(memberNo, command.accountNoB)?.let {
throw CoupleExistException("이미 커플이 존재하는 회원입니다.")
}
coupleRepository.save(Couple.create(memberNo, command.accountNoB))
}

fun modifyCoupleInfo(memberNo: Int, command: CoupleInfoModifyCommand) {
coupleRepository.findByAccountNoAOrAccountNoB(memberNo, memberNo)?.let {
it.modify(command)
coupleRepository.save(it)
} ?: throw CoupleExistException("이미 커플이 존재하는 회원입니다.")


}

suspend fun createInviteCode(memberNo: Int): String {
val code = memberNo.toString() + LocalDateTime.now()
redisTemplate.opsForValue().setAndAwait(code.encodeDtoToBase64(), memberNo.toString(), Duration.ofMinutes(5))
return code.encodeDtoToBase64()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.yedongsoon.example_project.application.couple

import com.yedongsoon.example_project.application.exception.CoupleNotFoundException
import com.yedongsoon.example_project.application.exception.MemberNotFoundException
import com.yedongsoon.example_project.domain.couple.Couple
import com.yedongsoon.example_project.domain.couple.CoupleRepository
import com.yedongsoon.example_project.domain.member.Member
import com.yedongsoon.example_project.domain.member.MemberRepository
import org.springframework.data.crossstore.ChangeSetPersister
import org.springframework.stereotype.Service


@Service
class CoupleQueryService(
private val coupleRepository: CoupleRepository,
private val memberRepository: MemberRepository,
) {
fun getDetail(accountNo: Int): Couple {
return coupleRepository.findByAccountNoAOrAccountNoB(accountNo, accountNo)
?: throw ChangeSetPersister.NotFoundException()
}

fun getLover(accountNo: Int): Member {
val loverNo = coupleRepository.findByAccountNoAOrAccountNoB(accountNo, accountNo).let {
if (it?.accountNoA == accountNo) it.accountNoB else it?.accountNoA
} ?: throw CoupleNotFoundException("커플이 존재하지 않습니다.")


return memberRepository.findByNo(loverNo) ?: throw MemberNotFoundException("사용자가 존재하지 않습니다.")
}
}

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.yedongsoon.example_project.application.exception

import org.springframework.http.HttpStatus

class CoupleNotFoundException(message: String) : AbstractException(HttpStatus.NOT_FOUND, message)
class CoupleExistException(message: String) : AbstractException(HttpStatus.BAD_REQUEST, message)
class InvalidInviteCodeException(message: String) : AbstractException(HttpStatus.BAD_REQUEST, message)

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.yedongsoon.example_project.application.exception

import org.springframework.http.HttpStatus

class MemberAdditionalInfoExistsException(message: String) : AbstractException(HttpStatus.BAD_REQUEST, message)
class MemberNotFoundException(message: String) : AbstractException(HttpStatus.NOT_FOUND, message)

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.yedongsoon.example_project.application.member


import com.yedongsoon.example_project.application.exception.MemberAdditionalInfoExistsException
import com.yedongsoon.example_project.application.exception.MemberNotFoundException
import com.yedongsoon.example_project.domain.member.MemberRepository
import com.yedongsoon.example_project.domain.member.model.MemberAdditionalInfoCommand
import com.yedongsoon.example_project.domain.member.model.MemberInfoModifyCommand
import jakarta.transaction.Transactional
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service


@Service
class MemberCommandService(
private val memberRepository: MemberRepository,
) {
@Transactional
suspend fun createAdditional(command: MemberAdditionalInfoCommand) {
memberRepository.findByIdOrNull(command.memberNo)?.also {
if (it.birthDate != null && it.mobileNo != null) throw MemberAdditionalInfoExistsException("추가 정보를 이미 입력한 유저입니다.")
it.createAdditionalInfo(command)
memberRepository.save(it) //트랜잭션 수정후 제거
} ?: throw throw MemberNotFoundException("유저를 찾을 수 없습니다")
}

@Transactional
suspend fun modifyMemberInfo(command: MemberInfoModifyCommand) {
memberRepository.findByIdOrNull(command.memberNo)?.also {
it.modify(command)
memberRepository.save(it) //트랜잭션 수정후 제거
} ?: throw throw MemberNotFoundException("유저를 찾을 수 없습니다")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.yedongsoon.example_project.application.member

import com.yedongsoon.example_project.application.exception.MemberNotFoundException
import com.yedongsoon.example_project.domain.member.Member
import com.yedongsoon.example_project.domain.member.MemberRepository
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service

@Service
class MemberQueryService(
private val memberRepository: MemberRepository,
) {
suspend fun getMember(memberNo: Int): Member {
return memberRepository.findByIdOrNull(memberNo) ?: throw throw MemberNotFoundException("유저를 찾을 수 없습니다")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.yedongsoon.example_project.domain.couple

import com.yedongsoon.example_project.domain.member.model.CoupleInfoCreateCommand
import com.yedongsoon.example_project.domain.member.model.CoupleInfoModifyCommand
import jakarta.persistence.*
import java.time.LocalDate
import java.time.LocalDateTime

@Entity
@Table(name = "couple")
class Couple(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "couple_no")
val no: Int = 0,

@Column(name = "account_no_a")
val accountNoA: Int,

@Column(name = "account_no_b")
val accountNoB: Int,

) {
@Column(name = "couple_name")
var name: String? = null
private set

@Column(name = "nick_name_a")
var nickNameA: String? = null
private set

@Column(name = "nick_name_b")
var nickNameB: String? = null
private set

@Column(name = "started_at")
var startedAt: LocalDate? = null
private set

@Column(name = "couple_img")
var coupleImg: String? = null
private set

@Column(name = "created_at")
val createdAt: LocalDateTime = LocalDateTime.now()

fun createInfo(command: CoupleInfoCreateCommand) {
name = command.name
nickNameA = command.nickNameA
nickNameB = command.nickNameB
startedAt = command.startedAt
coupleImg = command.coupleImg
}

fun modify(command: CoupleInfoModifyCommand) {
if (command.name != null) name = command.name
if (command.nickNameA != null) nickNameA = command.nickNameA
if (command.nickNameB != null) nickNameB = command.nickNameB
if (command.startedAt != null) startedAt = command.startedAt
if (command.coupleImg != null) coupleImg = command.coupleImg
}

companion object {
fun create(accountNoA: Int, accountNoB: Int) = Couple(
accountNoA = accountNoA,
accountNoB = accountNoB
)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.yedongsoon.example_project.domain.couple

import org.springframework.data.jpa.repository.JpaRepository

interface CoupleRepository : JpaRepository<Couple, Int> {
fun findByAccountNoAOrAccountNoB(accountNoA: Int, accountNoB: Int): Couple?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.yedongsoon.example_project.domain.member

import com.yedongsoon.example_project.domain.member.model.MemberAdditionalInfoCommand
import com.yedongsoon.example_project.domain.member.model.MemberInfoModifyCommand
import jakarta.persistence.*
import java.time.LocalDate
import java.time.LocalDateTime

@Entity
@Table(name = "member")
class Member(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "member_no")
val no: Int = 0,

@Column(name = "account_id")
val accountId: String?,

name: String?,

email: String?,

profilePhoto: String?,

birthDate: LocalDate?,

mobileNo: String?,
) {
@Column(name = "created_at")
val createdAt: LocalDateTime = LocalDateTime.now()

@Column(name = "name")
var name: String? = name
private set

@Column(name = "email")
var email: String? = email
private set

@Column(name = "profile_photo")
var profilePhoto: String? = profilePhoto
private set

@Column(name = "birth_date")
var birthDate: LocalDate? = birthDate
private set

@Column(name = "mobile_no")
var mobileNo: String? = mobileNo
private set

fun createAdditionalInfo(command: MemberAdditionalInfoCommand) {
name = command.name ?: name
profilePhoto = command.profilePhoto
birthDate = command.birthDate
mobileNo = command.mobileNo
}

fun modify(command: MemberInfoModifyCommand) {
name = command.name
email = command.email
profilePhoto = command.profilePhoto
birthDate = command.birthDate
mobileNo = command.mobileNo
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.yedongsoon.example_project.domain.member

import org.springframework.data.jpa.repository.JpaRepository


interface MemberRepository : JpaRepository<Member, Int> {
fun findByNo(no: Int): Member?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.yedongsoon.example_project.domain.member.model

data class CoupleCreateCommand(
val inviteCode: String,
val accountNoB: Int,
)
Loading

0 comments on commit 020b6ff

Please sign in to comment.