-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from PLADI-ALM/feat/PDS-11-searchOffiice
[PDS-11/feat] 회의실 목록 조회 API생성,exceptionHandler 추가
- Loading branch information
Showing
9 changed files
with
233 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
src/main/java/com/example/pladialmserver/global/exception/BaseException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.example.pladialmserver.global.exception; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public class BaseException extends RuntimeException { | ||
private BaseResponseCode baseResponseCode; | ||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/com/example/pladialmserver/global/exception/BaseResponseCode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.example.pladialmserver.global.exception; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
|
||
import java.util.Arrays; | ||
|
||
|
||
@Getter | ||
@AllArgsConstructor | ||
public enum BaseResponseCode { | ||
|
||
SUCCESS("S0001", HttpStatus.OK, "요청에 성공했습니다."), | ||
|
||
BAD_REQUEST("G0001", HttpStatus.BAD_REQUEST, "잘못된 요청입니다."), | ||
|
||
//office | ||
NOT_DATE_TIME("B0001", HttpStatus.BAD_REQUEST,"날짜나 시간을 모두 입력해주세요"); | ||
|
||
|
||
public final String code; | ||
public final HttpStatus status; | ||
public final String message; | ||
|
||
public static BaseResponseCode findByCode(String code) { | ||
return Arrays.stream(BaseResponseCode.values()) | ||
.filter(b -> b.getCode().equals(code)) | ||
.findAny().orElseThrow(() -> new BaseException(BAD_REQUEST)); | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/com/example/pladialmserver/global/exception/ExceptionHandlerAdvice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.example.pladialmserver.global.exception; | ||
|
||
import com.example.pladialmserver.global.response.ResponseCustom; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.validation.FieldError; | ||
import org.springframework.web.bind.MethodArgumentNotValidException; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
import java.util.Objects; | ||
|
||
@RestControllerAdvice | ||
public class ExceptionHandlerAdvice { | ||
@ExceptionHandler(BaseException.class) | ||
protected ResponseEntity<ResponseCustom> handleBaseException(BaseException e) { | ||
BaseResponseCode baseResponseCode = e.getBaseResponseCode(); | ||
return ResponseEntity.status(baseResponseCode.getStatus()) | ||
.body(ResponseCustom.error(baseResponseCode.getStatus().value(), baseResponseCode.getCode(), baseResponseCode.getMessage())); | ||
} | ||
|
||
@ResponseStatus(HttpStatus.BAD_REQUEST) | ||
@ExceptionHandler(MethodArgumentNotValidException.class) | ||
protected ResponseCustom handleMethodArgumentNotValidException(MethodArgumentNotValidException e) { | ||
FieldError fieldError = Objects.requireNonNull(e.getFieldError()); | ||
BaseResponseCode baseResponseCode = BaseResponseCode.findByCode(fieldError.getDefaultMessage()); | ||
return ResponseCustom.error(baseResponseCode.getStatus().value(), baseResponseCode.getCode(), baseResponseCode.getMessage()); | ||
} | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
src/main/java/com/example/pladialmserver/global/response/ResponseCustom.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.example.pladialmserver.global.response; | ||
|
||
|
||
|
||
import com.example.pladialmserver.global.exception.BaseResponseCode; | ||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.lang.Nullable; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
@AllArgsConstructor | ||
public class ResponseCustom<T>{ | ||
|
||
private final int status; | ||
|
||
private final String code; | ||
|
||
private final String message; | ||
|
||
@JsonInclude(JsonInclude.Include.NON_NULL) | ||
private T data; | ||
|
||
public static ResponseCustom OK() { | ||
BaseResponseCode baseResponseCode = BaseResponseCode.SUCCESS; | ||
return new ResponseCustom<>(baseResponseCode.getStatus().value(), baseResponseCode.getCode(), baseResponseCode.getMessage()); | ||
} | ||
|
||
public static <T> ResponseCustom<T> OK(@Nullable T data) { | ||
BaseResponseCode baseResponseCode = BaseResponseCode.SUCCESS; | ||
return new ResponseCustom<T>(baseResponseCode.getStatus().value(), baseResponseCode.getCode(), baseResponseCode.getMessage(), data); | ||
} | ||
|
||
public static ResponseCustom error(int status, String code, String message) { | ||
return new ResponseCustom<>(status, code, message); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/main/java/com/example/pladialmserver/office/dto/OfficeRes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.example.pladialmserver.office.dto; | ||
|
||
import com.example.pladialmserver.office.entity.Facility; | ||
import com.example.pladialmserver.office.entity.Office; | ||
import lombok.*; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Data | ||
@Builder | ||
public class OfficeRes { | ||
private String name; | ||
private String location; | ||
private Integer capacity; | ||
private List<String> facilityList; | ||
private String description; | ||
|
||
public static OfficeRes toDto(Office office, List<Facility> facilities){ | ||
return OfficeRes.builder() | ||
.name(office.getName()) | ||
.location(office.getLocation()) | ||
.capacity(office.getCapacity()) | ||
.facilityList(facilities.stream().map(Facility::getName).collect(Collectors.toList())) | ||
.description(office.getDescription()) | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/main/java/com/example/pladialmserver/office/service/OfficeService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,59 @@ | ||
package com.example.pladialmserver.office.service; | ||
|
||
import com.example.pladialmserver.office.dto.OfficeRes; | ||
import com.example.pladialmserver.office.entity.Facility; | ||
import com.example.pladialmserver.office.entity.Office; | ||
import com.example.pladialmserver.office.entity.OfficeBooking; | ||
import com.example.pladialmserver.office.repository.OfficeBookingRepository; | ||
import com.example.pladialmserver.office.repository.OfficeRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.time.LocalDate; | ||
import java.time.LocalTime; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class OfficeService { | ||
|
||
private final OfficeRepository officeRepository; | ||
private final OfficeBookingRepository officeBookingRepository; | ||
|
||
|
||
|
||
public List<OfficeRes> findAvailableOffices(LocalDate date, LocalTime startTime, LocalTime endTime) { | ||
List<Office> allOffices = officeRepository.findAll(); | ||
|
||
if (date != null && startTime != null && endTime != null) { | ||
// 입력된 날짜와 시간에 이미 예약된 회의실을 조회 | ||
List<OfficeBooking> bookedOffices = officeBookingRepository.findByDateAndTime(date, startTime, endTime); | ||
|
||
// 이미 예약된 회의실의 ID 목록을 추출 | ||
List<Long> bookedOfficeIds = bookedOffices.stream() | ||
.map(booking -> booking.getOffice().getOfficeId()) | ||
.collect(Collectors.toList()); | ||
|
||
// 예약된 회의실을 제외한 회의실 목록을 필터링 | ||
allOffices = allOffices.stream() | ||
.filter(office -> !bookedOfficeIds.contains(office.getOfficeId())) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
// 반환할 결과 리스트를 생성 | ||
List<OfficeRes> result = new ArrayList<>(); | ||
|
||
for (Office office : allOffices) { | ||
List<Facility> facilities = office.getFacilityList().stream() | ||
.map(officeFacility -> officeFacility.getFacility()) | ||
.collect(Collectors.toList()); | ||
|
||
result.add(OfficeRes.toDto(office, facilities)); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
} |