forked from Olympic/NTO-2025-Backend-TeamTask
решение 2 этапа
This commit is contained in:
@@ -1,134 +1,32 @@
|
|||||||
package com.example.nto.controller;
|
package com.example.nto.controller;
|
||||||
|
|
||||||
import com.example.nto.entity.Booking;
|
import com.example.nto.controller.dto.BookingCreateDto;
|
||||||
import com.example.nto.entity.Employee;
|
import com.example.nto.controller.dto.PlaceDto;
|
||||||
import com.example.nto.entity.Place;
|
|
||||||
import com.example.nto.service.BookingService;
|
import com.example.nto.service.BookingService;
|
||||||
import com.example.nto.service.EmployeeService;
|
import lombok.RequiredArgsConstructor;
|
||||||
import com.example.nto.service.PlaceService;
|
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.validation.annotation.Validated;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.stream.Collectors;
|
@Validated
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/api/{code}")
|
@RequestMapping("api")
|
||||||
|
@RequiredArgsConstructor
|
||||||
public class BookingController {
|
public class BookingController {
|
||||||
|
|
||||||
private final EmployeeService employeeService;
|
private static BookingService bookingService;
|
||||||
private final BookingService bookingService;
|
|
||||||
private final PlaceService placeService;
|
|
||||||
|
|
||||||
public BookingController(EmployeeService employeeService, BookingService bookingService, PlaceService placeService) {
|
@GetMapping("/{code}/booking")
|
||||||
this.employeeService = employeeService;
|
@ResponseStatus(code = HttpStatus.OK)
|
||||||
this.bookingService = bookingService;
|
public Map<LocalDate, List<PlaceDto>> getByDate(@PathVariable String code) {
|
||||||
this.placeService = placeService;
|
return bookingService.getFreePLace(code);
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/auth")
|
@GetMapping("/{code}/book")
|
||||||
public ResponseEntity<Void> auth(@PathVariable String code) {
|
@ResponseStatus(code = HttpStatus.CREATED)
|
||||||
Optional<Employee> employeeOpt = employeeService.findByCode(code);
|
public void create(@PathVariable String code, @RequestBody BookingCreateDto bookingCreateDto) {
|
||||||
if (employeeOpt.isPresent()) {
|
bookingService.create(code, bookingCreateDto);
|
||||||
return ResponseEntity.ok().build();
|
|
||||||
} else {
|
|
||||||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@GetMapping("/info")
|
|
||||||
public ResponseEntity<Map<String, Object>> info(@PathVariable String code) {
|
|
||||||
Optional<Employee> employeeOpt = employeeService.findByCode(code);
|
|
||||||
if (employeeOpt.isEmpty()) return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
|
|
||||||
|
|
||||||
Employee employee = employeeOpt.get();
|
|
||||||
Map<String, Object> response = new HashMap<>();
|
|
||||||
response.put("name", employee.getName());
|
|
||||||
response.put("photoUrl", employee.getPhotoUrl());
|
|
||||||
|
|
||||||
Map<String, List<Map<String, Object>>> bookingsMap = new HashMap<>();
|
|
||||||
List<Booking> bookingList = bookingService.getBookingsByEmployee(employee);
|
|
||||||
|
|
||||||
for (Booking b : bookingList) {
|
|
||||||
String dateKey = b.getDate().toString();
|
|
||||||
Map<String, Object> bookingData = new HashMap<>();
|
|
||||||
bookingData.put("id", b.getId());
|
|
||||||
bookingData.put("place", b.getPlace().getPlace());
|
|
||||||
|
|
||||||
bookingsMap.computeIfAbsent(dateKey, k -> new ArrayList<>()).add(bookingData);
|
|
||||||
}
|
|
||||||
|
|
||||||
response.put("booking", bookingsMap);
|
|
||||||
return ResponseEntity.ok(response);
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("/booking")
|
|
||||||
public ResponseEntity<Map<String, List<Map<String, Object>>>> availableBooking(@PathVariable String code) {
|
|
||||||
Optional<Employee> employeeOpt = employeeService.findByCode(code);
|
|
||||||
if (employeeOpt.isEmpty()) return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
|
|
||||||
|
|
||||||
Map<String, List<Map<String, Object>>> result = new LinkedHashMap<>();
|
|
||||||
LocalDate today = LocalDate.now();
|
|
||||||
List<Place> allPlaces = placeService.getAllPlaces();
|
|
||||||
|
|
||||||
for (int i = 0; i < 4; i++) {
|
|
||||||
LocalDate date = today.plusDays(i);
|
|
||||||
List<Booking> bookings = bookingService.getBookingsByDate(date);
|
|
||||||
|
|
||||||
Set<Long> bookedPlaceIds = bookings.stream()
|
|
||||||
.map(b -> b.getPlace().getId())
|
|
||||||
.collect(Collectors.toSet());
|
|
||||||
|
|
||||||
List<Map<String, Object>> available = allPlaces.stream()
|
|
||||||
.filter(p -> !bookedPlaceIds.contains(p.getId()))
|
|
||||||
.map(p -> {
|
|
||||||
Map<String, Object> m = new HashMap<>();
|
|
||||||
m.put("id", p.getId());
|
|
||||||
m.put("place", p.getPlace());
|
|
||||||
return m;
|
|
||||||
})
|
|
||||||
.collect(Collectors.toList());
|
|
||||||
|
|
||||||
result.put(date.toString(), available);
|
|
||||||
}
|
|
||||||
|
|
||||||
return ResponseEntity.ok(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
@PostMapping("/book")
|
|
||||||
public ResponseEntity<Void> book(@PathVariable String code, @RequestBody Map<String, String> body) {
|
|
||||||
Optional<Employee> employeeOpt = employeeService.findByCode(code);
|
|
||||||
if (employeeOpt.isEmpty()) return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
|
|
||||||
Employee employee = employeeOpt.get();
|
|
||||||
|
|
||||||
try {
|
|
||||||
String dateStr = body.get("date");
|
|
||||||
String placeIdStr = body.getOrDefault("placeID",
|
|
||||||
body.getOrDefault("placeId",
|
|
||||||
body.getOrDefault("place", null)));
|
|
||||||
|
|
||||||
if (dateStr == null || placeIdStr == null) {
|
|
||||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
LocalDate date = LocalDate.parse(dateStr);
|
|
||||||
Long placeId = Long.parseLong(placeIdStr);
|
|
||||||
Place place = placeService.getPlaceById(placeId);
|
|
||||||
|
|
||||||
if (bookingService.getBookingByEmployeeAndDate(employee, date).isPresent()) {
|
|
||||||
return ResponseEntity.status(HttpStatus.CONFLICT).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (bookingService.getBookingByDateAndPlace(date, place).isPresent()) {
|
|
||||||
return ResponseEntity.status(HttpStatus.CONFLICT).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
bookingService.createBooking(employee, place, date);
|
|
||||||
return ResponseEntity.status(HttpStatus.CREATED).build();
|
|
||||||
|
|
||||||
} catch (Exception e) {
|
|
||||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,30 +1,27 @@
|
|||||||
package com.example.nto.controller;
|
package com.example.nto.controller;
|
||||||
|
|
||||||
import com.example.nto.entity.Employee;
|
import com.example.nto.controller.dto.EmployeeDto;
|
||||||
import com.example.nto.service.EmployeeService;
|
import com.example.nto.service.EmployeeService;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/api/employee")
|
@RequestMapping("api")
|
||||||
|
@RequiredArgsConstructor
|
||||||
public class EmployeeController {
|
public class EmployeeController {
|
||||||
|
|
||||||
private final EmployeeService employeeService;
|
private final EmployeeService employeeService;
|
||||||
|
|
||||||
public EmployeeController(EmployeeService employeeService) {
|
@GetMapping("/{code}/auth")
|
||||||
this.employeeService = employeeService;
|
@ResponseStatus(code = HttpStatus.OK)
|
||||||
|
public void login(@PathVariable String code) {
|
||||||
|
employeeService.auth(code);
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/{code}")
|
@GetMapping("/{code}/info")
|
||||||
public ResponseEntity<Employee> getEmployeeByCode(@PathVariable String code) {
|
@ResponseStatus(code = HttpStatus.OK)
|
||||||
Optional<Employee> employeeOpt = employeeService.findByCode(code);
|
public EmployeeDto getByCode(@PathVariable String code) {
|
||||||
if (employeeOpt.isPresent()) {
|
return employeeService.getByCode(code);
|
||||||
return ResponseEntity.ok(employeeOpt.get());
|
|
||||||
} else {
|
|
||||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
package com.example.nto.controller.dto;
|
||||||
|
|
||||||
|
import jakarta.validation.constraints.NotNull;
|
||||||
|
import jakarta.validation.constraints.Positive;
|
||||||
|
import lombok.*;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class BookingCreateDto {
|
||||||
|
@NotNull
|
||||||
|
private LocalDate date;
|
||||||
|
@Positive
|
||||||
|
private long placeId;
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package com.example.nto.controller.dto;
|
||||||
|
|
||||||
|
|
||||||
|
import com.example.nto.entity.Booking;
|
||||||
|
import com.example.nto.entity.Employee;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.TreeMap;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class EmployeeDto {
|
||||||
|
private String name;
|
||||||
|
private String photoUrl;
|
||||||
|
private Map<LocalDate, PlaceDto> booking;
|
||||||
|
|
||||||
|
public static EmployeeDto toDto(Employee employee) {
|
||||||
|
Map<LocalDate, PlaceDto> dtoTreeMap = new TreeMap<>();
|
||||||
|
for (Booking booking : employee.getBookingList()) {
|
||||||
|
dtoTreeMap.put(booking.getDate(), PlaceDto.toDto(booking.getPlace()));
|
||||||
|
}
|
||||||
|
|
||||||
|
return new EmployeeDto(employee.getName(), employee.getPhotoUrl(), dtoTreeMap);
|
||||||
|
}
|
||||||
|
}
|
||||||
18
src/main/java/com/example/nto/controller/dto/PlaceDto.java
Normal file
18
src/main/java/com/example/nto/controller/dto/PlaceDto.java
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package com.example.nto.controller.dto;
|
||||||
|
|
||||||
|
import com.example.nto.entity.Place;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@Builder
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
public class PlaceDto {
|
||||||
|
private long id;
|
||||||
|
private String place;
|
||||||
|
|
||||||
|
public static PlaceDto toDto(Place place){return new PlaceDto(place.getId(), place.getPlace());}
|
||||||
|
}
|
||||||
@@ -1,42 +1,35 @@
|
|||||||
package com.example.nto.entity;
|
package com.example.nto.entity;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
@Data
|
||||||
@Entity
|
@Entity
|
||||||
|
@Builder
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
@Table(name = "booking")
|
@Table(name = "booking")
|
||||||
@JsonIgnoreProperties({"hibernateLazyInitializer"})
|
|
||||||
public class Booking {
|
public class Booking {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
private long id;
|
private long id;
|
||||||
|
|
||||||
|
@Column(name = "date")
|
||||||
private LocalDate date;
|
private LocalDate date;
|
||||||
|
|
||||||
@ManyToOne(fetch = FetchType.EAGER)
|
@ManyToOne(targetEntity = Place.class, fetch = FetchType.LAZY)
|
||||||
@JoinColumn(name = "place_id")
|
@JoinColumn(name = "place_id")
|
||||||
private Place place;
|
private Place place;
|
||||||
|
|
||||||
@ManyToOne(fetch = FetchType.EAGER)
|
@ManyToOne(fetch = FetchType.LAZY)
|
||||||
@JoinColumn(name = "employee_id")
|
@JoinColumn(name = "employee_id")
|
||||||
private Employee employee;
|
private Employee employee;
|
||||||
|
|
||||||
public Booking() {}
|
|
||||||
|
|
||||||
public Booking(Employee employee, Place place, LocalDate date) {
|
|
||||||
this.employee = employee;
|
|
||||||
this.place = place;
|
|
||||||
this.date = date;
|
|
||||||
}
|
|
||||||
|
|
||||||
public long getId() { return id; }
|
|
||||||
public void setId(long id) { this.id = id; }
|
|
||||||
public LocalDate getDate() { return date; }
|
|
||||||
public void setDate(LocalDate date) { this.date = date; }
|
|
||||||
public Place getPlace() { return place; }
|
|
||||||
public void setPlace(Place place) { this.place = place; }
|
|
||||||
public Employee getEmployee() { return employee; }
|
|
||||||
public void setEmployee(Employee employee) { this.employee = employee; }
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,18 @@
|
|||||||
package com.example.nto.entity;
|
package com.example.nto.entity;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
import java.util.ArrayList;
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@Data
|
||||||
@Entity
|
@Entity
|
||||||
|
@Builder
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
@Table(name = "employee")
|
@Table(name = "employee")
|
||||||
public class Employee {
|
public class Employee {
|
||||||
|
|
||||||
@@ -13,33 +20,16 @@ public class Employee {
|
|||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
private long id;
|
private long id;
|
||||||
|
|
||||||
|
@Column(name = "name")
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
@Column(unique = true)
|
@Column(name = "code")
|
||||||
private String code;
|
private String code;
|
||||||
|
|
||||||
|
@Column(name = "photo_url")
|
||||||
private String photoUrl;
|
private String photoUrl;
|
||||||
|
|
||||||
@OneToMany(mappedBy = "employee", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
|
@OneToMany(mappedBy = "employee", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
|
||||||
@JsonIgnore
|
private List<Booking> bookingList;
|
||||||
private List<Booking> bookingList = new ArrayList<>();
|
|
||||||
|
|
||||||
public Employee() {}
|
|
||||||
|
|
||||||
public Employee(String name, String code, String photoUrl) {
|
|
||||||
this.name = name;
|
|
||||||
this.code = code;
|
|
||||||
this.photoUrl = photoUrl;
|
|
||||||
}
|
|
||||||
|
|
||||||
public long getId() { return id; }
|
|
||||||
public void setId(long id) { this.id = id; }
|
|
||||||
public String getName() { return name; }
|
|
||||||
public void setName(String name) { this.name = name; }
|
|
||||||
public String getCode() { return code; }
|
|
||||||
public void setCode(String code) { this.code = code; }
|
|
||||||
public String getPhotoUrl() { return photoUrl; }
|
|
||||||
public void setPhotoUrl(String photoUrl) { this.photoUrl = photoUrl; }
|
|
||||||
public List<Booking> getBookingList() { return bookingList; }
|
|
||||||
public void setBookingList(List<Booking> bookingList) { this.bookingList = bookingList; }
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,23 +1,23 @@
|
|||||||
package com.example.nto.entity;
|
package com.example.nto.entity;
|
||||||
|
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
@Data
|
||||||
@Entity
|
@Entity
|
||||||
|
@Builder
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
@Table(name = "place")
|
@Table(name = "place")
|
||||||
public class Place {
|
public class Place {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
private Long id;
|
private long id;
|
||||||
|
|
||||||
@Column(name = "place_name", nullable = false, unique = true)
|
@Column(name = "place_name")
|
||||||
private String place;
|
private String place;
|
||||||
|
|
||||||
public Place() {}
|
|
||||||
public Place(String place) { this.place = place; }
|
|
||||||
|
|
||||||
public Long getId() { return id; }
|
|
||||||
public void setId(Long id) { this.id = id; }
|
|
||||||
public String getPlace() { return place; }
|
|
||||||
public void setPlace(String place) { this.place = place; }
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package com.example.nto.exception;
|
||||||
|
|
||||||
|
public class BookingAlreadyExistException extends RuntimeException {
|
||||||
|
public BookingAlreadyExistException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package com.example.nto.exception;
|
||||||
|
|
||||||
|
public class EmployeeNotFoundException extends RuntimeException {
|
||||||
|
public EmployeeNotFoundException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package com.example.nto.exception;
|
||||||
|
|
||||||
|
public class PlaceNotFoundException extends RuntimeException {
|
||||||
|
public PlaceNotFoundException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
package com.example.nto.exception.handler;
|
||||||
|
|
||||||
|
import com.example.nto.exception.BookingAlreadyExistException;
|
||||||
|
import com.example.nto.exception.EmployeeNotFoundException;
|
||||||
|
import com.example.nto.exception.PlaceNotFoundException;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||||
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
|
|
||||||
|
@ControllerAdvice
|
||||||
|
public class GlobalExceptionHandler {
|
||||||
|
@ExceptionHandler(EmployeeNotFoundException.class)
|
||||||
|
public ResponseEntity<String> handleEmployeeNotFoundException(EmployeeNotFoundException e) {
|
||||||
|
return new ResponseEntity<>(e.getMessage(), HttpStatus.UNAUTHORIZED);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(BookingAlreadyExistException.class)
|
||||||
|
public ResponseEntity<String> handleBookingAlreadyExistException(BookingAlreadyExistException e) {
|
||||||
|
return new ResponseEntity<>(e.getMessage(), HttpStatus.CONFLICT);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(PlaceNotFoundException.class)
|
||||||
|
public ResponseEntity<String> handlePlaceNotFoundException(PlaceNotFoundException e) {
|
||||||
|
return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(Exception.class)
|
||||||
|
public ResponseEntity<String> handlerGenericException(Exception e) {
|
||||||
|
return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -10,9 +10,9 @@ import java.util.List;
|
|||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
public interface BookingRepository extends JpaRepository<Booking, Long> {
|
public interface BookingRepository extends JpaRepository<Booking, Long> {
|
||||||
List<Booking> findByEmployee(Employee employee);
|
List<Booking> findByDateBetween(LocalDate start, LocalDate end);
|
||||||
List<Booking> findByDate(LocalDate date);
|
|
||||||
Optional<Booking> findByDateAndPlace(LocalDate date, Place place);
|
Optional<Booking> findByDateAndPlace(LocalDate date, Place place);
|
||||||
|
|
||||||
Optional<Booking> findByEmployeeAndDate(Employee employee, LocalDate date);
|
Optional<Booking> findByDateAndEmployee(LocalDate date, Employee employee);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,12 @@
|
|||||||
package com.example.nto.repository;
|
package com.example.nto.repository;
|
||||||
|
|
||||||
import com.example.nto.entity.Employee;
|
import com.example.nto.entity.Employee;
|
||||||
|
import org.springframework.data.jpa.repository.EntityGraph;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
|
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
|
||||||
|
@EntityGraph(attributePaths = {"bookingList", "bookingList.place"})
|
||||||
Optional<Employee> findByCode(String code);
|
Optional<Employee> findByCode(String code);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,19 +1,15 @@
|
|||||||
package com.example.nto.service;
|
package com.example.nto.service;
|
||||||
|
|
||||||
|
import com.example.nto.controller.dto.BookingCreateDto;
|
||||||
|
import com.example.nto.controller.dto.PlaceDto;
|
||||||
import com.example.nto.entity.Booking;
|
import com.example.nto.entity.Booking;
|
||||||
import com.example.nto.entity.Employee;
|
|
||||||
import com.example.nto.entity.Place;
|
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Map;
|
||||||
|
|
||||||
public interface BookingService {
|
public interface BookingService {
|
||||||
List<Booking> getBookingsByEmployee(Employee employee);
|
Map<LocalDate, List<PlaceDto>> getFreePLace(String code);
|
||||||
List<Booking> getBookingsByDate(LocalDate date);
|
|
||||||
Optional<Booking> getBookingByDateAndPlace(LocalDate date, Place place);
|
|
||||||
|
|
||||||
Optional<Booking> getBookingByEmployeeAndDate(Employee employee, LocalDate date);
|
Booking create(String code, BookingCreateDto bookingCreateDto);
|
||||||
|
|
||||||
Booking createBooking(Employee employee, Place place, LocalDate date);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
package com.example.nto.service;
|
package com.example.nto.service;
|
||||||
|
|
||||||
import com.example.nto.entity.Employee;
|
import com.example.nto.controller.dto.EmployeeDto;
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
public interface EmployeeService {
|
public interface EmployeeService {
|
||||||
Optional<Employee> findByCode(String code);
|
EmployeeDto getByCode(String code);
|
||||||
|
void auth(String code);
|
||||||
}
|
}
|
||||||
@@ -1,10 +0,0 @@
|
|||||||
package com.example.nto.service;
|
|
||||||
|
|
||||||
import com.example.nto.entity.Place;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public interface PlaceService {
|
|
||||||
List<Place> getAllPlaces();
|
|
||||||
Place getPlaceById(Long id);
|
|
||||||
}
|
|
||||||
@@ -1,51 +1,102 @@
|
|||||||
package com.example.nto.service.impl;
|
package com.example.nto.service.impl;
|
||||||
|
|
||||||
|
import com.example.nto.controller.dto.BookingCreateDto;
|
||||||
|
import com.example.nto.controller.dto.PlaceDto;
|
||||||
import com.example.nto.entity.Booking;
|
import com.example.nto.entity.Booking;
|
||||||
import com.example.nto.entity.Employee;
|
import com.example.nto.entity.Employee;
|
||||||
import com.example.nto.entity.Place;
|
import com.example.nto.entity.Place;
|
||||||
|
import com.example.nto.exception.BookingAlreadyExistException;
|
||||||
|
import com.example.nto.exception.EmployeeNotFoundException;
|
||||||
|
import com.example.nto.exception.PlaceNotFoundException;
|
||||||
import com.example.nto.repository.BookingRepository;
|
import com.example.nto.repository.BookingRepository;
|
||||||
|
import com.example.nto.repository.EmployeeRepository;
|
||||||
|
import com.example.nto.repository.PlaceRepository;
|
||||||
import com.example.nto.service.BookingService;
|
import com.example.nto.service.BookingService;
|
||||||
|
import com.example.nto.service.EmployeeService;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.util.List;
|
import java.time.ZoneId;
|
||||||
import java.util.Optional;
|
import java.util.*;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
public class BookingServiceImpl implements BookingService {
|
public class BookingServiceImpl implements BookingService {
|
||||||
|
|
||||||
private final BookingRepository bookingRepository;
|
private final BookingRepository bookingRepository;
|
||||||
|
private final EmployeeRepository employeeRepository;
|
||||||
|
private final PlaceRepository placeRepository;
|
||||||
|
private final EmployeeService employeeService;
|
||||||
|
|
||||||
public BookingServiceImpl(BookingRepository bookingRepository) {
|
@Value("${booking.days-ahead}")
|
||||||
this.bookingRepository = bookingRepository;
|
private int daysAhead;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Transactional(readOnly = true)
|
||||||
|
public Map<LocalDate, List<PlaceDto>> getFreePLace(String code) {
|
||||||
|
employeeService.auth(code);
|
||||||
|
|
||||||
|
List<Place> allPlaces = placeRepository.findAll();
|
||||||
|
|
||||||
|
LocalDate today = LocalDate.now(ZoneId.systemDefault());
|
||||||
|
LocalDate end = today.plusDays(daysAhead);
|
||||||
|
|
||||||
|
List<Booking> bookings = bookingRepository.findByDateBetween(today, end);
|
||||||
|
|
||||||
|
Map<LocalDate, Set<Long>> busyByDate = bookings.stream()
|
||||||
|
.collect(Collectors.groupingBy(Booking::getDate, Collectors.mapping(b -> b.getPlace().getId(), Collectors.toSet())));
|
||||||
|
|
||||||
|
|
||||||
|
Map<LocalDate, List<PlaceDto>> result = new LinkedHashMap<>();
|
||||||
|
|
||||||
|
for (int i = 0; i <= daysAhead; i++){
|
||||||
|
LocalDate currentDate = today.plusDays(i);
|
||||||
|
Set<Long> busyPlaces = busyByDate.getOrDefault(currentDate, Collections.emptySet());
|
||||||
|
|
||||||
|
List<PlaceDto> freePlaces = allPlaces.stream()
|
||||||
|
.filter(place -> busyPlaces.contains(place.getId()))
|
||||||
|
.map(place -> new PlaceDto(place.getId(), place.getPlace()))
|
||||||
|
.toList();
|
||||||
|
result.put(currentDate, freePlaces);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Booking> getBookingsByEmployee(Employee employee) {
|
@Transactional
|
||||||
return bookingRepository.findByEmployee(employee);
|
public Booking create(String code, BookingCreateDto bookingCreateDto) {
|
||||||
}
|
LocalDate date = bookingCreateDto.getDate();
|
||||||
|
LocalDate today = LocalDate.now(ZoneId.systemDefault());
|
||||||
|
if (date.isBefore(today) || date.isAfter(today.plusDays(daysAhead))) {
|
||||||
|
throw new IllegalArgumentException("Date is out of booking window");
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
Employee employee = employeeRepository.findByCode(code)
|
||||||
public List<Booking> getBookingsByDate(LocalDate date) {
|
.orElseThrow(() -> new EmployeeNotFoundException("Employee with " + code + " code not found!"));
|
||||||
return bookingRepository.findByDate(date);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
long placeId = bookingCreateDto.getPlaceId();
|
||||||
public Optional<Booking> getBookingByDateAndPlace(LocalDate date, Place place) {
|
Place place = placeRepository.findById(placeId)
|
||||||
return bookingRepository.findByDateAndPlace(date, place);
|
.orElseThrow(() -> new PlaceNotFoundException("Place with " + placeId + " id not found!"));
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
if (bookingRepository.findByDateAndPlace(date, place).isPresent()) {
|
||||||
public Optional<Booking> getBookingByEmployeeAndDate(Employee employee, LocalDate date) {
|
throw new BookingAlreadyExistException("Booking already exists");
|
||||||
return bookingRepository.findByEmployeeAndDate(employee, date);
|
}
|
||||||
}
|
|
||||||
|
if (bookingRepository.findByDateAndEmployee(date, employee).isPresent()) {
|
||||||
|
throw new BookingAlreadyExistException("This employee already has another booking on " + date);
|
||||||
|
}
|
||||||
|
|
||||||
|
Booking booking = Booking.builder()
|
||||||
|
.date(date)
|
||||||
|
.employee(employee)
|
||||||
|
.place(place)
|
||||||
|
.build();
|
||||||
|
|
||||||
@Override
|
|
||||||
public Booking createBooking(Employee employee, Place place, LocalDate date) {
|
|
||||||
Booking booking = new Booking();
|
|
||||||
booking.setEmployee(employee);
|
|
||||||
booking.setPlace(place);
|
|
||||||
booking.setDate(date);
|
|
||||||
return bookingRepository.save(booking);
|
return bookingRepository.save(booking);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,22 +1,31 @@
|
|||||||
package com.example.nto.service.impl;
|
package com.example.nto.service.impl;
|
||||||
|
|
||||||
import com.example.nto.entity.Employee;
|
import com.example.nto.controller.dto.EmployeeDto;
|
||||||
|
import com.example.nto.exception.EmployeeNotFoundException;
|
||||||
import com.example.nto.repository.EmployeeRepository;
|
import com.example.nto.repository.EmployeeRepository;
|
||||||
import com.example.nto.service.EmployeeService;
|
import com.example.nto.service.EmployeeService;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import java.util.Optional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
public class EmployeeServiceImpl implements EmployeeService {
|
public class EmployeeServiceImpl implements EmployeeService {
|
||||||
|
|
||||||
private final EmployeeRepository employeeRepository;
|
private final EmployeeRepository employeeRepository;
|
||||||
|
|
||||||
public EmployeeServiceImpl(EmployeeRepository employeeRepository) {
|
@Override
|
||||||
this.employeeRepository = employeeRepository;
|
@Transactional(readOnly = true)
|
||||||
|
public EmployeeDto getByCode(String code) {
|
||||||
|
return employeeRepository.findByCode(code).map(EmployeeDto::toDto)
|
||||||
|
.orElseThrow(() -> new EmployeeNotFoundException(("Employee with " + code + " code not found!")));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Optional<Employee> findByCode(String code) {
|
@Transactional(readOnly = true)
|
||||||
return employeeRepository.findByCode(code);
|
public void auth(String code) {
|
||||||
|
if (employeeRepository.findByCode(code).isEmpty()) {
|
||||||
|
throw new EmployeeNotFoundException("Employee with " + code + " code not found!");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,29 +0,0 @@
|
|||||||
package com.example.nto.service.impl;
|
|
||||||
|
|
||||||
import com.example.nto.entity.Place;
|
|
||||||
import com.example.nto.repository.PlaceRepository;
|
|
||||||
import com.example.nto.service.PlaceService;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@Service
|
|
||||||
public class PlaceServiceImpl implements PlaceService {
|
|
||||||
|
|
||||||
private final PlaceRepository placeRepository;
|
|
||||||
|
|
||||||
public PlaceServiceImpl(PlaceRepository placeRepository) {
|
|
||||||
this.placeRepository = placeRepository;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<Place> getAllPlaces() {
|
|
||||||
return placeRepository.findAll();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Place getPlaceById(Long id) {
|
|
||||||
return placeRepository.findById(id)
|
|
||||||
.orElseThrow(() -> new RuntimeException("Place not found"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user