forked from Olympic/NTO-2025-Backend-TeamTask
all fix
This commit is contained in:
@@ -1,12 +1,17 @@
|
|||||||
package com.example.nto;
|
package com.example.nto;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO: ДОРАБОТАТЬ в рамках задания
|
* TODO: ДОРАБОТАТЬ в рамках задания
|
||||||
* =================================
|
* =================================
|
||||||
* МОЖНО: Добавлять методы, аннотации, зависимости
|
* МОЖНО: Добавлять методы, аннотации, зависимости
|
||||||
* НЕЛЬЗЯ: Изменять название класса и пакета
|
* НЕЛЬЗЯ: Изменять название класса и пакета
|
||||||
*/
|
*/
|
||||||
|
@SpringBootApplication
|
||||||
public class App {
|
public class App {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(App.class, args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,48 @@
|
|||||||
package com.example.nto.controller;
|
package com.example.nto.controller;
|
||||||
|
|
||||||
|
import com.example.nto.dto.CreateBookingRequest;
|
||||||
|
import com.example.nto.dto.PlaceInfo;
|
||||||
|
import com.example.nto.dto.UserInfoResponse;
|
||||||
|
import com.example.nto.service.BookingService;
|
||||||
|
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.time.LocalDate;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO: ДОРАБОТАТЬ в рамках задания
|
* TODO: ДОРАБОТАТЬ в рамках задания
|
||||||
* =================================
|
* =================================
|
||||||
* МОЖНО: Добавлять методы, аннотации, зависимости
|
* МОЖНО: Добавлять методы, аннотации, зависимости
|
||||||
* НЕЛЬЗЯ: Изменять название класса и пакета
|
* НЕЛЬЗЯ: Изменять название класса и пакета
|
||||||
*/
|
*/
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/{code}")
|
||||||
|
@RequiredArgsConstructor
|
||||||
public class BookingController {
|
public class BookingController {
|
||||||
|
|
||||||
|
private final BookingService bookingService;
|
||||||
|
private final EmployeeService employeeService;
|
||||||
|
|
||||||
|
@GetMapping("/info")
|
||||||
|
public ResponseEntity<UserInfoResponse> getUserInfo(@PathVariable String code) {
|
||||||
|
return ResponseEntity.ok(bookingService.getUserInfo(code));
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/booking")
|
||||||
|
public ResponseEntity<Map<LocalDate, List<PlaceInfo>>> getAvailablePlaces(@PathVariable String code) {
|
||||||
|
employeeService.findByCode(code)
|
||||||
|
.orElseThrow(() -> new jakarta.persistence.EntityNotFoundException("Employee not found with code: " + code));
|
||||||
|
return ResponseEntity.ok(bookingService.getAvailablePlaces());
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/book")
|
||||||
|
public ResponseEntity<Void> createBooking(@PathVariable String code, @RequestBody CreateBookingRequest request) {
|
||||||
|
bookingService.createBooking(code, request);
|
||||||
|
return ResponseEntity.status(HttpStatus.CREATED).build();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,30 @@
|
|||||||
package com.example.nto.controller;
|
package com.example.nto.controller;
|
||||||
|
|
||||||
|
import com.example.nto.service.EmployeeService;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO: ДОРАБОТАТЬ в рамках задания
|
* TODO: ДОРАБОТАТЬ в рамках задания
|
||||||
* =================================
|
* =================================
|
||||||
* МОЖНО: Добавлять методы, аннотации, зависимости
|
* МОЖНО: Добавлять методы, аннотации, зависимости
|
||||||
* НЕЛЬЗЯ: Изменять название класса и пакета
|
* НЕЛЬЗЯ: Изменять название класса и пакета
|
||||||
*/
|
*/
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/api/{code}")
|
||||||
|
@RequiredArgsConstructor
|
||||||
public class EmployeeController {
|
public class EmployeeController {
|
||||||
|
|
||||||
|
private final EmployeeService employeeService;
|
||||||
|
|
||||||
|
@GetMapping("/auth")
|
||||||
|
public ResponseEntity<Void> auth(@PathVariable String code) {
|
||||||
|
employeeService.findByCode(code)
|
||||||
|
.orElseThrow(() -> new jakarta.persistence.EntityNotFoundException("Employee not found with code: " + code));
|
||||||
|
return ResponseEntity.ok().build();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package com.example.nto.controller.advice;
|
||||||
|
|
||||||
|
import jakarta.persistence.EntityNotFoundException;
|
||||||
|
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(EntityNotFoundException.class)
|
||||||
|
public ResponseEntity<String> handleEntityNotFoundException(EntityNotFoundException ex) {
|
||||||
|
if (ex.getMessage().contains("Employee not found")) {
|
||||||
|
return new ResponseEntity<>(ex.getMessage(), HttpStatus.UNAUTHORIZED);
|
||||||
|
}
|
||||||
|
return new ResponseEntity<>(ex.getMessage(), HttpStatus.BAD_REQUEST);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(IllegalStateException.class)
|
||||||
|
public ResponseEntity<String> handleIllegalStateException(IllegalStateException ex) {
|
||||||
|
return new ResponseEntity<>(ex.getMessage(), HttpStatus.CONFLICT);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(IllegalArgumentException.class)
|
||||||
|
public ResponseEntity<String> handleIllegalArgumentException(IllegalArgumentException ex) {
|
||||||
|
return new ResponseEntity<>(ex.getMessage(), HttpStatus.BAD_REQUEST);
|
||||||
|
}
|
||||||
|
}
|
||||||
13
src/main/java/com/example/nto/dto/BookingInfo.java
Normal file
13
src/main/java/com/example/nto/dto/BookingInfo.java
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package com.example.nto.dto;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class BookingInfo {
|
||||||
|
private long id;
|
||||||
|
private String place;
|
||||||
|
}
|
||||||
16
src/main/java/com/example/nto/dto/CreateBookingRequest.java
Normal file
16
src/main/java/com/example/nto/dto/CreateBookingRequest.java
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package com.example.nto.dto;
|
||||||
|
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class CreateBookingRequest {
|
||||||
|
private LocalDate date;
|
||||||
|
private Long placeId;
|
||||||
|
}
|
||||||
13
src/main/java/com/example/nto/dto/PlaceInfo.java
Normal file
13
src/main/java/com/example/nto/dto/PlaceInfo.java
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package com.example.nto.dto;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class PlaceInfo {
|
||||||
|
private long id;
|
||||||
|
private String place;
|
||||||
|
}
|
||||||
17
src/main/java/com/example/nto/dto/UserInfoResponse.java
Normal file
17
src/main/java/com/example/nto/dto/UserInfoResponse.java
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package com.example.nto.dto;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class UserInfoResponse {
|
||||||
|
private String name;
|
||||||
|
private String photoUrl;
|
||||||
|
private Map<LocalDate, BookingInfo> booking;
|
||||||
|
}
|
||||||
@@ -1,8 +1,6 @@
|
|||||||
package com.example.nto.entity;
|
package com.example.nto.entity;
|
||||||
|
|
||||||
import jakarta.persistence.FetchType;
|
import jakarta.persistence.*;
|
||||||
import jakarta.persistence.JoinColumn;
|
|
||||||
import jakarta.persistence.ManyToOne;
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
@@ -21,15 +19,22 @@ import java.time.LocalDate;
|
|||||||
@Builder
|
@Builder
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
|
@Entity
|
||||||
|
@Table(name = "booking")
|
||||||
public class Booking {
|
public class Booking {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
private long id;
|
private long id;
|
||||||
|
|
||||||
|
@Column(nullable = false)
|
||||||
private LocalDate date;
|
private LocalDate date;
|
||||||
|
|
||||||
@ManyToOne(targetEntity = Place.class, fetch = FetchType.LAZY)
|
@ManyToOne(fetch = FetchType.LAZY)
|
||||||
@JoinColumn(name = "place_id")
|
@JoinColumn(name = "place_id", nullable = false)
|
||||||
private Place place;
|
private Place place;
|
||||||
|
|
||||||
|
@ManyToOne(fetch = FetchType.LAZY)
|
||||||
|
@JoinColumn(name = "employee_id", nullable = false)
|
||||||
private Employee employee;
|
private Employee employee;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,14 +19,21 @@ import java.util.List;
|
|||||||
@Builder
|
@Builder
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
|
@Entity
|
||||||
|
@Table(name = "employee")
|
||||||
public class Employee {
|
public class Employee {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
private long id;
|
private long id;
|
||||||
|
|
||||||
|
@Column(nullable = false)
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
|
@Column(nullable = false, unique = true)
|
||||||
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)
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
package com.example.nto.entity;
|
package com.example.nto.entity;
|
||||||
|
|
||||||
import jakarta.persistence.GeneratedValue;
|
import jakarta.persistence.*;
|
||||||
import jakarta.persistence.GenerationType;
|
|
||||||
import jakarta.persistence.Id;
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
@@ -19,11 +17,14 @@ import lombok.NoArgsConstructor;
|
|||||||
@Builder
|
@Builder
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
|
@Entity
|
||||||
|
@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)
|
||||||
private String place;
|
private String place;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,22 @@
|
|||||||
package com.example.nto.repository;
|
package com.example.nto.repository;
|
||||||
|
|
||||||
|
import com.example.nto.entity.Booking;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO: ДОРАБОТАТЬ в рамках задания
|
* TODO: ДОРАБОТАТЬ в рамках задания
|
||||||
* =================================
|
* =================================
|
||||||
* МОЖНО: Добавлять методы, аннотации, зависимости
|
* МОЖНО: Добавлять методы, аннотации, зависимости
|
||||||
* НЕЛЬЗЯ: Изменять название класса и пакета
|
* НЕЛЬЗЯ: Изменять название класса и пакета
|
||||||
*/
|
*/
|
||||||
public interface BookingRepository {
|
@Repository
|
||||||
|
public interface BookingRepository extends JpaRepository<Booking, Long> {
|
||||||
|
List<Booking> findAllByDateBetween(LocalDate startDate, LocalDate endDate);
|
||||||
|
boolean existsByDateAndPlaceId(LocalDate date, Long placeId);
|
||||||
|
|
||||||
|
List<Booking> findAllByEmployeeId(Long employeeId);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,19 @@
|
|||||||
package com.example.nto.repository;
|
package com.example.nto.repository;
|
||||||
|
|
||||||
|
import com.example.nto.entity.Employee;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO: ДОРАБОТАТЬ в рамках задания
|
* TODO: ДОРАБОТАТЬ в рамках задания
|
||||||
* =================================
|
* =================================
|
||||||
* МОЖНО: Добавлять методы, аннотации, зависимости
|
* МОЖНО: Добавлять методы, аннотации, зависимости
|
||||||
* НЕЛЬЗЯ: Изменять название класса и пакета
|
* НЕЛЬЗЯ: Изменять название класса и пакета
|
||||||
*/
|
*/
|
||||||
public interface EmployeeRepository {
|
@Repository
|
||||||
|
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
|
||||||
|
|
||||||
|
Optional<Employee> findByCode(String code);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,15 @@
|
|||||||
package com.example.nto.repository;
|
package com.example.nto.repository;
|
||||||
|
|
||||||
|
import com.example.nto.entity.Place;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO: ДОРАБОТАТЬ в рамках задания
|
* TODO: ДОРАБОТАТЬ в рамках задания
|
||||||
* =================================
|
* =================================
|
||||||
* МОЖНО: Добавлять методы, аннотации, зависимости
|
* МОЖНО: Добавлять методы, аннотации, зависимости
|
||||||
* НЕЛЬЗЯ: Изменять название класса и пакета
|
* НЕЛЬЗЯ: Изменять название класса и пакета
|
||||||
*/
|
*/
|
||||||
public interface PlaceRepository {
|
@Repository
|
||||||
|
public interface PlaceRepository extends JpaRepository<Place, Long> {
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,13 @@
|
|||||||
package com.example.nto.service;
|
package com.example.nto.service;
|
||||||
|
|
||||||
|
import com.example.nto.dto.CreateBookingRequest;
|
||||||
|
import com.example.nto.dto.PlaceInfo;
|
||||||
|
import com.example.nto.dto.UserInfoResponse;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO: ДОРАБОТАТЬ в рамках задания
|
* TODO: ДОРАБОТАТЬ в рамках задания
|
||||||
* =================================
|
* =================================
|
||||||
@@ -7,4 +15,10 @@ package com.example.nto.service;
|
|||||||
* НЕЛЬЗЯ: Изменять название класса и пакета
|
* НЕЛЬЗЯ: Изменять название класса и пакета
|
||||||
*/
|
*/
|
||||||
public interface BookingService {
|
public interface BookingService {
|
||||||
|
|
||||||
|
UserInfoResponse getUserInfo(String code);
|
||||||
|
|
||||||
|
Map<LocalDate, List<PlaceInfo>> getAvailablePlaces();
|
||||||
|
|
||||||
|
void createBooking(String code, CreateBookingRequest request);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
package com.example.nto.service;
|
package com.example.nto.service;
|
||||||
|
|
||||||
|
import com.example.nto.entity.Employee;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO: ДОРАБОТАТЬ в рамках задания
|
* TODO: ДОРАБОТАТЬ в рамках задания
|
||||||
* =================================
|
* =================================
|
||||||
@@ -7,4 +11,5 @@ package com.example.nto.service;
|
|||||||
* НЕЛЬЗЯ: Изменять название класса и пакета
|
* НЕЛЬЗЯ: Изменять название класса и пакета
|
||||||
*/
|
*/
|
||||||
public interface EmployeeService {
|
public interface EmployeeService {
|
||||||
|
Optional<Employee> findByCode(String code);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,108 @@
|
|||||||
package com.example.nto.service.impl;
|
package com.example.nto.service.impl;
|
||||||
|
|
||||||
|
import com.example.nto.dto.BookingInfo;
|
||||||
|
import com.example.nto.dto.CreateBookingRequest;
|
||||||
|
import com.example.nto.dto.PlaceInfo;
|
||||||
|
import com.example.nto.dto.UserInfoResponse;
|
||||||
|
import com.example.nto.entity.Booking;
|
||||||
|
import com.example.nto.entity.Employee;
|
||||||
|
import com.example.nto.entity.Place;
|
||||||
|
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 jakarta.persistence.EntityNotFoundException;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
/**
|
import java.time.LocalDate;
|
||||||
* TODO: ДОРАБОТАТЬ в рамках задания
|
import java.util.List;
|
||||||
* =================================
|
import java.util.Map;
|
||||||
* МОЖНО: Добавлять методы, аннотации, зависимости
|
import java.util.function.Function;
|
||||||
* НЕЛЬЗЯ: Изменять название класса и пакета
|
import java.util.stream.Collectors;
|
||||||
*/
|
import java.util.stream.IntStream;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
public class BookingServiceImpl implements BookingService {
|
public class BookingServiceImpl implements BookingService {
|
||||||
|
|
||||||
|
private final BookingRepository bookingRepository;
|
||||||
|
private final PlaceRepository placeRepository;
|
||||||
|
private final EmployeeService employeeService;
|
||||||
|
|
||||||
|
@Value("${booking.days-ahead}")
|
||||||
|
private int daysAhead;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public UserInfoResponse getUserInfo(String code) {
|
||||||
|
Employee employee = employeeService.findByCode(code)
|
||||||
|
.orElseThrow(() -> new EntityNotFoundException("Employee not found with code: " + code));
|
||||||
|
|
||||||
|
List<Booking> bookings = bookingRepository.findAllByEmployeeId(employee.getId());
|
||||||
|
|
||||||
|
Map<LocalDate, BookingInfo> bookingInfoMap = bookings.stream()
|
||||||
|
.collect(Collectors.toMap(
|
||||||
|
Booking::getDate,
|
||||||
|
booking -> new BookingInfo(booking.getId(), booking.getPlace().getPlace())
|
||||||
|
));
|
||||||
|
|
||||||
|
return new UserInfoResponse(employee.getName(), employee.getPhotoUrl(), bookingInfoMap);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<LocalDate, List<PlaceInfo>> getAvailablePlaces() {
|
||||||
|
LocalDate today = LocalDate.now();
|
||||||
|
LocalDate endDate = today.plusDays(daysAhead);
|
||||||
|
|
||||||
|
List<Place> allPlaces = placeRepository.findAll();
|
||||||
|
List<Booking> bookings = bookingRepository.findAllByDateBetween(today, endDate);
|
||||||
|
|
||||||
|
Map<LocalDate, List<Long>> bookedPlaceIdsByDate = bookings.stream()
|
||||||
|
.collect(Collectors.groupingBy(
|
||||||
|
Booking::getDate,
|
||||||
|
Collectors.mapping(booking -> booking.getPlace().getId(), Collectors.toList())
|
||||||
|
));
|
||||||
|
|
||||||
|
return IntStream.rangeClosed(0, daysAhead)
|
||||||
|
.mapToObj(today::plusDays)
|
||||||
|
.collect(Collectors.toMap(
|
||||||
|
Function.identity(),
|
||||||
|
date -> {
|
||||||
|
List<Long> bookedPlaceIds = bookedPlaceIdsByDate.getOrDefault(date, List.of());
|
||||||
|
return allPlaces.stream()
|
||||||
|
.filter(place -> !bookedPlaceIds.contains(place.getId()))
|
||||||
|
.map(place -> new PlaceInfo(place.getId(), place.getPlace()))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void createBooking(String code, CreateBookingRequest request) {
|
||||||
|
Employee employee = employeeService.findByCode(code)
|
||||||
|
.orElseThrow(() -> new EntityNotFoundException("Employee not found with code: " + code));
|
||||||
|
|
||||||
|
placeRepository.findById(request.getPlaceId())
|
||||||
|
.orElseThrow(() -> new EntityNotFoundException("Place not found with id: " + request.getPlaceId()));
|
||||||
|
|
||||||
|
LocalDate today = LocalDate.now();
|
||||||
|
LocalDate endDate = today.plusDays(daysAhead);
|
||||||
|
if (request.getDate().isBefore(today) || request.getDate().isAfter(endDate)) {
|
||||||
|
throw new IllegalArgumentException("Booking date must be between today and " + endDate);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (bookingRepository.existsByDateAndPlaceId(request.getDate(), request.getPlaceId())) {
|
||||||
|
throw new IllegalStateException("Place is already booked on this date");
|
||||||
|
}
|
||||||
|
|
||||||
|
Booking booking = Booking.builder()
|
||||||
|
.date(request.getDate())
|
||||||
|
.employee(employee)
|
||||||
|
.place(Place.builder().id(request.getPlaceId()).build())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
bookingRepository.save(booking);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,12 @@
|
|||||||
package com.example.nto.service.impl;
|
package com.example.nto.service.impl;
|
||||||
|
|
||||||
|
import com.example.nto.entity.Employee;
|
||||||
|
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 java.util.Optional;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO: ДОРАБОТАТЬ в рамках задания
|
* TODO: ДОРАБОТАТЬ в рамках задания
|
||||||
@@ -8,5 +14,14 @@ import com.example.nto.service.EmployeeService;
|
|||||||
* МОЖНО: Добавлять методы, аннотации, зависимости
|
* МОЖНО: Добавлять методы, аннотации, зависимости
|
||||||
* НЕЛЬЗЯ: Изменять название класса и пакета
|
* НЕЛЬЗЯ: Изменять название класса и пакета
|
||||||
*/
|
*/
|
||||||
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
public class EmployeeServiceImpl implements EmployeeService {
|
public class EmployeeServiceImpl implements EmployeeService {
|
||||||
|
|
||||||
|
private final EmployeeRepository employeeRepository;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Optional<Employee> findByCode(String code) {
|
||||||
|
return employeeRepository.findByCode(code);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user