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

[Feature/19] 내 프로필 조회하기를 구현한다 #20

Closed
wants to merge 2 commits into from
Closed
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
@@ -1,6 +1,8 @@
package com.nexters.bottles.user.controller

import com.nexters.bottles.user.controller.dto.ProfileChoiceResponseDto
import com.nexters.bottles.user.controller.dto.ProfileResponseDto
import com.nexters.bottles.user.controller.dto.RegisterIntroductionRequestDto
import com.nexters.bottles.user.controller.dto.RegisterProfileRequestDto
import com.nexters.bottles.user.facade.UserProfileFacade
import org.springframework.web.bind.annotation.GetMapping
Expand All @@ -10,18 +12,28 @@ import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/api/v1")
@RequestMapping("/api/v1/profile")
class UserProfileController(
private val profileFacade: UserProfileFacade,
) {

@PostMapping("/profile/choice")
fun registerProfile(@RequestBody registerProfileRequestDto: RegisterProfileRequestDto) {
profileFacade.saveProfile(registerProfileRequestDto)
@PostMapping("/choice")
fun upsertProfile(@RequestBody registerProfileRequestDto: RegisterProfileRequestDto) {
profileFacade.upsertProfile(registerProfileRequestDto)
}

@GetMapping("/profile/choice")
@GetMapping("/choice")
fun getProfileChoiceList() : ProfileChoiceResponseDto {
return profileFacade.getProfileChoice()
}

@PostMapping("/introduction")
fun upsertIntroduction(@RequestBody registerIntroductionRequestDto: RegisterIntroductionRequestDto) {
profileFacade.upsertIntroduction(registerIntroductionRequestDto)
}

@GetMapping
fun getProfile(): ProfileResponseDto {
return profileFacade.getProfile()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.nexters.bottles.user.controller.dto

import com.nexters.bottles.user.domain.QuestionAndAnswer
import com.nexters.bottles.user.domain.UserProfileSelect

data class ProfileResponseDto(
val profileSelect: UserProfileSelect?,
val introduction: List<QuestionAndAnswer>,
) {

companion object {

private val emptyResponse = ProfileResponseDto(
profileSelect = null,
introduction = emptyList()
)

fun ofEmpty(): ProfileResponseDto {
return emptyResponse
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.nexters.bottles.user.controller.dto

import com.nexters.bottles.user.domain.QuestionAndAnswer

data class RegisterIntroductionRequestDto(
val introduction: List<QuestionAndAnswer>
)
12 changes: 11 additions & 1 deletion src/main/kotlin/com/nexters/bottles/user/domain/UserProfile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.nexters.bottles.user.domain

import com.nexters.bottles.user.controller.dto.InterestDto
import com.nexters.bottles.user.controller.dto.RegionDto
import com.nexters.bottles.user.repository.converter.QuestionAndAnswerConverter
import com.nexters.bottles.user.repository.converter.UserProfileSelectConverter
import org.hibernate.annotations.CreationTimestamp
import org.hibernate.annotations.UpdateTimestamp
Expand All @@ -20,7 +21,11 @@ class UserProfile(

@Column(name = "profile_select")
@Convert(converter = UserProfileSelectConverter::class)
var profileSelect: UserProfileSelect,
var profileSelect: UserProfileSelect? = null,

@Column(name = "introduction")
@Convert(converter = QuestionAndAnswerConverter::class)
var introduction: List<QuestionAndAnswer> = arrayListOf(),

@Column(name = "created_at", updatable = false)
@CreationTimestamp
Expand All @@ -42,3 +47,8 @@ data class UserProfileSelect(
val region: RegionDto,
)

data class QuestionAndAnswer(
val question: String,
val answer: String,
)

Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.nexters.bottles.user.facade

import com.nexters.bottles.user.controller.dto.ProfileChoiceResponseDto
import com.nexters.bottles.user.controller.dto.ProfileResponseDto
import com.nexters.bottles.user.controller.dto.RegisterIntroductionRequestDto
import com.nexters.bottles.user.controller.dto.RegisterProfileRequestDto
import com.nexters.bottles.user.domain.UserProfile
import com.nexters.bottles.user.domain.UserProfileSelect
import com.nexters.bottles.user.service.UserProfileService
import mu.KotlinLogging
import org.springframework.stereotype.Component
import regions

Expand All @@ -13,21 +15,21 @@ class UserProfileFacade(
private val profileService: UserProfileService,
) {

fun saveProfile(profileDto: RegisterProfileRequestDto) {
private val log = KotlinLogging.logger { }

fun upsertProfile(profileDto: RegisterProfileRequestDto) {
validateProfile(profileDto)

profileService.saveProfile(
UserProfile(
profileSelect = UserProfileSelect(
mbti = profileDto.mbti,
keyword = profileDto.keyword,
interest = profileDto.interest,
job = profileDto.job,
smoking = profileDto.smoking,
alcohol = profileDto.alcohol,
religion = profileDto.religion,
region = profileDto.region,
)
profileService.upsertProfile(
profileSelect = UserProfileSelect(
mbti = profileDto.mbti,
keyword = profileDto.keyword,
interest = profileDto.interest,
job = profileDto.job,
smoking = profileDto.smoking,
alcohol = profileDto.alcohol,
religion = profileDto.religion,
region = profileDto.region,
)
)
}
Expand All @@ -38,6 +40,20 @@ class UserProfileFacade(
)
}

fun upsertIntroduction(registerIntroductionRequestDto: RegisterIntroductionRequestDto) {
validateIntroduction(registerIntroductionRequestDto)

profileService.saveIntroduction(registerIntroductionRequestDto.introduction)
}

fun getProfile() : ProfileResponseDto {
val userProfile = profileService.findProfile() ?: return ProfileResponseDto.ofEmpty()
return ProfileResponseDto(
profileSelect = userProfile.profileSelect,
introduction = userProfile.introduction,
)
}

private fun validateProfile(profileDto: RegisterProfileRequestDto) {
require(profileDto.keyword.size <= 5) {
"키워드는 5개 이하여야 해요"
Expand All @@ -48,4 +64,13 @@ class UserProfileFacade(
"취미는 5개 이하여야 해요"
}
}

private fun validateIntroduction(introductionDto: RegisterIntroductionRequestDto) {
introductionDto.introduction.forEach {
// require(it.answer.length > 30 && it.answer.length <= 100) {
// "소개는 30자 이상 100자 이하로 써야 해요"
// }
// TODO: 개발 환경에서 빠르게 테스트 하기 위해 일단 주석 처리하고 라이브 서비스 나가기전 해제할 예정입니다.
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ package com.nexters.bottles.user.repository
import com.nexters.bottles.user.domain.UserProfile
import org.springframework.data.jpa.repository.JpaRepository

interface UserProfileRepository : JpaRepository<UserProfile, Long>
interface UserProfileRepository : JpaRepository<UserProfile, Long> {

fun findByUserId(userId: Long): UserProfile?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.nexters.bottles.user.repository.converter

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.KotlinFeature
import com.fasterxml.jackson.module.kotlin.KotlinModule
import com.nexters.bottles.user.domain.QuestionAndAnswer
import javax.persistence.AttributeConverter
import javax.persistence.Converter

@Converter(autoApply = true)
class QuestionAndAnswerConverter : AttributeConverter<List<QuestionAndAnswer>, String> {

private val objectMapper = ObjectMapper().registerModule(
KotlinModule.Builder()
.withReflectionCacheSize(512)
.configure(KotlinFeature.NullToEmptyCollection, false)
.configure(KotlinFeature.NullToEmptyMap, false)
.configure(KotlinFeature.NullIsSameAsDefault, false)
.configure(KotlinFeature.StrictNullChecks, false)
.build()
)

override fun convertToDatabaseColumn(attribute: List<QuestionAndAnswer>?): String? {
return attribute?.let { objectMapper.writeValueAsString(it) }
}

override fun convertToEntityAttribute(dbData: String?): List<QuestionAndAnswer>? {
return dbData?.let {
objectMapper.readValue(it, objectMapper.typeFactory.constructCollectionType(List::class.java, QuestionAndAnswer::class.java))
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.nexters.bottles.user.service

import com.nexters.bottles.user.domain.QuestionAndAnswer
import com.nexters.bottles.user.domain.UserProfile
import com.nexters.bottles.user.domain.UserProfileSelect
import com.nexters.bottles.user.repository.UserProfileRepository
import com.nexters.bottles.user.repository.UserRepository
import mu.KotlinLogging
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service
import javax.transaction.Transactional
import org.springframework.transaction.annotation.Transactional

@Service
class UserProfileService(
Expand All @@ -17,10 +19,56 @@ class UserProfileService(
private val log = KotlinLogging.logger { }

@Transactional
fun saveProfile(userProfile: UserProfile): UserProfile {
val user = userRepository.findByIdOrNull(1L) // TODO User 회원 가입 기능 구현후 수정
fun upsertProfile(profileSelect: UserProfileSelect) {
// TODO User 회원 가입 기능 구현후 수정
val user = userRepository.findByIdOrNull(1L)
if (user != null) {
user.userProfile?.profileSelect = profileSelect

userProfile.user = user
return profileRepository.save(userProfile)
profileRepository.findByUserId(user.id)?.let {
it.user = user
it.profileSelect = it.profileSelect
it.introduction = it.introduction
} ?: run {
profileRepository.save(
UserProfile(
user = user,
profileSelect = profileSelect,
)
)
}
} else {
throw IllegalStateException ("회원가입 상태를 문의해주세요")
}
}

@Transactional
fun saveIntroduction(introduction: List<QuestionAndAnswer>) {
// TODO User 회원 가입 기능 구현후 수정
val user = userRepository.findByIdOrNull(1L)
if (user != null) {
user.userProfile?.introduction = introduction

profileRepository.findByUserId(user.id)?.let {
it.user = user
it.profileSelect = it.profileSelect
it.introduction = it.introduction
} ?: run {
profileRepository.save(
UserProfile(
user = user,
introduction = introduction
)
)
}
} else {
throw IllegalStateException ("회원가입 상태를 문의해주세요")
}
}

@Transactional(readOnly = true)
fun findProfile() : UserProfile? {
val user = userRepository.findByIdOrNull(1L) ?: return null
return profileRepository.findByUserId(user.id)
}
}
1 change: 1 addition & 0 deletions src/main/resources/sql/table_query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ CREATE TABLE user_profile (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
user_id BIGINT NOT NULL,
profile_select JSON,
introduction JSON,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL
);
Loading