Skip to content

Commit

Permalink
add :: modifyUserInfoService
Browse files Browse the repository at this point in the history
  • Loading branch information
meltapplee committed Oct 8, 2024
1 parent 8cf2b73 commit 04ad08f
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 23 deletions.
12 changes: 8 additions & 4 deletions src/main/kotlin/org/meogo/domain/user/domain/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,27 @@ class User(
id: UUID? = null,

@Column(nullable = false, length = 4)
val name: String,
var name: String,

@Column(name = "account_id", nullable = false, length = 15, unique = true)
val accountId: String,

val password: String,

@Column(name = "enrolled_school", nullable = true)
val enrolledSchool: Int? = 0,
var enrolledSchool: Int? = 0,

var profile: String,

@Enumerated(EnumType.STRING)
val role: UserRole
) : BaseUUIDEntity(id) {
fun updateProfile(profile: String): User {
this.profile = profile
fun updateProfile(name: String, enrolledSchool: Int, profile: String?): User {
this.name = name
this.enrolledSchool = enrolledSchool
if (profile != null) {
this.profile = profile
}
return this
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package org.meogo.domain.user.presentation

import org.meogo.domain.user.presentation.dto.request.UserCheckRequest
import org.meogo.domain.user.presentation.dto.request.UserModifyRequest
import org.meogo.domain.user.presentation.dto.request.UserSignInRequest
import org.meogo.domain.user.presentation.dto.request.UserSignUpRequest
import org.meogo.domain.user.presentation.dto.response.MyPageResponse
import org.meogo.domain.user.service.CheckAccountIdService
import org.meogo.domain.user.service.MyPageService
import org.meogo.domain.user.service.UploadProfileService
import org.meogo.domain.user.service.ModifyUserInfoService
import org.meogo.domain.user.service.QueryMyPageService
import org.meogo.domain.user.service.UserSignInService
import org.meogo.domain.user.service.UserSignUpService
import org.meogo.global.jwt.dto.TokenResponse
Expand All @@ -28,8 +29,8 @@ class UserController(
private val userSignUpService: UserSignUpService,
private val userSignInService: UserSignInService,
private val userCheckAccountIdService: CheckAccountIdService,
private val myPageService: MyPageService,
private val uploadProfileService: UploadProfileService
private val queryMyPageService: QueryMyPageService,
private val modifyUserInfoService: ModifyUserInfoService
) {
@PostMapping("/signup")
@ResponseStatus(HttpStatus.CREATED)
Expand All @@ -49,9 +50,13 @@ class UserController(

@GetMapping("/my")
fun myPage(): MyPageResponse =
myPageService.execute()
queryMyPageService.execute()

@PatchMapping("/profile")
fun updateProfile(@RequestPart(name = "image") file: MultipartFile) =
uploadProfileService.uploadProfile(file)
@PatchMapping("/modify")
fun updateProfile(
@RequestPart(name = "image")
file: MultipartFile?,
@RequestPart(name = "request")
request: UserModifyRequest
) = modifyUserInfoService.execute(request, file)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.meogo.domain.user.presentation.dto.request

data class UserModifyRequest(
val name: String,
val enrolledSchool: Int
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.meogo.domain.user.service
import org.meogo.domain.user.domain.UserRepository
import org.meogo.domain.user.exception.UserNotFoundException
import org.meogo.domain.user.facade.UserFacade
import org.meogo.domain.user.presentation.dto.request.UserModifyRequest
import org.meogo.global.s3.FileUtil
import org.meogo.global.s3.Path
import org.springframework.beans.factory.annotation.Value
Expand All @@ -11,22 +12,23 @@ import org.springframework.transaction.annotation.Transactional
import org.springframework.web.multipart.MultipartFile

@Service
class UploadProfileService(
private val fileUtil: FileUtil,
private val userFacade: UserFacade,
class ModifyUserInfoService(
private val userRepository: UserRepository,
private val userFacade: UserFacade,
@Value("\${cloud.aws.s3.default-image}")
private val defaultImage: String

private val defaultImage: String,
private val fileUtil: FileUtil
) {

@Transactional
fun uploadProfile(file: MultipartFile) {
fun execute(request: UserModifyRequest, file: MultipartFile?) {
val user = userFacade.currentUser() ?: throw UserNotFoundException

if (user.profile != defaultImage) fileUtil.delete(user.profile, Path.USER)
val url = fileUtil.upload(file, Path.USER)
user.updateProfile(url)
userRepository.save(user)
val url = file?.let { fileUtil.upload(it, Path.USER) }

userRepository.save(
user.updateProfile(request.name, request.enrolledSchool, url)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package org.meogo.domain.user.service

import org.meogo.domain.user.domain.UserRepository
import org.meogo.domain.user.exception.PasswordMismatchException
import org.meogo.domain.user.exception.UserNotFoundException
import org.meogo.domain.user.presentation.dto.request.UserSignInRequest
import org.meogo.global.jwt.JwtTokenProvider
import org.meogo.global.jwt.dto.TokenResponse
Expand All @@ -19,7 +18,6 @@ class UserSignInService(
@Transactional
fun execute(request: UserSignInRequest): TokenResponse {
val user = userRepository.findByAccountId(request.accountId)
?: throw UserNotFoundException

if (!passwordEncoder.matches(request.password, user.password)) throw PasswordMismatchException

Expand Down

0 comments on commit 04ad08f

Please sign in to comment.