diff --git a/src/main/kotlin/org/meogo/domain/review/service/QueryAllBySchoolIdService.kt b/src/main/kotlin/org/meogo/domain/review/service/QueryAllBySchoolIdService.kt index 389d692..1ee008a 100644 --- a/src/main/kotlin/org/meogo/domain/review/service/QueryAllBySchoolIdService.kt +++ b/src/main/kotlin/org/meogo/domain/review/service/QueryAllBySchoolIdService.kt @@ -3,6 +3,7 @@ package org.meogo.domain.review.service import org.meogo.domain.review.domain.ReviewRepository import org.meogo.domain.review.presentation.dto.response.ReviewListResponse import org.meogo.domain.review.presentation.dto.response.ReviewResponse +import org.meogo.domain.user.exception.UserNotFoundException import org.meogo.domain.user.facade.UserFacade import org.meogo.global.s3.FileUtil import org.meogo.global.s3.Path @@ -23,7 +24,7 @@ class QueryAllBySchoolIdService( return ReviewListResponse( count = reviews.size, reviews = reviews.map { review -> - val user = userFacade.getUserById(review.userId) + val user = userFacade.getUserById(review.userId) ?: throw UserNotFoundException val profile = fileUtil.generateObjectUrl(user.profile, Path.USER) val image = review.picture?.split(",")?.map { pic -> fileUtil.generateObjectUrl(pic.trim(), Path.REVIEW) diff --git a/src/main/kotlin/org/meogo/domain/user/domain/User.kt b/src/main/kotlin/org/meogo/domain/user/domain/User.kt index 0d97a92..dd9168f 100644 --- a/src/main/kotlin/org/meogo/domain/user/domain/User.kt +++ b/src/main/kotlin/org/meogo/domain/user/domain/User.kt @@ -12,7 +12,7 @@ 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, @@ -20,15 +20,19 @@ class User( 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 } } diff --git a/src/main/kotlin/org/meogo/domain/user/domain/UserRepository.kt b/src/main/kotlin/org/meogo/domain/user/domain/UserRepository.kt index 0bd6a7b..bbbe90c 100644 --- a/src/main/kotlin/org/meogo/domain/user/domain/UserRepository.kt +++ b/src/main/kotlin/org/meogo/domain/user/domain/UserRepository.kt @@ -1,12 +1,14 @@ package org.meogo.domain.user.domain -import org.springframework.data.jpa.repository.JpaRepository +import org.springframework.data.repository.Repository import java.util.UUID -interface UserRepository : JpaRepository { +interface UserRepository : Repository { fun save(entity: User): User - fun findByAccountId(accountId: String): User? + fun findById(id: UUID): User? + + fun findByAccountId(accountId: String): User fun existsByAccountId(accountId: String): Boolean } diff --git a/src/main/kotlin/org/meogo/domain/user/facade/UserFacade.kt b/src/main/kotlin/org/meogo/domain/user/facade/UserFacade.kt index 96c6db3..3d5019b 100644 --- a/src/main/kotlin/org/meogo/domain/user/facade/UserFacade.kt +++ b/src/main/kotlin/org/meogo/domain/user/facade/UserFacade.kt @@ -2,7 +2,6 @@ package org.meogo.domain.user.facade import org.meogo.domain.user.domain.User import org.meogo.domain.user.domain.UserRepository -import org.meogo.domain.user.exception.UserNotFoundException import org.springframework.security.core.context.SecurityContextHolder import org.springframework.stereotype.Component import java.util.UUID @@ -17,9 +16,9 @@ class UserFacade( return accountId?.let { getUserByAccountId(it) } } - fun getUserByAccountId(accountId: String): User = - userRepository.findByAccountId(accountId) ?: throw UserNotFoundException + fun getUserByAccountId(accountId: String): User? = + userRepository.findByAccountId(accountId) - fun getUserById(id: UUID): User = - userRepository.findById(id).orElseThrow { UserNotFoundException } + fun getUserById(id: UUID): User? = + userRepository.findById(id) } diff --git a/src/main/kotlin/org/meogo/domain/user/presentation/UserController.kt b/src/main/kotlin/org/meogo/domain/user/presentation/UserController.kt index d63fb24..343ae70 100644 --- a/src/main/kotlin/org/meogo/domain/user/presentation/UserController.kt +++ b/src/main/kotlin/org/meogo/domain/user/presentation/UserController.kt @@ -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 @@ -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) @@ -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) } diff --git a/src/main/kotlin/org/meogo/domain/user/presentation/dto/request/UserModifyRequest.kt b/src/main/kotlin/org/meogo/domain/user/presentation/dto/request/UserModifyRequest.kt new file mode 100644 index 0000000..9e9182d --- /dev/null +++ b/src/main/kotlin/org/meogo/domain/user/presentation/dto/request/UserModifyRequest.kt @@ -0,0 +1,6 @@ +package org.meogo.domain.user.presentation.dto.request + +data class UserModifyRequest( + val name: String, + val enrolledSchool: Int +) diff --git a/src/main/kotlin/org/meogo/domain/user/service/UploadProfileService.kt b/src/main/kotlin/org/meogo/domain/user/service/ModifyUserInfoService.kt similarity index 58% rename from src/main/kotlin/org/meogo/domain/user/service/UploadProfileService.kt rename to src/main/kotlin/org/meogo/domain/user/service/ModifyUserInfoService.kt index 756035a..1d41cc1 100644 --- a/src/main/kotlin/org/meogo/domain/user/service/UploadProfileService.kt +++ b/src/main/kotlin/org/meogo/domain/user/service/ModifyUserInfoService.kt @@ -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 @@ -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) + if (file != null && user.profile != defaultImage) fileUtil.delete(user.profile, Path.USER) + val url = file?.let { fileUtil.upload(it, Path.USER) } + + userRepository.save( + user.updateProfile(request.name, request.enrolledSchool, url) + ) } } diff --git a/src/main/kotlin/org/meogo/domain/user/service/MyPageService.kt b/src/main/kotlin/org/meogo/domain/user/service/QueryMyPageService.kt similarity index 96% rename from src/main/kotlin/org/meogo/domain/user/service/MyPageService.kt rename to src/main/kotlin/org/meogo/domain/user/service/QueryMyPageService.kt index 40ba249..f8f7bbd 100644 --- a/src/main/kotlin/org/meogo/domain/user/service/MyPageService.kt +++ b/src/main/kotlin/org/meogo/domain/user/service/QueryMyPageService.kt @@ -7,7 +7,7 @@ import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional @Service -class MyPageService( +class QueryMyPageService( private val userFacade: UserFacade ) { diff --git a/src/main/kotlin/org/meogo/domain/user/service/UserSignInService.kt b/src/main/kotlin/org/meogo/domain/user/service/UserSignInService.kt index 004a4ab..832faec 100644 --- a/src/main/kotlin/org/meogo/domain/user/service/UserSignInService.kt +++ b/src/main/kotlin/org/meogo/domain/user/service/UserSignInService.kt @@ -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 @@ -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 diff --git a/src/main/kotlin/org/meogo/global/auth/AuthDetailsService.kt b/src/main/kotlin/org/meogo/global/auth/AuthDetailsService.kt index 3a357af..3eadb0f 100644 --- a/src/main/kotlin/org/meogo/global/auth/AuthDetailsService.kt +++ b/src/main/kotlin/org/meogo/global/auth/AuthDetailsService.kt @@ -1,5 +1,6 @@ package org.meogo.global.auth +import org.meogo.domain.user.exception.UserNotFoundException import org.meogo.domain.user.facade.UserFacade import org.springframework.security.core.userdetails.UserDetails import org.springframework.security.core.userdetails.UserDetailsService @@ -10,7 +11,7 @@ class AuthDetailsService( private val userFacade: UserFacade ) : UserDetailsService { override fun loadUserByUsername(username: String): UserDetails { - val user = userFacade.getUserByAccountId(username) + val user = userFacade.getUserByAccountId(username) ?: throw UserNotFoundException return AuthDetails(user.accountId, user.role) } }