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] 프로필 조회 구현 #22

Merged
merged 3 commits into from
Jul 23, 2024
Merged
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
Expand Up @@ -4,6 +4,7 @@ import com.nexters.bottles.user.facade.dto.ProfileChoiceResponseDto
import com.nexters.bottles.user.facade.dto.RegisterIntroductionRequestDto
import com.nexters.bottles.user.facade.dto.RegisterProfileRequestDto
import com.nexters.bottles.user.facade.UserProfileFacade
import com.nexters.bottles.user.facade.dto.UserProfileResponseDto
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
Expand All @@ -30,4 +31,9 @@ class UserProfileController(
fun upsertIntroduction(@RequestBody registerIntroductionRequestDto: RegisterIntroductionRequestDto) {
profileFacade.upsertIntroduction(registerIntroductionRequestDto)
}

@GetMapping
fun getProfile(): UserProfileResponseDto {
return profileFacade.getProfile()
}
}
3 changes: 3 additions & 0 deletions src/main/kotlin/com/nexters/bottles/user/domain/User.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.global.BaseEntity
import com.nexters.bottles.user.domain.enum.Gender
import java.time.LocalDate
import javax.persistence.*

@Entity
Expand All @@ -10,6 +11,8 @@ class User(
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long = 0,

var birthdate: LocalDate? = null,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

생년월일을 저장하고 나이가 필요하면 계산해서 줘야겠네요

Copy link
Member

Choose a reason for hiding this comment

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

이거 카카오에서 주는 정보가 생년월일 밖에 없어서 그런건가요??

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

앗 아뇨! 나이를 저장하면 두 가지 문제가 있을거 같아요.

  • 매년 해가 바뀔때마다 1씩 증가시켜줘야함
  • 만 나이 제도를 적용하고 싶을 때 유연하지 않음

그래서 생년월일을 저장하는게 훨씬 유리할거 같아요!


var name: String? = null,

var kakaoId: String? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import com.nexters.bottles.user.facade.dto.ProfileChoiceResponseDto
import com.nexters.bottles.user.facade.dto.RegisterIntroductionRequestDto
import com.nexters.bottles.user.facade.dto.RegisterProfileRequestDto
import com.nexters.bottles.user.domain.UserProfileSelect
import com.nexters.bottles.user.facade.dto.UserProfileResponseDto
import com.nexters.bottles.user.service.UserProfileService
import mu.KotlinLogging
import org.springframework.stereotype.Component
import regions

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

private val log = KotlinLogging.logger { }

fun upsertProfile(profileDto: RegisterProfileRequestDto) {
validateProfile(profileDto)
val convertedProfileDto = convertProfileDto(profileDto)
Expand Down Expand Up @@ -43,6 +47,17 @@ class UserProfileFacade(
profileService.saveIntroduction(registerIntroductionRequestDto.introduction)
}

fun getProfile(): UserProfileResponseDto {

val userProfile = profileService.findUserProfile(1L) // TODO: 회원 기능 구현후 수정
return UserProfileResponseDto(
userName = "테스트",
age = 20,
introduction = userProfile?.introduction,
profileSelect = userProfile?.profileSelect
)
}

private fun validateProfile(profileDto: RegisterProfileRequestDto) {
require(profileDto.keyword.size <= 5) {
"키워드는 5개 이하여야 해요"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.nexters.bottles.user.facade.dto

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

data class UserProfileResponseDto(
val userName: String,
val age: Int,
val introduction: List<QuestionAndAnswer>? = null,
val profileSelect: UserProfileSelect? = null,
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ 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 @@ -26,7 +26,6 @@ class UserProfileService(
profileRepository.findByUserId(user.id)?.let {
it.user = user
it.profileSelect = profileSelect
it.introduction = it.introduction
} ?: run {
profileRepository.save(
UserProfile(
Expand All @@ -53,4 +52,9 @@ class UserProfileService(
)
}
}

@Transactional(readOnly = true)
fun findUserProfile(userId: Long): UserProfile? {
return profileRepository.findByUserId(userId)
Copy link
Member

Choose a reason for hiding this comment

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

오 저도 오랜만에 해서 까먹었었는데 생각해보니까
profileRepository.findByUser(user)
이렇게 id로 찾지 않고 객체로 찾아왔던 것 같아요!
인준님이 물어보셨던 객체지향스럽게 짜는게 저런게 아닌가 생각이 들었어요! ㅋㅋㅋ

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

아하 그쵸 맞아요 그랬던 기억이 쪼끔나서 여쭤봤었어요 ㅋㅋ

}
}
12 changes: 12 additions & 0 deletions src/main/kotlin/com/nexters/bottles/user/service/UserService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.nexters.bottles.user.service

import com.nexters.bottles.user.domain.User
import com.nexters.bottles.user.repository.UserRepository
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
class UserService(
private val userRepository: UserRepository,
) {
}
16 changes: 8 additions & 8 deletions src/main/resources/sql/table_query.sql
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
CREATE TABLE User
(
id BIGINT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) DEFAULT NULL,
kakao_id VARCHAR(255) DEFAULT NULL,
CREATE TABLE User (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) DEFAULT NULL,
birthdate DATE DEFAULT NULL,
kakao_id VARCHAR(255) DEFAULT NULL,
phone_number VARCHAR(255) DEFAULT NULL,
gender VARCHAR(10) DEFAULT 'MALE',
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
gender VARCHAR(10) DEFAULT 'MALE',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL
);

CREATE TABLE user_profile
Expand Down
Loading