From b16008c0d1da1c497d42a8ac488768438e2ffef1 Mon Sep 17 00:00:00 2001 From: ckx6954 Date: Tue, 18 Mar 2025 20:10:45 +0900 Subject: [PATCH 1/3] =?UTF-8?q?handler=20Error=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/exception/code/UserErrorCode.java | 8 ++--- .../handler/MemberAuthFailureHandler.java | 30 ++++++------------- 2 files changed, 13 insertions(+), 25 deletions(-) diff --git a/src/main/java/io/company/localhost/common/exception/code/UserErrorCode.java b/src/main/java/io/company/localhost/common/exception/code/UserErrorCode.java index c7a4be5..42cd8c8 100644 --- a/src/main/java/io/company/localhost/common/exception/code/UserErrorCode.java +++ b/src/main/java/io/company/localhost/common/exception/code/UserErrorCode.java @@ -26,10 +26,10 @@ public enum UserErrorCode implements ErrorCode { NOT_AUTH_USER(HttpStatus.UNAUTHORIZED ,"로그인이 필요합니다."), INACTIVE_USER(HttpStatus.FORBIDDEN,"권한이 필요합니다."), - USER_NOT_FOUND(HttpStatus.UNAUTHORIZED,"아이디 혹은 비밀번호가 틀렸습니다."), - NOT_AUTHORIZED(HttpStatus.UNAUTHORIZED,"비인가 계정입니다."), - EXIT_USER(HttpStatus.UNAUTHORIZED,"탈퇴한 계정입니다."), - BAD_CREDENTIAL(HttpStatus.UNAUTHORIZED, "아이디 혹은 비밀번호 문제") + USER_NOT_FOUND(10001,HttpStatus.UNAUTHORIZED,"아이디 혹은 비밀번호가 틀렸습니다."), + NOT_AUTHORIZED(10002,HttpStatus.UNAUTHORIZED,"비인가 계정입니다."), + EXIT_USER(10003,HttpStatus.UNAUTHORIZED,"탈퇴한 계정입니다."), + BAD_CREDENTIAL(10004,HttpStatus.UNAUTHORIZED, "아이디 혹은 비밀번호 문제") ; private final long code; diff --git a/src/main/java/io/company/localhost/common/security/handler/MemberAuthFailureHandler.java b/src/main/java/io/company/localhost/common/security/handler/MemberAuthFailureHandler.java index 5bf065c..134b19c 100644 --- a/src/main/java/io/company/localhost/common/security/handler/MemberAuthFailureHandler.java +++ b/src/main/java/io/company/localhost/common/security/handler/MemberAuthFailureHandler.java @@ -15,6 +15,8 @@ package io.company.localhost.common.security.handler; import com.fasterxml.jackson.databind.ObjectMapper; +import io.company.localhost.common.dto.ApiResponse; +import io.company.localhost.common.exception.code.UserErrorCode; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import org.springframework.http.MediaType; @@ -24,8 +26,6 @@ import org.springframework.security.web.authentication.AuthenticationFailureHand import org.springframework.stereotype.Component; import java.io.IOException; -import java.util.HashMap; -import java.util.Map; @Component("failHandler") public class MemberAuthFailureHandler implements AuthenticationFailureHandler { @@ -35,31 +35,19 @@ public class MemberAuthFailureHandler implements AuthenticationFailureHandler { ObjectMapper mapper = new ObjectMapper(); response.setStatus(HttpServletResponse.SC_OK); response.setContentType(MediaType.APPLICATION_JSON_VALUE); - - Map responseMap = new HashMap<>(); - responseMap.put("success", false); // 로그인 실패 표시 - + String message = exception.getMessage(); - String errorCode; - String errorMessage; + + ApiResponse res = UserErrorCode.BAD_CREDENTIAL.getApiResponse(); if (exception instanceof BadCredentialsException || message.startsWith("NOT_FOUND")) { - errorCode = "USER_NOT_FOUND"; - errorMessage = "아이디 또는 비밀번호가 일치하지 않습니다."; + res = UserErrorCode.USER_NOT_FOUND.getApiResponse(); } else if (message.startsWith("NOT_AUTHORIZED")) { - errorCode = "NOT_AUTHORIZED"; - errorMessage = "접근 권한이 없습니다."; + res = UserErrorCode.NOT_AUTH_USER.getApiResponse(); } else if (message.startsWith("EXIT")) { - errorCode = "EXIT_USER"; - errorMessage = "탈퇴한 사용자입니다."; - } else { - errorCode = "BAD_CREDENTIAL"; - errorMessage = "인증에 실패했습니다."; + res = UserErrorCode.EXIT_USER.getApiResponse(); } - - responseMap.put("code", errorCode); - responseMap.put("message", errorMessage); - response.getWriter().write(mapper.writeValueAsString(responseMap)); + response.getWriter().write(mapper.writeValueAsString(res)); } } From ea506179801c63c531556abc9afd42aa943bdcc2 Mon Sep 17 00:00:00 2001 From: ckx6954 Date: Tue, 18 Mar 2025 20:34:27 +0900 Subject: [PATCH 2/3] =?UTF-8?q?ErrorCode=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/company/localhost/common/dto/ApiResponse.java | 10 ++++++++-- .../localhost/common/exception/code/UserErrorCode.java | 6 +++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/main/java/io/company/localhost/common/dto/ApiResponse.java b/src/main/java/io/company/localhost/common/dto/ApiResponse.java index 8919aa2..fc3abe5 100644 --- a/src/main/java/io/company/localhost/common/dto/ApiResponse.java +++ b/src/main/java/io/company/localhost/common/dto/ApiResponse.java @@ -14,11 +14,12 @@ *************************************************************/ package io.company.localhost.common.dto; +import lombok.AllArgsConstructor; +import lombok.Getter; import org.springframework.http.HttpStatus; -import lombok.Getter; - @Getter +@AllArgsConstructor public class ApiResponse { private int code; private HttpStatus status; @@ -47,6 +48,11 @@ public class ApiResponse { public static ApiResponse error(HttpStatus status, String message) { return new ApiResponse<>(status, message, null); } + + public static ApiResponse error(int code,HttpStatus status, String message) { + return new ApiResponse<>(code,status, message, null); + } + public static ApiResponse okMessage(String message) { return new ApiResponse<>(HttpStatus.OK, message, null); } diff --git a/src/main/java/io/company/localhost/common/exception/code/UserErrorCode.java b/src/main/java/io/company/localhost/common/exception/code/UserErrorCode.java index 42cd8c8..086d3b2 100644 --- a/src/main/java/io/company/localhost/common/exception/code/UserErrorCode.java +++ b/src/main/java/io/company/localhost/common/exception/code/UserErrorCode.java @@ -43,7 +43,11 @@ public enum UserErrorCode implements ErrorCode { } public ApiResponse getApiResponse() { - return ApiResponse.error(this.getHttpStatus() , this.getMessage()); + if(this.code > 10000){ + return ApiResponse.error((int) this.getCode(),this.getHttpStatus() , this.getMessage()); + }else{ + return ApiResponse.error(this.getHttpStatus() , this.getMessage()); + } } public ApiResponse getApiResponse(String message) { From 42bb0dd2442e1b2ebfe9d27982da3b4b5ed0dd68 Mon Sep 17 00:00:00 2001 From: dyhj625 Date: Wed, 19 Mar 2025 17:45:46 +0900 Subject: [PATCH 3/3] =?UTF-8?q?=ED=9C=B4=EA=B0=80=20=EC=A0=80=EC=9E=A5?= =?UTF-8?q?=EB=A1=9C=EC=A7=81=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/api/VacationController.java | 21 +++++++----- .../localhost/mapper/localvacaMapper.java | 8 +++-- .../localhost/service/localvacaService.java | 34 +++++++++++++++++-- src/main/resources/mapper/localvacaMapper.xml | 23 +++++++++++-- 4 files changed, 70 insertions(+), 16 deletions(-) diff --git a/src/main/java/io/company/localhost/controller/api/VacationController.java b/src/main/java/io/company/localhost/controller/api/VacationController.java index c1eb988..5b279df 100644 --- a/src/main/java/io/company/localhost/controller/api/VacationController.java +++ b/src/main/java/io/company/localhost/controller/api/VacationController.java @@ -99,20 +99,23 @@ public class VacationController { Long user = AuthUtil.getUser().getId(); List addRequests = map.getList("add", MapDto.class); List deleteIds = map.getList("delete", Long.class); - - if (addRequests != null) { + + // 1️⃣ 삭제 먼저 처리 + if (deleteIds != null && !deleteIds.isEmpty()) { + localVacaService.deleteVacation(deleteIds); + } + + // 2️⃣ 추가 또는 업데이트 처리 + if (addRequests != null && !addRequests.isEmpty()) { for (MapDto addRequest : addRequests) { addRequest.put("employeeId", user); - localVacaService.insertVacation(addRequest); + localVacaService.upsertVacation(addRequest); } - } - if (deleteIds != null) { - for (Long deleteId : deleteIds) { - localVacaService.deleteVacation(deleteId); - } - } + } + return ApiResponse.ok("휴가 변경이 성공적으로 처리되었습니다."); } + /** * 전체 사원의 휴가 조회 diff --git a/src/main/java/io/company/localhost/mapper/localvacaMapper.java b/src/main/java/io/company/localhost/mapper/localvacaMapper.java index c90a248..ad7643b 100644 --- a/src/main/java/io/company/localhost/mapper/localvacaMapper.java +++ b/src/main/java/io/company/localhost/mapper/localvacaMapper.java @@ -25,8 +25,8 @@ import io.company.localhost.common.dto.MapDto; @Mapper public interface localvacaMapper { void insertVacation(MapDto map); - - void deleteVacation(Long vacationId); + + void deleteVacation(@Param("vacationIds") List vacationIds); List selectVacations(@Param("year") int year, @Param("month") int month); @@ -37,6 +37,10 @@ public interface localvacaMapper { List selectEmployeeRemainingVacation(); List selectSentVacationCount(MapDto map); + + Long findVacationIdByDate(@Param("userId") Long employeeId, @Param("date") String date); + + void updateVacation(MapDto vacation); } diff --git a/src/main/java/io/company/localhost/service/localvacaService.java b/src/main/java/io/company/localhost/service/localvacaService.java index 616f91e..bb061b5 100644 --- a/src/main/java/io/company/localhost/service/localvacaService.java +++ b/src/main/java/io/company/localhost/service/localvacaService.java @@ -34,6 +34,7 @@ import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import org.springframework.web.client.RestTemplate; import io.company.localhost.common.dto.MapDto; import io.company.localhost.mapper.localvacaMapper; @@ -53,10 +54,39 @@ public class localvacaService { localvacaMapper.insertVacation(vacation); } - public void deleteVacation(Long vacationId) { - localvacaMapper.deleteVacation(vacationId); + /** + * 여러 개의 휴가를 삭제 + */ + @Transactional + public void deleteVacation(List vacationIds) { + if (vacationIds != null && !vacationIds.isEmpty()) { + localvacaMapper.deleteVacation(vacationIds); + } } + /** + * 휴가 추가 또는 업데이트 (중복 검사 후 처리) + */ + @Transactional + public void upsertVacation(MapDto vacation) { + String date = vacation.getString("date"); + Long employeeId = vacation.getLong("employeeId"); + + // 기존 데이터 확인 + Long existingId = localvacaMapper.findVacationIdByDate(employeeId, date); + + if (existingId != null) { + // 기존 휴가가 존재하면 업데이트 + vacation.put("id", existingId); + localvacaMapper.updateVacation(vacation); + } else { + System.out.println(vacation); + // 기존 휴가가 없으면 새롭게 추가 + localvacaMapper.insertVacation(vacation); + } + } + + public List selectVacationList(int year, int month) { return localvacaMapper.selectVacations(year, month); } diff --git a/src/main/resources/mapper/localvacaMapper.xml b/src/main/resources/mapper/localvacaMapper.xml index 8a16d2c..cf3bb35 100644 --- a/src/main/resources/mapper/localvacaMapper.xml +++ b/src/main/resources/mapper/localvacaMapper.xml @@ -8,10 +8,27 @@ VALUES (#{employeeId}, #{date}, #{type}, NOW(), #{receiverId}) - - + + + + + + UPDATE localvaca + SET LOCVACTYP = #{type} + WHERE LOCVACSEQ = #{id} + + + + DELETE FROM localvaca - WHERE LOCVACSEQ = #{vacationId} + WHERE LOCVACSEQ IN + + #{id} +