From e2ef67a3d9bb91af711a0f5502917deef8f9f31e Mon Sep 17 00:00:00 2001 From: dyhj625 Date: Thu, 20 Feb 2025 16:19:52 +0900 Subject: [PATCH] =?UTF-8?q?=ED=9C=B4=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 --- .../company/localhost/common/dto/MapDto.java | 32 ++++++++++- .../controller/api/VacationController.java | 56 ++++++++++++++----- .../localhost/mapper/localvacaMapper.java | 2 + .../localhost/service/localvacaService.java | 9 ++- src/main/resources/mapper/localvacaMapper.xml | 8 ++- 5 files changed, 89 insertions(+), 18 deletions(-) diff --git a/src/main/java/io/company/localhost/common/dto/MapDto.java b/src/main/java/io/company/localhost/common/dto/MapDto.java index ec17348..2b6e6d5 100644 --- a/src/main/java/io/company/localhost/common/dto/MapDto.java +++ b/src/main/java/io/company/localhost/common/dto/MapDto.java @@ -20,7 +20,11 @@ import org.springframework.util.ObjectUtils; import java.io.Serial; import java.math.BigDecimal; import java.math.BigInteger; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; import java.util.Map; +import java.util.stream.Collectors; public class MapDto extends ListOrderedMap { @@ -74,6 +78,32 @@ public class MapDto extends ListOrderedMap { } return null; } - + + public List getList(String key, Class clazz) { + Object value = this.get(key); + if (value == null) { + return Collections.emptyList(); + } + if (!(value instanceof List)) { + throw new IllegalArgumentException("Value for key " + key + " is not a List"); + } + List list = (List) value; + List result = new ArrayList<>(); + for (Object item : list) { + if (item == null) { + continue; // null인 항목은 건너뜁니다. + } + if (clazz.isInstance(item)) { + result.add(clazz.cast(item)); + } else if (clazz.equals(Long.class) && item instanceof Integer) { + result.add(clazz.cast(Long.valueOf((Integer) item))); + } else if (item instanceof Map && clazz.equals(MapDto.class)) { + result.add(clazz.cast(new MapDto((Map) item))); + } else { + throw new ClassCastException("Cannot cast " + item.getClass() + " to " + clazz); + } + } + return result; + } } 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 ef61e70..041cf3f 100644 --- a/src/main/java/io/company/localhost/controller/api/VacationController.java +++ b/src/main/java/io/company/localhost/controller/api/VacationController.java @@ -33,24 +33,52 @@ public class VacationController { @Member @ParameterCheck @PostMapping - public ApiResponse saveVacations(@RequestBody List map) { - Long user = AuthUtil.getUser().getId(); - for (MapDto request : map) { - String date = request.getString("date"); - String type = request.getString("type"); - Object receiverId = request.get("receiverId"); - - if ( date == null || type == null) { - throw new IllegalArgumentException("요청 데이터에 누락된 값이 있습니다: " + request); + public ApiResponse saveVacations(@RequestBody List list) { + Long user = AuthUtil.getUser().getId(); + for (MapDto request : list) { + String date = request.getString("date"); + String type = request.getString("type"); + Object receiverId = request.get("receiverId"); + request.put("employeeId", user); + + if (date == null || type == null) { + throw new IllegalArgumentException("요청 데이터에 누락된 값이 있습니다: " + request); + } + + // count 필드가 있으면, 해당 값만큼 반복해서 insert + Integer count = request.getInt("count"); + if (count == null || count < 1) { + count = 1; + } + for (int i = 0; i < count; i++) { + localVacaService.insertVacation(request); + } } - request.put("employeeId", user); - // 데이터 저장 - localVacaService.insertVacation(request); + return ApiResponse.ok("모든 휴가가 성공적으로 저장되었습니다."); } - return ApiResponse.ok("모든 휴가가 성공적으로 저장되었습니다."); + @Member + @ParameterCheck + @PostMapping("/batchUpdate") + public ApiResponse batchUpdateVacations(@ReqMap MapDto map) { + Long user = AuthUtil.getUser().getId(); + List addRequests = map.getList("add", MapDto.class); + List deleteIds = map.getList("delete", Long.class); + + if (addRequests != null) { + for (MapDto addRequest : addRequests) { + addRequest.put("employeeId", user); + localVacaService.insertVacation(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 6b4779e..00b8fb1 100644 --- a/src/main/java/io/company/localhost/mapper/localvacaMapper.java +++ b/src/main/java/io/company/localhost/mapper/localvacaMapper.java @@ -12,6 +12,8 @@ import io.company.localhost.common.dto.MapDto; public interface localvacaMapper { void insertVacation(MapDto map); + void deleteVacation(Long vacationId); + List findVacations(@Param("year") int year, @Param("month") int month); List getUsedVacations(@Param("userId") Long userId); diff --git a/src/main/java/io/company/localhost/service/localvacaService.java b/src/main/java/io/company/localhost/service/localvacaService.java index 39ebedd..bdc2350 100644 --- a/src/main/java/io/company/localhost/service/localvacaService.java +++ b/src/main/java/io/company/localhost/service/localvacaService.java @@ -34,8 +34,13 @@ public class localvacaService { @Value("${api.public-holiday.key}") private String serviceKey; - public void insertVacation(MapDto map) { - localvacaMapper.insertVacation(map); + public void insertVacation(MapDto vacation) { + // 필요한 경우 데이터 검증/전처리 후 매퍼 호출 + localvacaMapper.insertVacation(vacation); + } + + public void deleteVacation(Long vacationId) { + localvacaMapper.deleteVacation(vacationId); } public List getVacationList(int year, int month) { diff --git a/src/main/resources/mapper/localvacaMapper.xml b/src/main/resources/mapper/localvacaMapper.xml index 879936b..88a948d 100644 --- a/src/main/resources/mapper/localvacaMapper.xml +++ b/src/main/resources/mapper/localvacaMapper.xml @@ -8,9 +8,15 @@ VALUES (#{employeeId}, #{date}, #{type}, NOW(), #{receiverId}) + + + DELETE FROM localvaca + WHERE LOCVACSEQ = #{vacationId} + +