Skip to content

Commit

Permalink
Merge pull request #4 from PLADI-ALM/feat/PDS-11-searchOffiice
Browse files Browse the repository at this point in the history
[PDS-11/feat] 회의실 목록 조회 API생성,exceptionHandler 추가
  • Loading branch information
psyeon1120 authored Sep 19, 2023
2 parents 518271b + ec58640 commit 6b5db30
Show file tree
Hide file tree
Showing 9 changed files with 233 additions and 4 deletions.
6 changes: 6 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ dependencies {
annotationProcessor 'org.projectlombok:lombok'
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.springframework.boot:spring-boot-starter-validation'


// mysql
runtimeOnly 'com.mysql:mysql-connector-j'
Expand All @@ -42,3 +44,7 @@ dependencies {
tasks.named('test') {
useJUnitPlatform()
}




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;
}
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));
}

}
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());
}

}
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);
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
package com.example.pladialmserver.office.controller;

import com.example.pladialmserver.global.exception.BaseException;
import com.example.pladialmserver.global.exception.BaseResponseCode;
import com.example.pladialmserver.global.response.ResponseCustom;
import com.example.pladialmserver.office.dto.OfficeRes;
import com.example.pladialmserver.office.entity.Office;
import com.example.pladialmserver.office.service.OfficeService;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.apache.coyote.Response;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.List;

@RestController
@RequiredArgsConstructor
Expand All @@ -13,13 +24,28 @@ public class OfficeController {
private final OfficeService officeService;

/**
* 회의실 목록 조회
* 전체 회의실 목록 조회 and 예약 가능한 회의실 목록 조회
*/

@GetMapping()
public ResponseCustom<List<OfficeRes>> searchOffice(
@RequestParam(required = false ) @DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate date,
@RequestParam(required = false) @DateTimeFormat(pattern = "HH:mm:ss") LocalTime startTime,
@RequestParam(required = false) @DateTimeFormat(pattern = "HH:mm:ss") LocalTime endTime
)
{
// 날짜와 시작 시간 또는 종료 시간 중 하나라도 입력되지 않았다면 에러 반환
if ((date != null && (startTime == null || endTime == null)) ||
(date == null && (startTime != null || endTime != null))){
throw new BaseException(BaseResponseCode.NOT_DATE_TIME);
}
return ResponseCustom.OK(officeService.findAvailableOffices(date, startTime, endTime));
}
/**
* 회의실 개별 조회
*/



/**
* 회의실 일자별 예약 현황 조회
*/
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/com/example/pladialmserver/office/dto/OfficeRes.java
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@

import com.example.pladialmserver.office.entity.OfficeBooking;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.List;

@Repository
public interface OfficeBookingRepository extends JpaRepository<OfficeBooking, Long> {
@Query("SELECT ob FROM OfficeBooking ob WHERE ob.date = :date AND ((ob.startTime <= :startTime AND ob.endTime > :startTime) OR (ob.startTime < :endTime AND ob.endTime >= :endTime))")
List<OfficeBooking> findByDateAndTime(@Param("date") LocalDate date, @Param("startTime") LocalTime startTime, @Param("endTime") LocalTime endTime);

}

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;
}

}

0 comments on commit 6b5db30

Please sign in to comment.