forked from Olympic/NTO-2025-Backend-TeamTask
решение 2 этапа
This commit is contained in:
@@ -1,134 +1,32 @@
|
||||
package com.example.nto.controller;
|
||||
|
||||
import com.example.nto.entity.Booking;
|
||||
import com.example.nto.entity.Employee;
|
||||
import com.example.nto.entity.Place;
|
||||
import com.example.nto.controller.dto.BookingCreateDto;
|
||||
import com.example.nto.controller.dto.PlaceDto;
|
||||
import com.example.nto.service.BookingService;
|
||||
import com.example.nto.service.EmployeeService;
|
||||
import com.example.nto.service.PlaceService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Validated
|
||||
@RestController
|
||||
@RequestMapping("/api/{code}")
|
||||
@RequestMapping("api")
|
||||
@RequiredArgsConstructor
|
||||
public class BookingController {
|
||||
|
||||
private final EmployeeService employeeService;
|
||||
private final BookingService bookingService;
|
||||
private final PlaceService placeService;
|
||||
private static BookingService bookingService;
|
||||
|
||||
public BookingController(EmployeeService employeeService, BookingService bookingService, PlaceService placeService) {
|
||||
this.employeeService = employeeService;
|
||||
this.bookingService = bookingService;
|
||||
this.placeService = placeService;
|
||||
@GetMapping("/{code}/booking")
|
||||
@ResponseStatus(code = HttpStatus.OK)
|
||||
public Map<LocalDate, List<PlaceDto>> getByDate(@PathVariable String code) {
|
||||
return bookingService.getFreePLace(code);
|
||||
}
|
||||
|
||||
@GetMapping("/auth")
|
||||
public ResponseEntity<Void> auth(@PathVariable String code) {
|
||||
Optional<Employee> employeeOpt = employeeService.findByCode(code);
|
||||
if (employeeOpt.isPresent()) {
|
||||
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();
|
||||
}
|
||||
@GetMapping("/{code}/book")
|
||||
@ResponseStatus(code = HttpStatus.CREATED)
|
||||
public void create(@PathVariable String code, @RequestBody BookingCreateDto bookingCreateDto) {
|
||||
bookingService.create(code, bookingCreateDto);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,30 +1,27 @@
|
||||
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 lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/employee")
|
||||
@RequestMapping("api")
|
||||
@RequiredArgsConstructor
|
||||
public class EmployeeController {
|
||||
|
||||
private final EmployeeService employeeService;
|
||||
|
||||
public EmployeeController(EmployeeService employeeService) {
|
||||
this.employeeService = employeeService;
|
||||
@GetMapping("/{code}/auth")
|
||||
@ResponseStatus(code = HttpStatus.OK)
|
||||
public void login(@PathVariable String code) {
|
||||
employeeService.auth(code);
|
||||
}
|
||||
|
||||
@GetMapping("/{code}")
|
||||
public ResponseEntity<Employee> getEmployeeByCode(@PathVariable String code) {
|
||||
Optional<Employee> employeeOpt = employeeService.findByCode(code);
|
||||
if (employeeOpt.isPresent()) {
|
||||
return ResponseEntity.ok(employeeOpt.get());
|
||||
} else {
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
|
||||
}
|
||||
@GetMapping("/{code}/info")
|
||||
@ResponseStatus(code = HttpStatus.OK)
|
||||
public EmployeeDto getByCode(@PathVariable String code) {
|
||||
return employeeService.getByCode(code);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import jakarta.persistence.*;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
@Data
|
||||
@Entity
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Table(name = "booking")
|
||||
@JsonIgnoreProperties({"hibernateLazyInitializer"})
|
||||
public class Booking {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private long id;
|
||||
|
||||
@Column(name = "date")
|
||||
private LocalDate date;
|
||||
|
||||
@ManyToOne(fetch = FetchType.EAGER)
|
||||
@ManyToOne(targetEntity = Place.class, fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "place_id")
|
||||
private Place place;
|
||||
|
||||
@ManyToOne(fetch = FetchType.EAGER)
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "employee_id")
|
||||
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;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import jakarta.persistence.*;
|
||||
import java.util.ArrayList;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
@Entity
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Table(name = "employee")
|
||||
public class Employee {
|
||||
|
||||
@@ -13,33 +20,16 @@ public class Employee {
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private long id;
|
||||
|
||||
@Column(name = "name")
|
||||
private String name;
|
||||
|
||||
@Column(unique = true)
|
||||
@Column(name = "code")
|
||||
private String code;
|
||||
|
||||
@Column(name = "photo_url")
|
||||
private String photoUrl;
|
||||
|
||||
@OneToMany(mappedBy = "employee", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
|
||||
@JsonIgnore
|
||||
private List<Booking> bookingList = new ArrayList<>();
|
||||
private List<Booking> bookingList;
|
||||
|
||||
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;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@Entity
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Table(name = "place")
|
||||
public class Place {
|
||||
|
||||
@Id
|
||||
@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;
|
||||
|
||||
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;
|
||||
|
||||
public interface BookingRepository extends JpaRepository<Booking, Long> {
|
||||
List<Booking> findByEmployee(Employee employee);
|
||||
List<Booking> findByDate(LocalDate date);
|
||||
List<Booking> findByDateBetween(LocalDate start, LocalDate end);
|
||||
|
||||
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;
|
||||
|
||||
import com.example.nto.entity.Employee;
|
||||
import org.springframework.data.jpa.repository.EntityGraph;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
|
||||
@EntityGraph(attributePaths = {"bookingList", "bookingList.place"})
|
||||
Optional<Employee> findByCode(String code);
|
||||
}
|
||||
|
||||
@@ -1,19 +1,15 @@
|
||||
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.Employee;
|
||||
import com.example.nto.entity.Place;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Map;
|
||||
|
||||
public interface BookingService {
|
||||
List<Booking> getBookingsByEmployee(Employee employee);
|
||||
List<Booking> getBookingsByDate(LocalDate date);
|
||||
Optional<Booking> getBookingByDateAndPlace(LocalDate date, Place place);
|
||||
Map<LocalDate, List<PlaceDto>> getFreePLace(String code);
|
||||
|
||||
Optional<Booking> getBookingByEmployeeAndDate(Employee employee, LocalDate date);
|
||||
|
||||
Booking createBooking(Employee employee, Place place, LocalDate date);
|
||||
Booking create(String code, BookingCreateDto bookingCreateDto);
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package com.example.nto.service;
|
||||
|
||||
import com.example.nto.entity.Employee;
|
||||
import java.util.Optional;
|
||||
import com.example.nto.controller.dto.EmployeeDto;
|
||||
|
||||
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;
|
||||
|
||||
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.Employee;
|
||||
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.EmployeeRepository;
|
||||
import com.example.nto.repository.PlaceRepository;
|
||||
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.transaction.annotation.Transactional;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.time.ZoneId;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class BookingServiceImpl implements BookingService {
|
||||
|
||||
private final BookingRepository bookingRepository;
|
||||
private final EmployeeRepository employeeRepository;
|
||||
private final PlaceRepository placeRepository;
|
||||
private final EmployeeService employeeService;
|
||||
|
||||
public BookingServiceImpl(BookingRepository bookingRepository) {
|
||||
this.bookingRepository = bookingRepository;
|
||||
@Value("${booking.days-ahead}")
|
||||
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
|
||||
public List<Booking> getBookingsByEmployee(Employee employee) {
|
||||
return bookingRepository.findByEmployee(employee);
|
||||
}
|
||||
@Transactional
|
||||
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
|
||||
public List<Booking> getBookingsByDate(LocalDate date) {
|
||||
return bookingRepository.findByDate(date);
|
||||
}
|
||||
Employee employee = employeeRepository.findByCode(code)
|
||||
.orElseThrow(() -> new EmployeeNotFoundException("Employee with " + code + " code not found!"));
|
||||
|
||||
@Override
|
||||
public Optional<Booking> getBookingByDateAndPlace(LocalDate date, Place place) {
|
||||
return bookingRepository.findByDateAndPlace(date, place);
|
||||
}
|
||||
long placeId = bookingCreateDto.getPlaceId();
|
||||
Place place = placeRepository.findById(placeId)
|
||||
.orElseThrow(() -> new PlaceNotFoundException("Place with " + placeId + " id not found!"));
|
||||
|
||||
@Override
|
||||
public Optional<Booking> getBookingByEmployeeAndDate(Employee employee, LocalDate date) {
|
||||
return bookingRepository.findByEmployeeAndDate(employee, date);
|
||||
}
|
||||
if (bookingRepository.findByDateAndPlace(date, place).isPresent()) {
|
||||
throw new BookingAlreadyExistException("Booking already exists");
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,22 +1,31 @@
|
||||
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.service.EmployeeService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import java.util.Optional;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class EmployeeServiceImpl implements EmployeeService {
|
||||
|
||||
private final EmployeeRepository employeeRepository;
|
||||
|
||||
public EmployeeServiceImpl(EmployeeRepository employeeRepository) {
|
||||
this.employeeRepository = employeeRepository;
|
||||
@Override
|
||||
@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
|
||||
public Optional<Employee> findByCode(String code) {
|
||||
return employeeRepository.findByCode(code);
|
||||
@Transactional(readOnly = true)
|
||||
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