Skip to content

Commit

Permalink
Merge pull request #136 from GSM-MSG/135-user-accept-api
Browse files Browse the repository at this point in the history
135 user accept api
  • Loading branch information
esperar authored Apr 6, 2023
2 parents 74cc22e + 74f5baf commit 165eca5
Show file tree
Hide file tree
Showing 13 changed files with 109 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package com.msg.gauth.domain.auth.presentation
import com.msg.gauth.domain.auth.presentation.dto.request.SignUpDto
import com.msg.gauth.domain.auth.presentation.dto.request.SigninRequestDto
import com.msg.gauth.domain.auth.presentation.dto.response.RefreshResponseDto
import com.msg.gauth.domain.auth.presentation.dto.response.SigninResponseDto
import com.msg.gauth.domain.auth.services.*
import com.msg.gauth.domain.auth.presentation.dto.request.PasswordInitReqDto
import com.msg.gauth.domain.auth.presentation.dto.response.SigninResponseDto
import com.msg.gauth.domain.auth.presentation.dto.response.SignupImageResDto
import com.msg.gauth.domain.auth.services.InitPasswordService
import org.springframework.http.HttpStatus
Expand All @@ -22,7 +22,7 @@ class AuthController(
private val signInService: SignInService,
private val signUpService: SignUpService,
private val initPasswordService: InitPasswordService,
private val signupImageUploadService: SignupImageUploadService,
private val signUpImageUploadService: SignupImageUploadService,
) {
@PatchMapping
fun refresh(@RequestHeader("RefreshToken") refreshToken: String): ResponseEntity<RefreshResponseDto> {
Expand All @@ -37,8 +37,8 @@ class AuthController(
}

@PostMapping
fun signin(@Valid @RequestBody signinRequestDto: SigninRequestDto): ResponseEntity<SigninResponseDto> {
val result = signInService.execute(signinRequestDto)
fun signin(@Valid @RequestBody signInRequestDto: SigninRequestDto): ResponseEntity<SigninResponseDto> {
val result = signInService.execute(signInRequestDto)
return ResponseEntity.ok(result)
}

Expand All @@ -50,7 +50,7 @@ class AuthController(

@PatchMapping("/image")
fun uploadSignupImage(@RequestPart("image") image: MultipartFile, @RequestPart("imageUrl") previousUrl: String?, @RequestPart email: String): ResponseEntity<SignupImageResDto> {
val result = signupImageUploadService.execute(image, previousUrl, email)
val result = signUpImageUploadService.execute(image, previousUrl, email)
return ResponseEntity.ok(result)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.msg.gauth.domain.client.exception

import com.msg.gauth.global.exception.ErrorCode
import com.msg.gauth.global.exception.exceptions.BasicException

class BadUserRoleRequestException : BasicException(ErrorCode.BAD_USER_ROLE_REQUEST)
20 changes: 19 additions & 1 deletion src/main/kotlin/com/msg/gauth/domain/user/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.msg.gauth.domain.user.enums.Gender
import com.msg.gauth.domain.user.enums.UserRole
import com.msg.gauth.domain.user.enums.UserState
import com.msg.gauth.domain.user.presentation.dto.request.AcceptStudentReqDto
import com.msg.gauth.domain.user.presentation.dto.request.AcceptUserReqDto
import com.msg.gauth.global.entity.BaseIdEntity
import javax.persistence.*
import javax.validation.constraints.Size
Expand Down Expand Up @@ -69,7 +70,7 @@ class User(
num = this.num,
gender = gender,
password = this.password,
roles = this.roles,
roles = mutableListOf(UserRole.ROLE_TEACHER),
state = UserState.CREATED,
profileUrl = this.profileUrl
)
Expand Down Expand Up @@ -110,6 +111,23 @@ class User(
return user
}

fun update(acceptUserReqDto: AcceptUserReqDto): User{
val user = User(
name = acceptUserReqDto.name,
email = this.email,
grade = acceptUserReqDto.grade,
classNum = acceptUserReqDto.classNum,
num = acceptUserReqDto.num,
gender = acceptUserReqDto.gender,
password = this.password,
roles = this.roles,
state = UserState.CREATED,
profileUrl = this.profileUrl
)
user.id = this.id
return user
}

fun updateProfile(profileUrl: String): User{
val user = User(
name = this.name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.msg.gauth.domain.user.presentation
import com.msg.gauth.domain.user.presentation.dto.response.GetMyRolesResDto
import com.msg.gauth.domain.user.presentation.dto.request.AcceptStudentReqDto
import com.msg.gauth.domain.user.presentation.dto.request.AcceptTeacherReqDto
import com.msg.gauth.domain.user.presentation.dto.request.AcceptUserReqDto
import com.msg.gauth.domain.user.presentation.dto.request.PasswordChangeReqDto
import com.msg.gauth.domain.user.presentation.dto.response.MyProfileResDto
import com.msg.gauth.domain.user.presentation.dto.response.SingleAcceptedUserResDto
Expand All @@ -24,7 +25,8 @@ class UserController(
private val acceptTeacherSignUpService: AcceptTeacherSignUpService,
private val getPendingUsersService: GetPendingUsersService,
private val acceptStudentSignUpService: AcceptStudentSignUpService,
private val getMyRolesService: GetMyRolesService
private val getMyRolesService: GetMyRolesService,
private val acceptUserSignUpService: AcceptUserSignUpService
) {
@GetMapping("/role")
fun getMyRoles(): ResponseEntity<GetMyRolesResDto> {
Expand Down Expand Up @@ -66,6 +68,12 @@ class UserController(
return ResponseEntity.ok(result)
}

@PatchMapping("/accept-user/{id}")
fun acceptUser(@PathVariable id: Long, @RequestBody @Valid acceptUserReqDto: AcceptUserReqDto): ResponseEntity<Void>{
acceptUserSignUpService.execute(id, acceptUserReqDto)
return ResponseEntity.noContent().build()
}

@Deprecated("This api is deprecated. Use acceptUser instead")
@PatchMapping("/accept-teacher")
fun acceptTeacher(@RequestBody @Valid acceptTeacherReqDto: AcceptTeacherReqDto): ResponseEntity<Void>{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.msg.gauth.domain.user.presentation.dto.request

import com.msg.gauth.domain.user.enums.Gender
import javax.validation.constraints.NotBlank

data class AcceptTeacherReqDto(
val id: Long,
@field:NotBlank
val name: String,
val gender: Gender
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.msg.gauth.domain.user.presentation.dto.request

import com.msg.gauth.domain.user.enums.Gender
import com.msg.gauth.domain.user.enums.UserRole
import javax.validation.constraints.NotBlank
import javax.validation.constraints.NotNull

data class AcceptUserReqDto(
@field:NotNull
val userRole: UserRole,
@field:NotBlank
val name: String,
@field:NotNull
val gender: Gender,
val grade: Int?,
val classNum: Int?,
val num: Int?
)
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ interface UserRepository: JpaRepository<User, Long>, JpaSpecificationExecutor<Us
@Query("select user.email from User user")
fun findAllEmail(): List<String>
fun findAllByState(state: UserState): List<User>
fun findByIdAndState(id: Long, roles: UserState): User?
fun findAllByStateOrderByGrade(state: UserState, pageable: Pageable): List<User>
fun findAllByStateAndNameContainingOrderByGrade(state: UserState, name: String, pageable: Pageable): List<User>
fun findAllByStateAndClassNumOrderByGrade(state: UserState, classNum: Int, pageable: Pageable): List<User>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class AcceptStudentSignUpService(
val user = userRepository.findByIdAndStateAndRoles(acceptedStudentReqDto.id, UserState.PENDING, mutableListOf(UserRole.ROLE_STUDENT))
?: throw UserNotFoundException()

userRepository.save(user.update(acceptedStudentReqDto))
user.update(acceptedStudentReqDto)
.let { userRepository.save(it) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class AcceptTeacherSignUpService(
fun execute(acceptTeacherReqDto: AcceptTeacherReqDto) {
val user: User = userRepository.findByIdAndStateAndRoles(acceptTeacherReqDto.id, UserState.PENDING, mutableListOf(UserRole.ROLE_TEACHER))
?: throw UserNotFoundException()
userRepository.save(user.update(acceptTeacherReqDto.name, acceptTeacherReqDto.gender))
user.update(acceptTeacherReqDto.name, acceptTeacherReqDto.gender)
.let { userRepository.save(it) }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.msg.gauth.domain.user.services

import com.msg.gauth.domain.client.exception.BadUserRoleRequestException
import com.msg.gauth.domain.user.enums.UserRole
import com.msg.gauth.domain.user.enums.UserState
import com.msg.gauth.domain.user.exception.UserNotFoundException
import com.msg.gauth.domain.user.presentation.dto.request.AcceptUserReqDto
import com.msg.gauth.domain.user.repository.UserRepository
import com.msg.gauth.global.annotation.service.TransactionalService

@TransactionalService
class AcceptUserSignUpService(
private val userRepository: UserRepository
) {

fun execute(id: Long, acceptUserReqDto: AcceptUserReqDto) =
when(acceptUserReqDto.userRole){
UserRole.ROLE_STUDENT -> acceptStudent(id, acceptUserReqDto)
UserRole.ROLE_TEACHER -> acceptTeacher(id, acceptUserReqDto)
else -> throw BadUserRoleRequestException()
}

private fun getUser(id: Long) =
userRepository.findByIdAndState(id, UserState.PENDING)
?: throw UserNotFoundException()

private fun acceptStudent(id: Long, acceptUserReqDto: AcceptUserReqDto) =
getUser(id).update(acceptUserReqDto)
.let { userRepository.save(it) }


private fun acceptTeacher(id: Long, acceptUserReqDto: AcceptUserReqDto) =
getUser(id).update(acceptUserReqDto.name, acceptUserReqDto.gender)
.let { userRepository.save(it) }

}
6 changes: 3 additions & 3 deletions src/main/kotlin/com/msg/gauth/global/exception/ErrorCode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ enum class ErrorCode(
USER_NOT_SAME("유저가 일치하지 않습니다", 401),
OAUTH_CODE_EXPIRED("oauth 코드가 만료되었습니다.", 401),

BAD_USER_ROLE_REQUEST("잘못된 유저 역할입니다..", 400),

FORBIDDEN("금지된 요청입니다.", 403),
USER_STATE_PENDING("유저가 보류중 입니다.", 403),

Expand All @@ -32,7 +34,5 @@ enum class ErrorCode(
MANY_REQUEST_EMAIL_AUTH("15분에 최대 3번 이메일 인증을 요청할 수 있습니다.", 429),

MAIL_SEND_FAIL("메일을 보내는데 실패했습니다.", 500),
INTERNAL_SERVER_ERROR("서버 내부 에러", 500)

;
INTERNAL_SERVER_ERROR("서버 내부 에러", 500);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.msg.gauth.global.exception.exceptions

import com.msg.gauth.global.exception.ErrorCode

class BadRequestException : BasicException(ErrorCode.BAD_REQUEST)
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class SecurityConfig(
.antMatchers(HttpMethod.PATCH, "/user/accept-teacher").hasRole("ADMIN")
.antMatchers(HttpMethod.GET, "/user/pending").hasRole("ADMIN")
.antMatchers(HttpMethod.PATCH, "/user/accept-student").hasRole("ADMIN")
.antMatchers(HttpMethod.PATCH, "/user/accept-user").hasRole("ADMIN")

.anyRequest().denyAll()
.and()
Expand Down

0 comments on commit 165eca5

Please sign in to comment.