Skip to content

Commit

Permalink
Merge pull request #4 from HappyScrolls/feature/#1_user_additional_infos
Browse files Browse the repository at this point in the history
Feature/#1 user additional infos
  • Loading branch information
chs98412 authored Sep 9, 2024
2 parents 0b459bc + 47a09fe commit 47591a0
Show file tree
Hide file tree
Showing 14 changed files with 210 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.yedongsoon.account_service.application
package com.yedongsoon.account_service.application.couple

import com.yedongsoon.account_service.domain.couple.Couple
import com.yedongsoon.account_service.domain.couple.CoupleRepository
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.yedongsoon.account_service.application.exception

import org.springframework.http.HttpStatus

abstract class AbstractException(
val status: HttpStatus,
override val message: String
) : RuntimeException(message)
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.yedongsoon.account_service.application.exception

import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.ControllerAdvice
import org.springframework.web.bind.annotation.ExceptionHandler
import reactor.core.publisher.Mono

@ControllerAdvice
class GlobalExceptionHandler {

@ExceptionHandler(AbstractException::class)
fun handleCustomException(ex: AbstractException): Mono<ResponseEntity<String>> {
return Mono.just(
ResponseEntity
.status(ex.status)
.body(ex.message)
)
}

@ExceptionHandler(Exception::class)
fun handleGeneralException(ex: Exception): Mono<ResponseEntity<String>> {
return Mono.just(
ResponseEntity
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body("An unexpected error occurred: ${ex.message}")
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.yedongsoon.account_service.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,24 @@
package com.yedongsoon.account_service.application.member

import com.yedongsoon.account_service.application.exception.MemberAdditionalInfoExistsException
import com.yedongsoon.account_service.application.exception.MemberNotFoundException
import com.yedongsoon.account_service.domain.member.MemberRepository
import com.yedongsoon.account_service.domain.member.model.MemberAdditionalInfoCommand
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("유저를 찾을 수 없습니다")
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.yedongsoon.account_service.domain.member

import com.yedongsoon.account_service.domain.member.model.MemberAdditionalInfoCommand
import jakarta.persistence.*
import java.time.LocalDate
import java.time.LocalDateTime
@Entity
@Table(name = "member")
Expand All @@ -13,24 +15,36 @@ class Member(
@Column(name = "account_id")
val accountId: String?,

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

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

@Column(name = "profile_photo")
val profilePhoto: String?,
profilePhoto: String?,

birthDate: LocalDate?,

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

companion object {
fun create(accountId: String,name: String,email: String,profilePhoto:String) = Member(
accountId=accountId,
name = name,
email = email,
profilePhoto=profilePhoto,
)
@Column(name = "name")
var name: String?=name
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
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.yedongsoon.account_service.domain.member.model

import java.time.LocalDate

data class MemberAdditionalInfoCommand(
val memberNo:Int,
val name: String?,
val mobileNo:String,
val birthDate: LocalDate,
val profilePhoto: String?,
//추후 개인정보 동의 여부 추가
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.yedongsoon.account_service.presentation

import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.web.cors.CorsConfiguration
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource
import org.springframework.web.cors.reactive.CorsWebFilter

@Configuration
class WebFluxConfig {

@Bean
fun corsFilter(): CorsWebFilter {
val corsConfig = CorsConfiguration()
corsConfig.allowedOrigins = listOf("*") // 모든 도메인 허용
corsConfig.allowedMethods = listOf("*") // 모든 HTTP 메서드 허용
corsConfig.allowedHeaders = listOf("*") // 모든 헤더 허용
corsConfig.allowCredentials = false // 자격 증명 비허용

val source = UrlBasedCorsConfigurationSource()
source.registerCorsConfiguration("/**", corsConfig) // 모든 경로에 대해 CORS 설정 적용

return CorsWebFilter(source)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.yedongsoon.account_service.presentation.handler

import com.yedongsoon.account_service.application.CoupleQueryService
import com.yedongsoon.account_service.application.couple.CoupleQueryService
import com.yedongsoon.account_service.presentation.extension.extractMemberCodeHeader
import com.yedongsoon.account_service.presentation.handler.model.CoupleResponse
import com.yedongsoon.account_service.presentation.handler.model.MemberResponse
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.yedongsoon.account_service.presentation.handler

import com.yedongsoon.account_service.application.member.MemberCommandService
import com.yedongsoon.account_service.presentation.extension.extractMemberCodeHeader
import com.yedongsoon.account_service.presentation.handler.model.CoupleResponse
import com.yedongsoon.account_service.presentation.handler.model.MemberAdditionalInfoRequest
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.springframework.stereotype.Service
import org.springframework.web.reactive.function.server.*

@Service
class MemberHandler(
private val memberCommandService: MemberCommandService,
) {
suspend fun createAdditional(request: ServerRequest): ServerResponse = withContext(Dispatchers.IO) {
val memberHeader = request.extractMemberCodeHeader()
val command = request.awaitBodyOrNull<MemberAdditionalInfoRequest>()?.toCommand(memberHeader.no)
?: throw IllegalArgumentException()

memberCommandService.createAdditional(command)
ServerResponse.ok().buildAndAwait()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.yedongsoon.account_service.presentation.handler.model

import com.yedongsoon.account_service.domain.member.model.MemberAdditionalInfoCommand
import java.time.LocalDate


data class MemberAdditionalInfoRequest(
val name: String?,
val mobileNo:String,
val birthDate: LocalDate,
val profilePhoto: String?,
//추후 개인정보 동의 여부 추가
){
fun toCommand(no:Int)= MemberAdditionalInfoCommand(
memberNo =no,
name=name,
mobileNo=mobileNo,
birthDate=birthDate,
profilePhoto=profilePhoto,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class CoupleRouter(private val coupleHandler: CoupleHandler) {
@Bean
fun coupleRoute(): RouterFunction<ServerResponse> {
return coRouter {
(accept(MediaType.APPLICATION_JSON) and "/couple").nest {
(accept(MediaType.APPLICATION_JSON) and "/account-service/couple").nest {
GET("/detail", coupleHandler::getDetail)
GET("/lover", coupleHandler::getLover)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.yedongsoon.account_service.presentation.router

import com.yedongsoon.account_service.presentation.handler.MemberHandler
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.http.MediaType
import org.springframework.web.reactive.function.server.RouterFunction
import org.springframework.web.reactive.function.server.ServerResponse
import org.springframework.web.reactive.function.server.coRouter

@Configuration
class MemberRouter(private val memberHandler: MemberHandler) {
@Bean
fun memberRoute(): RouterFunction<ServerResponse> {
return coRouter {
(accept(MediaType.APPLICATION_JSON) and "/account-service/member").nest {
POST("/additional-info", memberHandler::createAdditional)
}
}
}
}
11 changes: 11 additions & 0 deletions src/test/http/member.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
### 추가 정보 기입
POST {{url}}/account-service/member/additional-info
Content-Type: application/json
Member-Code: eyJubyI6MSwibmFtZSI6Im5hbWUiLCJhY2NvdW50IjoiYWNjb3VudCJ9

{
"name" : null,
"mobileNo": "123123123",
"birthDate":"2024-01-01",
"profilePhoto": null
}

0 comments on commit 47591a0

Please sign in to comment.