From bb344fa56fe140f3a981ece331cd5006d0fd5eb4 Mon Sep 17 00:00:00 2001 From: teddy Date: Thu, 27 Nov 2025 16:57:42 +0900 Subject: [PATCH] =?UTF-8?q?=EA=B4=80=EB=A6=AC=EC=9E=90=20=EA=B4=80?= =?UTF-8?q?=EB=A6=AC=20=EC=B6=94=EA=B0=80,=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cd/kamcoback/auth/AuthApiController.java | 100 +++++++++++++----- .../kamco/cd/kamcoback/auth/dto/AuthDto.java | 70 ++++++++++-- .../kamcoback/auth/service/AuthService.java | 38 ++++++- .../postgres/core/AuthCoreService.java | 83 +++++++++++++-- .../kamcoback/postgres/entity/UserEntity.java | 27 +++-- .../repository/auth/AuthRepositoryCustom.java | 1 - .../repository/auth/AuthRepositoryImpl.java | 31 +----- 7 files changed, 268 insertions(+), 82 deletions(-) diff --git a/src/main/java/com/kamco/cd/kamcoback/auth/AuthApiController.java b/src/main/java/com/kamco/cd/kamcoback/auth/AuthApiController.java index f8940533..a718a490 100644 --- a/src/main/java/com/kamco/cd/kamcoback/auth/AuthApiController.java +++ b/src/main/java/com/kamco/cd/kamcoback/auth/AuthApiController.java @@ -15,7 +15,9 @@ import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Page; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; 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.RequestParam; @@ -29,35 +31,79 @@ public class AuthApiController { private final AuthService authService; - @Operation(summary = "관리자 등록", description = "관리자 를 등록 합니다.") + @Operation(summary = "관리자 등록", description = "관리자를 등록 합니다.") @ApiResponses( - value = { - @ApiResponse( - responseCode = "201", - description = "관리자 등록 성공", - content = - @Content( - mediaType = "application/json", - schema = @Schema(implementation = Long.class))), - @ApiResponse(responseCode = "400", description = "잘못된 요청 데이터", content = @Content), - @ApiResponse(responseCode = "404", description = "코드를 찾을 수 없음", content = @Content), - @ApiResponse(responseCode = "500", description = "서버 오류", content = @Content) - }) - @PostMapping("/signup") - public ApiResponseDto signup( - @io.swagger.v3.oas.annotations.parameters.RequestBody( - description = "관리자 정보", - required = true, - content = - @Content( - mediaType = "application/json", - schema = @Schema(implementation = AuthDto.Signup.class))) - @RequestBody - @Valid - AuthDto.Signup signup) { - return ApiResponseDto.createOK(authService.signup(signup)); + value = { + @ApiResponse( + responseCode = "201", + description = "관리자 등록 성공", + content = + @Content( + mediaType = "application/json", + schema = @Schema(implementation = Long.class))), + @ApiResponse(responseCode = "400", description = "잘못된 요청 데이터", content = @Content), + @ApiResponse(responseCode = "404", description = "코드를 찾을 수 없음", content = @Content), + @ApiResponse(responseCode = "500", description = "서버 오류", content = @Content) + }) + @PostMapping("/save") + public ApiResponseDto save( + @io.swagger.v3.oas.annotations.parameters.RequestBody( + description = "관리자 정보", + required = true, + content = + @Content( + mediaType = "application/json", + schema = @Schema(implementation = AuthDto.SaveReq.class))) + @RequestBody + @Valid + AuthDto.SaveReq saveReq) { + return ApiResponseDto.createOK(authService.save(saveReq).getId()); } + @Operation(summary = "관리자 정보 수정", description = "관리자 정보를 수정 합니다.") + @ApiResponses( + value = { + @ApiResponse( + responseCode = "201", + description = "관리자 정보 수정 성공", + content = + @Content( + mediaType = "application/json", + schema = @Schema(implementation = Long.class))), + @ApiResponse(responseCode = "400", description = "잘못된 요청 데이터", content = @Content), + @ApiResponse(responseCode = "404", description = "코드를 찾을 수 없음", content = @Content), + @ApiResponse(responseCode = "500", description = "서버 오류", content = @Content) + }) + @PutMapping("/update/{id}") + public ApiResponseDto update( + @PathVariable + Long id, + @RequestBody + AuthDto.SaveReq saveReq + ) { + return ApiResponseDto.createOK(authService.update(id, saveReq).getId()); + } + + @Operation(summary = "관리자 정보 탈퇴처리", description = "관리자 정보를 탈퇴처리 합니다.") + @ApiResponses( + value = { + @ApiResponse( + responseCode = "201", + description = "관리자 탈퇴처리 성공", + content = + @Content( + mediaType = "application/json", + schema = @Schema(implementation = Long.class))), + @ApiResponse(responseCode = "400", description = "잘못된 요청 데이터", content = @Content), + @ApiResponse(responseCode = "404", description = "코드를 찾을 수 없음", content = @Content), + @ApiResponse(responseCode = "500", description = "서버 오류", content = @Content) + }) + @PutMapping("/withdrawal/{id}") + public ApiResponseDto withdrawal(@PathVariable Long id) { + return ApiResponseDto.deleteOk(authService.withdrawal(id).getId()); + } + + @ApiResponses( value = { @ApiResponse( @@ -104,7 +150,7 @@ public class AuthApiController { @RequestParam(defaultValue = "20") int size, @Parameter(description = "정렬 조건 (형식: 필드명,방향)", example = "name,asc") @RequestParam(required = false) String sort - ) { + ) { AuthDto.SearchReq searchReq = new AuthDto.SearchReq(userNm, page, size, sort); Page userList = authService.getUserList(searchReq); return ApiResponseDto.ok(userList); diff --git a/src/main/java/com/kamco/cd/kamcoback/auth/dto/AuthDto.java b/src/main/java/com/kamco/cd/kamcoback/auth/dto/AuthDto.java index 6d47358e..969c42a6 100644 --- a/src/main/java/com/kamco/cd/kamcoback/auth/dto/AuthDto.java +++ b/src/main/java/com/kamco/cd/kamcoback/auth/dto/AuthDto.java @@ -40,10 +40,10 @@ public class AuthDto { } } - @Schema(name = "Signup", description = "사용자 등록 정보") + @Schema(name = "save request", description = "사용자 등록 정보") @Getter @Setter - public static class Signup { + public static class SaveReq { @Schema(description = "구분", example = "관리자/라벨러/검수자 중 하나") @NotBlank @@ -69,13 +69,64 @@ public class AuthDto { @NotBlank private String userEmail; - public Signup( - String userAuth, - String userNm, - String userId, - String userPw, - String empId, - String userEmail) { + public SaveReq( + String userAuth, + String userNm, + String userId, + String userPw, + String empId, + String userEmail) { + this.userAuth = userAuth; + this.userNm = userNm; + this.userId = userId; + this.userPw = userPw; + this.empId = empId; + this.userEmail = userEmail; + } + } + + @Schema(name = "update request", description = "사용자 수정 정보") + @Getter + @Setter + public static class UpdateReq { + + @Schema(description = "id", example = "1") + @NotBlank + private Long id; + + @Schema(description = "구분", example = "관리자/라벨러/검수자 중 하나") + @NotBlank + private String userAuth; + + @NotBlank + @Schema(description = "이름", example = "홍길동") + private String userNm; + + @Schema(description = "ID", example = "gildong") + @NotBlank + private String userId; + + @Schema(description = "PW", example = "password") + @NotBlank + private String userPw; + + @Schema(description = "사번", example = "사번") + @NotBlank + private String empId; + + @Schema(description = "이메일", example = "gildong@naver.com") + @NotBlank + private String userEmail; + + public UpdateReq( + Long id, + String userAuth, + String userNm, + String userId, + String userPw, + String empId, + String userEmail) { + this.id = id; this.userAuth = userAuth; this.userNm = userNm; this.userId = userId; @@ -87,6 +138,7 @@ public class AuthDto { @Getter public static class User { + String userId; String userPw; } diff --git a/src/main/java/com/kamco/cd/kamcoback/auth/service/AuthService.java b/src/main/java/com/kamco/cd/kamcoback/auth/service/AuthService.java index 7b379cc0..bf2e3366 100644 --- a/src/main/java/com/kamco/cd/kamcoback/auth/service/AuthService.java +++ b/src/main/java/com/kamco/cd/kamcoback/auth/service/AuthService.java @@ -3,6 +3,7 @@ package com.kamco.cd.kamcoback.auth.service; import com.kamco.cd.kamcoback.auth.dto.AuthDto; import com.kamco.cd.kamcoback.auth.dto.AuthDto.Basic; import com.kamco.cd.kamcoback.postgres.core.AuthCoreService; +import com.kamco.cd.kamcoback.postgres.entity.UserEntity; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Page; import org.springframework.security.crypto.password.PasswordEncoder; @@ -13,32 +14,59 @@ import org.springframework.transaction.annotation.Transactional; @Transactional(readOnly = true) @RequiredArgsConstructor public class AuthService { + private final AuthCoreService authCoreService; private final PasswordEncoder passwordEncoder; /** * 관리자 등록 * - * @param signup + * @param saveReq * @return */ @Transactional - public Long signup(AuthDto.Signup signup) { - signup.setUserPw(passwordEncoder.encode(signup.getUserPw())); - return authCoreService.signup(signup); + public UserEntity save(AuthDto.SaveReq saveReq) { + saveReq.setUserPw(passwordEncoder.encode(saveReq.getUserPw())); + return authCoreService.save(saveReq); + } + + /** + * 관리자 정보 수정 + * + * @param id + * @param saveReq + * @return + */ + public UserEntity update(Long id, AuthDto.SaveReq saveReq) { + if (saveReq.getUserPw() != null) { + saveReq.setUserPw(passwordEncoder.encode(saveReq.getUserPw())); + } + return authCoreService.update(id, saveReq); + } + + /** + * 관리자 삭제 + * + * @param id + * @return + */ + public UserEntity withdrawal(Long id) { + return authCoreService.withdrawal(id); } /** * 시퀀스 id로 관리자 조회 + * * @param id * @return */ - public AuthDto.Basic getFindUserById(Long id){ + public AuthDto.Basic getFindUserById(Long id) { return authCoreService.findUserById(id); } /** * 관리자 목록 조회 + * * @param searchReq * @return */ diff --git a/src/main/java/com/kamco/cd/kamcoback/postgres/core/AuthCoreService.java b/src/main/java/com/kamco/cd/kamcoback/postgres/core/AuthCoreService.java index e31595f9..1f364d35 100644 --- a/src/main/java/com/kamco/cd/kamcoback/postgres/core/AuthCoreService.java +++ b/src/main/java/com/kamco/cd/kamcoback/postgres/core/AuthCoreService.java @@ -1,9 +1,11 @@ package com.kamco.cd.kamcoback.postgres.core; import com.kamco.cd.kamcoback.auth.dto.AuthDto; +import com.kamco.cd.kamcoback.auth.dto.AuthDto.SaveReq; import com.kamco.cd.kamcoback.postgres.entity.UserEntity; import com.kamco.cd.kamcoback.postgres.repository.auth.AuthRepository; import jakarta.persistence.EntityNotFoundException; +import java.time.ZonedDateTime; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Page; import org.springframework.stereotype.Service; @@ -11,33 +13,102 @@ import org.springframework.stereotype.Service; @Service @RequiredArgsConstructor public class AuthCoreService { + private final AuthRepository authRepository; /** * 관리자 등록 * - * @param signup + * @param saveReq * @return */ - public Long signup(AuthDto.Signup signup) { - if (authRepository.findByUserId(signup.getUserId()).isPresent()) { - new EntityNotFoundException("중복된 아이디가 있습니다. " + signup.getUserId()); + public UserEntity save(SaveReq saveReq) { + if (authRepository.findByUserId(saveReq.getUserId()).isPresent()) { + new EntityNotFoundException("중복된 아이디가 있습니다. " + saveReq.getUserId()); } - return authRepository.signup(signup); + + UserEntity userEntity = new UserEntity( + null, + saveReq.getUserAuth(), + saveReq.getUserNm(), + saveReq.getUserId(), + saveReq.getEmpId(), + saveReq.getUserEmail(), + saveReq.getUserPw() + ); + + return authRepository.save(userEntity); + } + + /** + * 관리자 정보 수정 + * + * @param id + * @param saveReq + * @return + */ + public UserEntity update(Long id, AuthDto.SaveReq saveReq) { + UserEntity userEntity = authRepository.findById(id) + .orElseThrow(() -> new RuntimeException("유저가 존재하지 않습니다.")); + + if (saveReq.getUserAuth() != null) { + userEntity.setUserAuth(saveReq.getUserAuth()); + } + + if (saveReq.getUserNm() != null) { + userEntity.setUserNm(saveReq.getUserNm()); + } + + if (saveReq.getUserId() != null) { + userEntity.setUserId(saveReq.getUserId()); + } + + if (saveReq.getEmpId() != null) { + userEntity.setEmpId(saveReq.getEmpId()); + } + + if (saveReq.getUserEmail() != null) { + userEntity.setUserEmail(saveReq.getUserEmail()); + } + + if (saveReq.getUserPw() != null) { + userEntity.setUserPw(saveReq.getUserPw()); + } + + return authRepository.save(userEntity); + } + + /** + * 관리자 삭제 + * + * @param id + * @return + */ + public UserEntity withdrawal(Long id) { + UserEntity userEntity = authRepository.findById(id) + .orElseThrow(() -> new RuntimeException("유저가 존재하지 않습니다.")); + + userEntity.setId(id); + userEntity.setDateWithdrawal(ZonedDateTime.now()); + userEntity.setState("WITHDRAWAL"); + + return authRepository.save(userEntity); } /** * 시퀀스 id로 관리자 조회 + * * @param id * @return */ - public AuthDto.Basic findUserById(Long id){ + public AuthDto.Basic findUserById(Long id) { UserEntity entity = authRepository.findUserById(id).orElseThrow(() -> new EntityNotFoundException("관리자를 찾을 수 없습니다. " + id)); return entity.toDto(); } /** * 관리자 목록 조회 + * * @param searchReq * @return */ diff --git a/src/main/java/com/kamco/cd/kamcoback/postgres/entity/UserEntity.java b/src/main/java/com/kamco/cd/kamcoback/postgres/entity/UserEntity.java index beb67c6e..c0f2469c 100644 --- a/src/main/java/com/kamco/cd/kamcoback/postgres/entity/UserEntity.java +++ b/src/main/java/com/kamco/cd/kamcoback/postgres/entity/UserEntity.java @@ -12,24 +12,27 @@ import jakarta.persistence.UniqueConstraint; import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.Size; import java.time.ZonedDateTime; +import lombok.AccessLevel; import lombok.Getter; +import lombok.NoArgsConstructor; import lombok.Setter; import org.hibernate.annotations.ColumnDefault; @Getter @Setter @Entity +@NoArgsConstructor(access = AccessLevel.PROTECTED) @Table( - name = "tb_user", - uniqueConstraints = {@UniqueConstraint(name = "ux_tb_user_user_id", columnNames = "user_id")}) + name = "tb_user", + uniqueConstraints = {@UniqueConstraint(name = "ux_tb_user_user_id", columnNames = "user_id")}) public class UserEntity { @Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "tb_user_id_gen") @SequenceGenerator( - name = "tb_user_id_gen", - sequenceName = "tb_user_user_uid_seq", - allocationSize = 1) + name = "tb_user_id_gen", + sequenceName = "tb_user_user_uid_seq", + allocationSize = 1) @Column(name = "user_uid", nullable = false) private Long id; @@ -52,7 +55,7 @@ public class UserEntity { @NotNull @ColumnDefault("'ACTIVE'") @Column(name = "state", nullable = false) - private String state; + private String state = "ACTIVE"; @Column(name = "date_withdrawal") private ZonedDateTime dateWithdrawal; @@ -85,6 +88,16 @@ public class UserEntity { @Column(name = "emp_id", nullable = false) private String empId; + public UserEntity(Long id, String userAuth, String userNm, String userId, String empId, String userEmail, String userPw) { + this.id = id; + this.userAuth = userAuth; + this.userNm = userNm; + this.userId = userId; + this.empId = empId; + this.userEmail = userEmail; + this.userPw = userPw; + } + public AuthDto.Basic toDto() { return new AuthDto.Basic( this.id, @@ -94,7 +107,7 @@ public class UserEntity { this.empId, this.userEmail, this.createdDttm - ) ; + ); } } diff --git a/src/main/java/com/kamco/cd/kamcoback/postgres/repository/auth/AuthRepositoryCustom.java b/src/main/java/com/kamco/cd/kamcoback/postgres/repository/auth/AuthRepositoryCustom.java index 797046a3..1ec82999 100644 --- a/src/main/java/com/kamco/cd/kamcoback/postgres/repository/auth/AuthRepositoryCustom.java +++ b/src/main/java/com/kamco/cd/kamcoback/postgres/repository/auth/AuthRepositoryCustom.java @@ -7,7 +7,6 @@ import java.util.Optional; import org.springframework.data.domain.Page; public interface AuthRepositoryCustom { - Long signup(AuthDto.Signup signup); Optional findByUserId(String userId); diff --git a/src/main/java/com/kamco/cd/kamcoback/postgres/repository/auth/AuthRepositoryImpl.java b/src/main/java/com/kamco/cd/kamcoback/postgres/repository/auth/AuthRepositoryImpl.java index 9abb5f20..85aec762 100644 --- a/src/main/java/com/kamco/cd/kamcoback/postgres/repository/auth/AuthRepositoryImpl.java +++ b/src/main/java/com/kamco/cd/kamcoback/postgres/repository/auth/AuthRepositoryImpl.java @@ -19,35 +19,10 @@ import org.springframework.stereotype.Repository; @Repository @RequiredArgsConstructor public class AuthRepositoryImpl implements AuthRepositoryCustom { + private final JPAQueryFactory queryFactory; private final QUserEntity userEntity = QUserEntity.userEntity; - /** - * 관리자 등록 - * - * @param signup - * @return - */ - @Override - public Long signup(AuthDto.Signup signup) { - return queryFactory - .insert(userEntity) - .columns( - userEntity.userAuth, - userEntity.userId, - userEntity.userNm, - userEntity.userPw, - userEntity.userEmail, - userEntity.empId) - .values( - signup.getUserAuth(), - signup.getUserId(), - signup.getUserNm(), - signup.getUserPw(), - signup.getUserEmail(), - signup.getEmpId()) - .execute(); - } /** * 유저 아이디로 조회 @@ -58,11 +33,12 @@ public class AuthRepositoryImpl implements AuthRepositoryCustom { @Override public Optional findByUserId(String userId) { return Optional.ofNullable( - queryFactory.selectFrom(userEntity).where(userEntity.userId.eq(userId)).fetchOne()); + queryFactory.selectFrom(userEntity).where(userEntity.userId.eq(userId)).fetchOne()); } /** * 유저 시퀀스 id로 조회 + * * @param id * @return */ @@ -74,6 +50,7 @@ public class AuthRepositoryImpl implements AuthRepositoryCustom { /** * 관리자 목록 조회 + * * @param searchReq * @return */