Skip to content

Commit

Permalink
Merge pull request #24 from PSR-Co/feat/#23-profile
Browse files Browse the repository at this point in the history
[feat] 프로필 변경 API
  • Loading branch information
chaerlo127 authored Jul 30, 2023
2 parents 96bef4b + c8dac3d commit 306f3ab
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 3 deletions.
27 changes: 24 additions & 3 deletions src/main/kotlin/com/psr/psr/user/controller/UserController.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.psr.psr.user.controller

import com.psr.psr.global.dto.BaseResponse
import com.psr.psr.global.exception.BaseResponseCode
import com.psr.psr.global.jwt.UserAccount
import com.psr.psr.global.jwt.dto.TokenRes
import com.psr.psr.user.dto.LoginReq
import com.psr.psr.user.dto.SignUpReq
import com.psr.psr.user.dto.CheckNicknameReq
import com.psr.psr.user.dto.*
import com.psr.psr.user.service.UserService
import org.springframework.security.core.annotation.AuthenticationPrincipal
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.*

Expand Down Expand Up @@ -41,4 +42,24 @@ class UserController(
// 사용 가능 : True, 사용 불가 : False
return BaseResponse(!userService.checkDuplicateNickname(nicknameReq.nickname))
}

/**
* 사용자 프로필 불러오기
*/
@GetMapping("/profile")
@ResponseBody
fun getProfile(@AuthenticationPrincipal userAccount: UserAccount) : BaseResponse<ProfileRes>{
return BaseResponse(userService.getProfile(userAccount.getUser()))
}

/**
* 사용자 프로필 변경하기
*/
@PostMapping("/profile")
@ResponseBody
fun postProfile(@AuthenticationPrincipal userAccount: UserAccount, @RequestBody @Validated profileReq: ProfileReq) : BaseResponse<Any> {
userService.postProfile(userAccount.getUser(), profileReq)
return BaseResponse(BaseResponseCode.SUCCESS)
}

}
15 changes: 15 additions & 0 deletions src/main/kotlin/com/psr/psr/user/dto/ProfileReq.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.psr.psr.user.dto

import jakarta.validation.constraints.NotBlank
import jakarta.validation.constraints.Pattern


data class ProfileReq(
@field:NotBlank
@field:Pattern(
regexp = "^([a-zA-Z0-9ㄱ-ㅎ|ㅏ-ㅣ|가-힣]).{0,10}\$",
message = "한글, 영어, 숫자만 입력해주세요. (10글자)"
)
val nickname: String,
val profileImgKey: String? = null
)
7 changes: 7 additions & 0 deletions src/main/kotlin/com/psr/psr/user/dto/ProfileRes.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.psr.psr.user.dto


data class ProfileRes(
val email: String,
val imgKey: String? = null
)
4 changes: 4 additions & 0 deletions src/main/kotlin/com/psr/psr/user/dto/SignUpReq.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ data class SignUpReq (
@field:Nullable
val imgKey: String? = null,
@field:NotBlank
@field:Pattern(
regexp = "^([a-zA-Z0-9ㄱ-ㅎ|ㅏ-ㅣ|가-힣]).{0,10}\$",
message = "한글, 영어, 숫자만 입력해주세요. (10글자)"
)
val nickname: String,
val marketing: Boolean,
@field:NotNull
Expand Down
4 changes: 4 additions & 0 deletions src/main/kotlin/com/psr/psr/user/entity/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ package com.psr.psr.user.entity

import com.psr.psr.global.entity.BaseEntity
import jakarta.persistence.*
import org.hibernate.annotations.DynamicInsert
import org.hibernate.annotations.DynamicUpdate
import org.jetbrains.annotations.NotNull

@DynamicUpdate
@DynamicInsert
@Entity
class User(
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
18 changes: 18 additions & 0 deletions src/main/kotlin/com/psr/psr/user/service/UserService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import com.psr.psr.global.exception.BaseResponseCode.NOT_EXIST_EMAIL
import com.psr.psr.global.jwt.dto.TokenRes
import com.psr.psr.global.jwt.utils.JwtUtils
import com.psr.psr.user.dto.LoginReq
import com.psr.psr.user.dto.ProfileReq
import com.psr.psr.user.dto.ProfileRes
import com.psr.psr.user.dto.SignUpReq
import com.psr.psr.user.entity.User
import com.psr.psr.user.repository.UserInterestRepository
Expand Down Expand Up @@ -82,5 +84,21 @@ class UserService(
return jwtUtils.createToken(authentication, user.type)
}

// 사용자 프로필 불러오기
fun getProfile(user: User): ProfileRes {
return ProfileRes(user.email, user.imgKey)
}

// 사용자 프로필 변경
@Transactional
fun postProfile(user: User, profileReq: ProfileReq) {
if(user.nickname != profileReq.nickname) {
if(userRepository.existsByNickname(profileReq.nickname)) throw BaseException(BaseResponseCode.EXISTS_NICKNAME)
user.nickname = profileReq.nickname
}
if(user.imgKey != profileReq.profileImgKey) user.imgKey = profileReq.profileImgKey
userRepository.save(user)
}


}

0 comments on commit 306f3ab

Please sign in to comment.