Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

135 user accept api #136

Merged
merged 17 commits into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
bdfa4f2
:sparkles: :: accept user request
esperar Apr 6, 2023
bbb2641
:sparkles: :: AcceptUserService
esperar Apr 6, 2023
77a09a7
:sparkles: :: accept user api
esperar Apr 6, 2023
92fcd4d
:art: :: security config accept-user path μΆ”κ°€
esperar Apr 6, 2023
89e843d
:recycle: :: getUser Role 인자둜 λ°›κΈ°
esperar Apr 6, 2023
47219d3
:recycle: :: BadUserRoleRequestException 으둜 λ˜μ§€κΈ°
esperar Apr 6, 2023
e4d5afa
:art: :: path둜 user id λ°›κ²Œ μˆ˜μ •
esperar Apr 6, 2023
2b086d3
:fire: :: accept user request id μ‚­μ œ
esperar Apr 6, 2023
fbb0bee
:recycle: :: Signup -> SignUp
esperar Apr 6, 2023
bfa24f3
:recycle: :: user accept μœ μ € κΆŒν•œ λΆ€μ—¬ μˆ˜μ •
esperar Apr 6, 2023
ba9f39c
:art: :: validation μΆ”κ°€
esperar Apr 6, 2023
1082003
:recycle: :: private fun μœ„λ‘œ μ˜¬λ €μ•Όν•˜λŠ”κ΅°μš”
esperar Apr 6, 2023
8e8ba97
:art: user role μ—…λ°μ΄νŠΈ
esperar Apr 6, 2023
36c2b4f
:recycle: :: 이름 λ°”κΎΈλ‹ˆκΉŒ redeclaration λ– μ„œ λ‹€μ‹œ λŒλ €λ†“κΈ°
esperar Apr 6, 2023
c9b7fe2
:art: :: update μ—μ„œ Teacher을 λΆ€μ—¬ν•˜λ„λ‘ λ³€κ²½
esperar Apr 6, 2023
110f617
:recycle: :: μœ μ €λ₯Ό μ—…λ°μ΄νŠΈν•˜λŠ” λΆ€λΆ„ μ½”λ“œ κ°œμ„ 
esperar Apr 6, 2023
74f5baf
:art: :: Accept student, teacher service도 let을 μ‚¬μš©
esperar Apr 6, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
esperar marked this conversation as resolved.
Show resolved Hide resolved
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