Skip to content

Commit

Permalink
#12 feat: 사용자 주제 생성 및 res user type
Browse files Browse the repository at this point in the history
  • Loading branch information
chaerlo127 committed Jul 26, 2023
1 parent 1fdeb59 commit 071a3af
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ enum class BaseResponseCode(status: HttpStatus, message: String) {
EXISTS_NICKNAME(HttpStatus.BAD_REQUEST, "이미 가입되어 있는 닉네임입니다."),

// User - type
INVALID_USER_TYPE_NAME(HttpStatus.BAD_REQUEST, "올바르지 않은 사용자 역할입니다.");
INVALID_USER_TYPE_NAME(HttpStatus.BAD_REQUEST, "올바르지 않은 사용자 역할입니다."),
INVALID_USER_CATEGORY(HttpStatus.BAD_REQUEST, "올바르지 않은 사용자 카테고리입니다."),

// User - category
INVALID_USER_INTEREST_COUNT(HttpStatus.BAD_REQUEST, "사용자 관심 주제는 1개이상, 3개 이하여야하며, 중복된 값이 포함되어 있지 않아야 합니다");

val status: HttpStatus = status
val message: String = message
Expand Down
4 changes: 3 additions & 1 deletion src/main/kotlin/com/psr/psr/global/jwt/dto/TokenRes.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package com.psr.psr.global.jwt.dto

data class TokenRes(val accessToken: String, val refreshToken: String)
import com.psr.psr.user.entity.Type

data class TokenRes(val accessToken: String, val refreshToken: String, val type: String)
5 changes: 3 additions & 2 deletions src/main/kotlin/com/psr/psr/global/jwt/utils/JwtUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.psr.psr.global.exception.BaseException
import com.psr.psr.global.exception.BaseResponseCode
import com.psr.psr.global.jwt.UserDetailsServiceImpl
import com.psr.psr.global.jwt.dto.TokenRes
import com.psr.psr.user.entity.Type
import io.jsonwebtoken.*
import io.jsonwebtoken.io.Decoders
import io.jsonwebtoken.security.Keys
Expand Down Expand Up @@ -39,7 +40,7 @@ class JwtUtils(
/**
* 토큰 생성
*/
fun createToken(authentication: Authentication): TokenRes {
fun createToken(authentication: Authentication, type: Type): TokenRes {
val authorities = authentication.authorities.stream()
.map { obj: GrantedAuthority -> obj.authority }
.collect(Collectors.joining(","))
Expand All @@ -57,7 +58,7 @@ class JwtUtils(
.signWith(key, SignatureAlgorithm.HS512)
.compact()

return TokenRes(BEARER_PREFIX + accessToken, BEARER_PREFIX + refreshToken)
return TokenRes(BEARER_PREFIX + accessToken, BEARER_PREFIX + refreshToken, type.value)
}

/**
Expand Down
20 changes: 16 additions & 4 deletions src/main/kotlin/com/psr/psr/user/dto/SignUpReq.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.psr.psr.user.dto

import com.psr.psr.user.entity.Provider
import com.psr.psr.user.entity.Type
import com.psr.psr.user.entity.User
import com.psr.psr.user.entity.*
import java.util.stream.Collector
import java.util.stream.Collectors




data class SignUpReq (
val email: String,
Expand All @@ -11,7 +14,8 @@ data class SignUpReq (
val phone: String,
val nickname: String,
val marketing: Boolean,
val notification: Boolean
val notification: Boolean,
val interestList: List<UserInterestReq>
) {
fun toEntity(): User {
return User(email = email,
Expand All @@ -23,4 +27,12 @@ data class SignUpReq (
notification = notification,
nickname = nickname)
}

fun toInterestEntity(user: User): List<UserInterest> {
return interestList.stream()
.map { i ->
UserInterest(category = Category.getCategoryByName(i.category),
user = user)
}.collect(Collectors.toList())
}
}
12 changes: 12 additions & 0 deletions src/main/kotlin/com/psr/psr/user/dto/UserInterestReq.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.psr.psr.user.dto

import com.psr.psr.user.entity.Category

data class UserInterestReq (
val category: String
){
fun checkInterestCategory() : Category{
return Category.getCategoryByName(category)
}

}
12 changes: 11 additions & 1 deletion src/main/kotlin/com/psr/psr/user/entity/Category.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.psr.psr.user.entity

import com.psr.psr.global.exception.BaseException
import com.psr.psr.global.exception.BaseResponseCode

enum class Category(val value: String) {
BROADCAST_PRODUCT("방송가능 상품소싱"),
SHOW_HOST_ADVERTISE("쇼호스트 구인"),
Expand All @@ -9,5 +12,12 @@ enum class Category(val value: String) {
VIDEO_EDITING("영상편집"),
INSTRUCTOR_MATCHING("강사매칭"),
SNS_MARKETING("SNS 마케팅"),
PROMOTION_DESIGN("홍보물 디자인")
PROMOTION_DESIGN("홍보물 디자인");

companion object {
fun getCategoryByName(name: String): Category {
return enumValues<Category>().find { it.value == name }
?: throw BaseException(BaseResponseCode.INVALID_USER_CATEGORY)
}
}
}
2 changes: 1 addition & 1 deletion src/main/kotlin/com/psr/psr/user/entity/UserInterest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import org.jetbrains.annotations.NotNull
@Entity
data class UserInterest(
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
var id: Long,
var id: Long? = null,

@ManyToOne
@JoinColumn(nullable = false, name = "user_id")
Expand Down
21 changes: 19 additions & 2 deletions src/main/kotlin/com/psr/psr/user/service/UserService.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.psr.psr.user.service

import com.psr.psr.global.Constant
import com.fasterxml.jackson.databind.ser.Serializers.Base
import com.psr.psr.global.Constant.User.User.EMAIL_VALIDATION
import com.psr.psr.global.Constant.User.User.PASSWORD_VALIDATION
import com.psr.psr.global.Constant.User.User.PHONE_VALIDATION
Expand All @@ -9,29 +9,44 @@ import com.psr.psr.global.exception.BaseResponseCode
import com.psr.psr.global.jwt.dto.TokenRes
import com.psr.psr.global.jwt.utils.JwtUtils
import com.psr.psr.user.dto.SignUpReq
import com.psr.psr.user.entity.Category
import com.psr.psr.user.entity.User
import com.psr.psr.user.entity.UserInterest
import com.psr.psr.user.repository.UserInterestRepository
import com.psr.psr.user.repository.UserRepository
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder
import org.springframework.security.crypto.password.PasswordEncoder
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import java.util.regex.Pattern
import java.util.stream.Collectors

@Service
@Transactional(readOnly = true)
class UserService(
private val userRepository: UserRepository,
private val userInterestRepository: UserInterestRepository,
private val authenticationManagerBuilder: AuthenticationManagerBuilder,
private val jwtUtils: JwtUtils,
private val passwordEncoder: PasswordEncoder

) {
// 회원가입
@Transactional
fun signUp(signUpReq: SignUpReq): TokenRes {
// 이메일의 형식이 맞는지 확인
if(!isValidRegularExpression(signUpReq.email, EMAIL_VALIDATION)) throw BaseException(BaseResponseCode.INVALID_EMAIL)
// 비밀번호의 형식이 맞는지 확인
if(!isValidRegularExpression(signUpReq.password, PASSWORD_VALIDATION)) throw BaseException(BaseResponseCode.INVALID_PASSWORD)
// 휴대폰 번호의 형식이 맞는지 확인
if(!isValidRegularExpression(signUpReq.phone, PHONE_VALIDATION)) throw BaseException(BaseResponseCode.INVALID_PHONE)
// Category가 알맞은 이름을 갖고 있는지, 값이 중복되어있는지 확인
val categoryCheck = signUpReq.interestList.stream().map { i -> i.checkInterestCategory() }.collect(Collectors.toList()).groupingBy { it }.eachCount().any { it.value > 1 }
if(categoryCheck) throw BaseException(BaseResponseCode.INVALID_USER_INTEREST_COUNT)
// category 의 사이즈 확인
val listSize = signUpReq.interestList.size
if(listSize < 1 || listSize > 3) throw BaseException(BaseResponseCode.INVALID_USER_INTEREST_COUNT)

// 이미 가지고 있는 이메일, 닉네임, 휴대폰 번호인지 확인
if(userRepository.existsByEmail(signUpReq.email)) throw BaseException(BaseResponseCode.EXISTS_EMAIL)
Expand All @@ -46,6 +61,8 @@ class UserService(
signUpReq.password = encodedPassword
// user 저장
val user = userRepository.save(signUpReq.toEntity())
userInterestRepository.saveAll(signUpReq.toInterestEntity(user))

// token 생성
return createToken(user, password)
}
Expand All @@ -60,6 +77,6 @@ class UserService(
private fun createToken(user: User, password: String): TokenRes {
val authenticationToken = UsernamePasswordAuthenticationToken(user.id.toString(), password)
val authentication = authenticationManagerBuilder.`object`.authenticate(authenticationToken)
return jwtUtils.createToken(authentication)
return jwtUtils.createToken(authentication, user.type)
}
}

0 comments on commit 071a3af

Please sign in to comment.