Merge branch 'main' into yoon

This commit is contained in:
yoon 2025-03-20 10:08:40 +09:00
commit 1643a5fc7c
7 changed files with 96 additions and 44 deletions

View File

@ -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<T> {
private int code;
private HttpStatus status;
@ -47,6 +48,11 @@ public class ApiResponse<T> {
public static <T> ApiResponse<T> error(HttpStatus status, String message) {
return new ApiResponse<>(status, message, null);
}
public static <T> ApiResponse<T> error(int code,HttpStatus status, String message) {
return new ApiResponse<>(code,status, message, null);
}
public static <T> ApiResponse<T> okMessage(String message) {
return new ApiResponse<>(HttpStatus.OK, message, null);
}

View File

@ -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;
@ -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) {

View File

@ -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 {
@ -36,30 +36,18 @@ public class MemberAuthFailureHandler implements AuthenticationFailureHandler {
response.setStatus(HttpServletResponse.SC_OK);
response.setContentType(MediaType.APPLICATION_JSON_VALUE);
Map<String, Object> 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));
}
}

View File

@ -100,20 +100,23 @@ public class VacationController {
List<MapDto> addRequests = map.getList("add", MapDto.class);
List<Long> 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);
}
}
if (deleteIds != null) {
for (Long deleteId : deleteIds) {
localVacaService.deleteVacation(deleteId);
localVacaService.upsertVacation(addRequest);
}
}
return ApiResponse.ok("휴가 변경이 성공적으로 처리되었습니다.");
}
/**
* 전체 사원의 휴가 조회
* @param year, month

View File

@ -26,7 +26,7 @@ import io.company.localhost.common.dto.MapDto;
public interface localvacaMapper {
void insertVacation(MapDto map);
void deleteVacation(Long vacationId);
void deleteVacation(@Param("vacationIds") List<Long> vacationIds);
List<MapDto> selectVacations(@Param("year") int year, @Param("month") int month);
@ -37,6 +37,10 @@ public interface localvacaMapper {
List<MapDto> selectEmployeeRemainingVacation();
List<MapDto> selectSentVacationCount(MapDto map);
Long findVacationIdByDate(@Param("userId") Long employeeId, @Param("date") String date);
void updateVacation(MapDto vacation);
}

View File

@ -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<Long> 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<MapDto> selectVacationList(int year, int month) {
return localvacaMapper.selectVacations(year, month);
}

View File

@ -8,10 +8,27 @@
VALUES (#{employeeId}, #{date}, #{type}, NOW(), #{receiverId})
</insert>
<!-- 휴가 데이터 삭제 -->
<delete id="deleteVacation" parameterType="long">
<!-- 특정 날짜의 휴가 ID 조회 (존재 여부 확인) -->
<select id="findVacationIdByDate" resultType="Long">
SELECT LOCVACSEQ
FROM localvaca
WHERE MEMBERSEQ = #{userId} AND LOCVACUDT = #{date}
</select>
<!-- 휴가 수정 (기존 데이터가 존재하는 경우) -->
<update id="updateVacation">
UPDATE localvaca
SET LOCVACTYP = #{type}
WHERE LOCVACSEQ = #{id}
</update>
<!-- ✅ 여러 개의 휴가 삭제 -->
<delete id="deleteVacation">
DELETE FROM localvaca
WHERE LOCVACSEQ = #{vacationId}
WHERE LOCVACSEQ IN
<foreach item="id" collection="vacationIds" open="(" separator="," close=")">
#{id}
</foreach>
</delete>
<!-- 휴가 정보 조회 -->