Skip to content

Commit

Permalink
feat: 회원 정보 조회 & 수정 api
Browse files Browse the repository at this point in the history
feat: 회원 정보 조회 & 수정 api
  • Loading branch information
Woonseok105 authored Oct 24, 2023
2 parents 1bf6166 + 9787912 commit c59113d
Show file tree
Hide file tree
Showing 13 changed files with 177 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@ object PasswordMismatchException : CustomException(

object UserAlreadyExist : CustomException(
DomainErrorCode.ALREADY_USER_EXIST
)

object AttendanceNotFound : CustomException(
DomainErrorCode.ATTENDANCE_NOT_FOUND
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package kr.hs.dsm.inq.domain.user.persistence

import java.time.LocalDate
import javax.persistence.*

@Table(name = "tbl_attendence")
@Entity
class Attendance (

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Long,

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", columnDefinition = "BIGINT", nullable = false)
val user: User,

var monday: Boolean = false,

var tuesday: Boolean = false,

var wednesday: Boolean = false,

var thursday: Boolean = false,

var friday: Boolean = false,

var saturday: Boolean = false,

var sunday: Boolean = false,
)
34 changes: 28 additions & 6 deletions src/main/kotlin/kr/hs/dsm/inq/domain/user/persistence/User.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package kr.hs.dsm.inq.domain.user.persistence

import kr.hs.dsm.inq.domain.user.presentation.dto.UpdateUserInfoRequest
import java.time.LocalDate
import javax.persistence.Column
import javax.persistence.Entity
import javax.persistence.GeneratedValue
import javax.persistence.GenerationType
Expand All @@ -9,17 +12,36 @@ import javax.persistence.Table
@Table(name = "tbl_user")
@Entity
data class User(

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
var id: Long = 0L,

val username: String,
@Column(nullable = false)
var username: String,

@Column(nullable = false)
var job: String,

@Column(columnDefinition = "INT", nullable = false)
var jobDuration: Int,

@Column(nullable = false)
var accountId: String,

val job: String,
@Column(nullable = false)
var password: String,

val jobDuration: Int,
@Column(nullable = false)
val joinDate: LocalDate,

val accountId: String,
@Column(columnDefinition = "INT", nullable = false)
var coin: Int = 0
) {

val password: String
)
fun updateInfo(request: UpdateUserInfoRequest) {
this.username = request.username
this.job = request.job
this.jobDuration = request.jobDuration
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package kr.hs.dsm.inq.domain.user.persistence.repository

import kr.hs.dsm.inq.domain.user.persistence.Attendance
import org.springframework.data.repository.CrudRepository

interface AttendanceRepository : CrudRepository<Attendance, Long> {
fun findAllByUserId(userId: Long): List<Attendance>?
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package kr.hs.dsm.inq.domain.user.persistence
package kr.hs.dsm.inq.domain.user.persistence.repository

import kr.hs.dsm.inq.domain.user.persistence.User
import org.springframework.data.repository.CrudRepository

interface UserRepository: CrudRepository<User, Long> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ package kr.hs.dsm.inq.domain.user.presentation

import javax.validation.Valid
import kr.hs.dsm.inq.common.dto.TokenResponse
import kr.hs.dsm.inq.domain.user.presentation.dto.request.UserSignInRequest
import kr.hs.dsm.inq.domain.user.presentation.dto.request.UserSignUpRequest
import kr.hs.dsm.inq.domain.user.presentation.dto.UpdateUserInfoRequest
import kr.hs.dsm.inq.domain.user.presentation.dto.UserInfoResponse
import kr.hs.dsm.inq.domain.user.presentation.dto.UserSignInRequest
import kr.hs.dsm.inq.domain.user.presentation.dto.UserSignUpRequest
import kr.hs.dsm.inq.domain.user.service.UserService
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.PutMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.ResponseStatus
Expand All @@ -27,4 +31,14 @@ class UserController(
@PostMapping("/auth")
fun userSignIn(@Valid @RequestBody request: UserSignInRequest): TokenResponse =
userService.signIn(request)

@GetMapping("/profile")
fun queryUserInfo(): UserInfoResponse =
userService.queryUserInfo()

@PutMapping("/profile")
fun updateUserInfo(@RequestBody request: UpdateUserInfoRequest) {
userService.updateUserInfo(request)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package kr.hs.dsm.inq.domain.user.presentation.dto

import javax.validation.constraints.NotBlank

data class UserSignInRequest(

@field:NotBlank
val accountId: String,

@field:NotBlank
val password: String
)

data class UserSignUpRequest(

@field:NotBlank
val accountId: String,

@field:NotBlank
val username: String,

@field:NotBlank
val password: String,

@field:NotBlank
val job: String,

@field:NotBlank
val jobDuration: Int
)

data class UpdateUserInfoRequest(

@field:NotBlank
val username: String,

@field:NotBlank
val job: String,

@field:NotBlank
val jobDuration: Int
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package kr.hs.dsm.inq.domain.user.presentation.dto

import java.time.LocalDate

data class UserInfoResponse(
val username: String,
val joinDate: LocalDate,
val coin: Int,
val job: String,
val jobDuration: Int
)

This file was deleted.

This file was deleted.

39 changes: 33 additions & 6 deletions src/main/kotlin/kr/hs/dsm/inq/domain/user/service/UserService.kt
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
package kr.hs.dsm.inq.domain.user.service

import kr.hs.dsm.inq.common.dto.TokenResponse
import kr.hs.dsm.inq.common.util.SecurityUtil
import kr.hs.dsm.inq.domain.user.exception.AttendanceNotFound
import kr.hs.dsm.inq.domain.user.exception.PasswordMismatchException
import kr.hs.dsm.inq.domain.user.exception.UserAlreadyExist
import kr.hs.dsm.inq.domain.user.exception.UserNotFound
import kr.hs.dsm.inq.domain.user.persistence.User
import kr.hs.dsm.inq.domain.user.persistence.UserRepository
import kr.hs.dsm.inq.domain.user.presentation.dto.request.UserSignInRequest
import kr.hs.dsm.inq.domain.user.presentation.dto.request.UserSignUpRequest
import kr.hs.dsm.inq.domain.user.persistence.repository.AttendanceRepository
import kr.hs.dsm.inq.domain.user.persistence.repository.UserRepository
import kr.hs.dsm.inq.domain.user.presentation.dto.*
import kr.hs.dsm.inq.global.security.token.JwtGenerator
import org.springframework.security.crypto.password.PasswordEncoder
import org.springframework.stereotype.Service
import java.time.LocalDate
import javax.transaction.Transactional

@Service
class UserService(
private val userRepository: UserRepository,
private val passwordEncoder: PasswordEncoder,
private val jwtGenerator: JwtGenerator
private val jwtGenerator: JwtGenerator,
private val attendanceRepository: AttendanceRepository
) {

fun signIn(request: UserSignInRequest): TokenResponse {
Expand All @@ -38,12 +43,34 @@ class UserService(

userRepository.save(
User(
username = request.userName,
username = request.username,
job = request.job,
jobDuration = request.jobDuration,
accountId = request.accountId,
password = passwordEncoder.encode(request.password)
password = passwordEncoder.encode(request.password),
joinDate = LocalDate.now()
)
)
}

fun queryUserInfo(): UserInfoResponse {

val user = SecurityUtil.getCurrentUser()

return UserInfoResponse(
username = user.username,
joinDate = user.joinDate,
coin = user.coin,
job = user.job,
jobDuration = user.jobDuration
)
}

@Transactional
fun updateUserInfo(request: UpdateUserInfoRequest) {

val user = SecurityUtil.getCurrentUser()

user.updateInfo(request)
}
}
1 change: 1 addition & 0 deletions src/main/kotlin/kr/hs/dsm/inq/global/error/ErrorCode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ enum class DomainErrorCode(
QUESTION_NOT_FOUND(ErrorStatus.NOT_FOUND, "Question Not Found", 2),
ANSWER_NOT_FOUND(ErrorStatus.NOT_FOUND, "Answer Not Found", 3),
TAG_NOT_FOUND(ErrorStatus.NOT_FOUND, "Tag Not Found", 4),
ATTENDANCE_NOT_FOUND(ErrorStatus.NOT_FOUND, "Attendance Not Found", 5),

ALREADY_LIKED_POST(ErrorStatus.CONFLICT, "Already liked post", 1),
ALREADY_DISLIKED_POST(ErrorStatus.CONFLICT, "Already disliked post", 2),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package kr.hs.dsm.inq.global.security.principle

import kr.hs.dsm.inq.domain.user.persistence.UserRepository
import kr.hs.dsm.inq.domain.user.persistence.repository.UserRepository
import kr.hs.dsm.inq.global.security.exception.InvalidTokenException
import org.springframework.data.repository.findByIdOrNull
import org.springframework.security.core.userdetails.UserDetails
Expand Down

0 comments on commit c59113d

Please sign in to comment.