forked from Olympic/NTO-2025-Backend-TeamTask
Compare commits
1 Commits
bbd0182ae8
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 83b3202ea2 |
@@ -51,7 +51,7 @@
|
|||||||
| **GET** | `api/<CODE>/auth` | Проверка авторизации | `<CODE>` - код для входа | `400` - что-то пошло не так<br>`401` - кода не существует<br>`200` - данный код существует - можно пользоваться приложением |
|
| **GET** | `api/<CODE>/auth` | Проверка авторизации | `<CODE>` - код для входа | `400` - что-то пошло не так<br>`401` - кода не существует<br>`200` - данный код существует - можно пользоваться приложением |
|
||||||
| **GET** | `api/<CODE>/info` | Получение информации о пользователе | `<CODE>` - код для входа | `400` - что-то пошло не так<br>`401` - кода не существует<br>`200` - ОК<br><pre>{<br> "name":"Иванов Петр Федорович",<br> "photoUrl":"<https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg>",<br> "booking":{<br> "2025-01-05": {"id":1,"place":"102"},<br> "2025-01-06": {"id":2,"place":"209.13"},<br> "2025-01-09": {"id":3,"place":"Зона 51. 50"}<br> }<br>}</pre> |
|
| **GET** | `api/<CODE>/info` | Получение информации о пользователе | `<CODE>` - код для входа | `400` - что-то пошло не так<br>`401` - кода не существует<br>`200` - ОК<br><pre>{<br> "name":"Иванов Петр Федорович",<br> "photoUrl":"<https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg>",<br> "booking":{<br> "2025-01-05": {"id":1,"place":"102"},<br> "2025-01-06": {"id":2,"place":"209.13"},<br> "2025-01-09": {"id":3,"place":"Зона 51. 50"}<br> }<br>}</pre> |
|
||||||
| **GET** | `api/<CODE>/booking` | Получение доступных для бронирования мест | `<CODE>` - код для входа | `400` - что-то пошло не так<br>`401` - кода не существует<br>`200` - ОК<br><pre>{<br> "2025-01-05": [{"id": 1, "place": "102"},{"id": 2, "place": "209.13"}],<br> "2025-01-06": [{"id": 3, "place": "Зона 51. 50"}],<br> "2025-01-07": [{"id": 1, "place": "102"},{"id": 2, "place": "209.13"}],<br> "2025-01-08": [{"id": 2, "place": "209.13"}]<br>}</pre> **Список дат ограничен текущим + 3 днями** (ответ от сервера содержит 4 дня со свободными местами для каждого)
|
| **GET** | `api/<CODE>/booking` | Получение доступных для бронирования мест | `<CODE>` - код для входа | `400` - что-то пошло не так<br>`401` - кода не существует<br>`200` - ОК<br><pre>{<br> "2025-01-05": [{"id": 1, "place": "102"},{"id": 2, "place": "209.13"}],<br> "2025-01-06": [{"id": 3, "place": "Зона 51. 50"}],<br> "2025-01-07": [{"id": 1, "place": "102"},{"id": 2, "place": "209.13"}],<br> "2025-01-08": [{"id": 2, "place": "209.13"}]<br>}</pre> **Список дат ограничен текущим + 3 днями** (ответ от сервера содержит 4 дня со свободными местами для каждого)
|
||||||
| **POST** | `api/<CODE>/book` | Создание нового бронирования | `<CODE>` - код для входа<br>Тело: <br> <pre>{<br> “date”: “2025-01-05”,<br> “placeID”: 1 <br>}</pre> |`400` - что-то пошло не так<br>`401` - кода не существует<br>`409` - уже забронировано<br>`201` - бронирование успешно создано
|
| **POST** | `api/<CODE>/book` | Создание нового бронирования | `<CODE>` - код для входа<br>Тело: <br> <pre>{<br> “date”: “2025-01-05”,<br> “placeId”: 1 <br>}</pre> |`400` - что-то пошло не так<br>`401` - кода не существует<br>`409` - уже забронировано<br>`201` - бронирование успешно создано
|
||||||
|
|
||||||
|
|
||||||
## 📊 Пример данных
|
## 📊 Пример данных
|
||||||
|
|||||||
@@ -1,8 +1,4 @@
|
|||||||
package com.example.nto;
|
package com.example.nto;
|
||||||
import org.springframework.boot.SpringApplication;
|
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO: ДОРАБОТАТЬ в рамках задания
|
* TODO: ДОРАБОТАТЬ в рамках задания
|
||||||
@@ -10,9 +6,7 @@ import org.springframework.web.bind.annotation.RestController;
|
|||||||
* МОЖНО: Добавлять методы, аннотации, зависимости
|
* МОЖНО: Добавлять методы, аннотации, зависимости
|
||||||
* НЕЛЬЗЯ: Изменять название класса и пакета
|
* НЕЛЬЗЯ: Изменять название класса и пакета
|
||||||
*/
|
*/
|
||||||
@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,108 +1,10 @@
|
|||||||
package com.example.nto.controller;
|
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.excepation.EmployeeNotFoundException;
|
|
||||||
import com.example.nto.repository.BookingRepository;
|
|
||||||
import com.example.nto.repository.PlaceRepository;
|
|
||||||
import com.example.nto.service.BookingService;
|
|
||||||
import com.example.nto.service.EmployeeService;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.http.HttpStatus;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
|
||||||
import org.springframework.web.bind.annotation.*;
|
|
||||||
|
|
||||||
import java.time.LocalDate;
|
|
||||||
import java.util.LinkedHashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO: ДОРАБОТАТЬ в рамках задания
|
* TODO: ДОРАБОТАТЬ в рамках задания
|
||||||
* =================================
|
* =================================
|
||||||
* МОЖНО: Добавлять методы, аннотации, зависимости
|
* МОЖНО: Добавлять методы, аннотации, зависимости
|
||||||
* НЕЛЬЗЯ: Изменять название класса и пакета
|
* НЕЛЬЗЯ: Изменять название класса и пакета
|
||||||
*/
|
*/
|
||||||
@RestController
|
|
||||||
@RequestMapping("/api")
|
|
||||||
public class BookingController {
|
public class BookingController {
|
||||||
@Autowired
|
|
||||||
private BookingService bookingService;
|
|
||||||
@Autowired
|
|
||||||
private PlaceRepository placeRepository;
|
|
||||||
@Autowired
|
|
||||||
private BookingRepository bookingRepository;
|
|
||||||
@Autowired
|
|
||||||
private EmployeeService employeeService;
|
|
||||||
|
|
||||||
@GetMapping("/{code}/booking")
|
|
||||||
public ResponseEntity<?> getAvailableBookings(@PathVariable String code) {
|
|
||||||
try {
|
|
||||||
Map<String, Object> response = bookingService.findAvailableBookings(code);
|
|
||||||
return ResponseEntity.ok(response);
|
|
||||||
} catch (IllegalArgumentException e) {
|
|
||||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
|
|
||||||
} catch (EmployeeNotFoundException e) {
|
|
||||||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(e.getMessage());
|
|
||||||
} catch (Exception e) {
|
|
||||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Unexpected error");
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@PostMapping("/{code}/book")
|
|
||||||
public ResponseEntity<?> createBooking(
|
|
||||||
@PathVariable String code,
|
|
||||||
@RequestParam Long placeId,
|
|
||||||
@RequestParam String date
|
|
||||||
) {
|
|
||||||
try {
|
|
||||||
if (placeId == null || date == null || date.isEmpty()) {
|
|
||||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
|
|
||||||
.body("Place ID and date must be provided");
|
|
||||||
}
|
|
||||||
Employee employee = employeeService.getEmployeeWithBookings(code);
|
|
||||||
if (employee == null) {
|
|
||||||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
|
|
||||||
.body("Employee not found");
|
|
||||||
}
|
|
||||||
Place place = placeRepository.findById(placeId).orElse(null);
|
|
||||||
if (place == null) {
|
|
||||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
|
|
||||||
.body("Place not found");
|
|
||||||
}
|
|
||||||
LocalDate bookingDate;
|
|
||||||
try {
|
|
||||||
bookingDate = LocalDate.parse(date);
|
|
||||||
} catch (Exception ex) {
|
|
||||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
|
|
||||||
.body("Invalid date format. Use yyyy-MM-dd");
|
|
||||||
}
|
|
||||||
boolean exists = bookingRepository.findByDateBetween(bookingDate, bookingDate)
|
|
||||||
.stream()
|
|
||||||
.anyMatch(b -> b.getPlace().getId() == placeId);
|
|
||||||
if (exists) {
|
|
||||||
return ResponseEntity.status(HttpStatus.CONFLICT)
|
|
||||||
.body("Place already booked for this date");
|
|
||||||
}
|
|
||||||
Booking booking = new Booking();
|
|
||||||
booking.setEmployee((Map<String, Object>) employee);
|
|
||||||
booking.setPlace(place);
|
|
||||||
booking.setDate(bookingDate);
|
|
||||||
bookingRepository.save(booking);
|
|
||||||
Map<String, Object> response = new LinkedHashMap<>();
|
|
||||||
response.put("id", booking.getId());
|
|
||||||
response.put("date", booking.getDate());
|
|
||||||
response.put("placeId", place.getId());
|
|
||||||
response.put("employeeId", employee.getId());
|
|
||||||
return ResponseEntity.status(HttpStatus.CREATED).body(response);
|
|
||||||
} catch (java.lang.IllegalArgumentException e) {
|
|
||||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
|
|
||||||
} catch (Exception e) {
|
|
||||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Unexpected error");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,4 @@
|
|||||||
package com.example.nto.controller;
|
package com.example.nto.controller;
|
||||||
import com.example.nto.entity.Employee;
|
|
||||||
import com.example.nto.excepation.EmployeeNotFoundException;
|
|
||||||
import com.example.nto.service.EmployeeService;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.http.HttpStatus;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
|
||||||
import org.springframework.web.bind.annotation.*;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO: ДОРАБОТАТЬ в рамках задания
|
* TODO: ДОРАБОТАТЬ в рамках задания
|
||||||
@@ -13,36 +6,5 @@ import org.springframework.web.bind.annotation.*;
|
|||||||
* МОЖНО: Добавлять методы, аннотации, зависимости
|
* МОЖНО: Добавлять методы, аннотации, зависимости
|
||||||
* НЕЛЬЗЯ: Изменять название класса и пакета
|
* НЕЛЬЗЯ: Изменять название класса и пакета
|
||||||
*/
|
*/
|
||||||
@RestController
|
|
||||||
@RequestMapping("/api")
|
|
||||||
public class EmployeeController {
|
public class EmployeeController {
|
||||||
@Autowired
|
|
||||||
private EmployeeService employeeService;
|
|
||||||
@GetMapping("/{code}/auth")
|
|
||||||
public ResponseEntity<Void> checkAuth(
|
|
||||||
@PathVariable String code,
|
|
||||||
@RequestHeader(value = "Authorization", required = false) String authHeader) {
|
|
||||||
if (authHeader == null || !authHeader.equals("Bearer valid-token")) {
|
|
||||||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
employeeService. getEmployeeWithBookings(code);
|
|
||||||
return ResponseEntity.ok().build();
|
|
||||||
} catch (IllegalArgumentException e) {
|
|
||||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
|
|
||||||
} catch (EmployeeNotFoundException e) {
|
|
||||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@GetMapping("/{code}/info")
|
|
||||||
public ResponseEntity<?> getEmployeeInfo(@PathVariable String code) {
|
|
||||||
try {
|
|
||||||
Employee response = employeeService.getEmployeeWithBookings(code);
|
|
||||||
return ResponseEntity.ok(response);
|
|
||||||
} catch (IllegalArgumentException e) {
|
|
||||||
return ResponseEntity.status(400).body(e.getMessage());
|
|
||||||
} catch (EmployeeNotFoundException e) {
|
|
||||||
return ResponseEntity.status(404).body(e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
package com.example.nto.controller;
|
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
|
||||||
import lombok.Builder;
|
|
||||||
import lombok.Data;
|
|
||||||
import lombok.NoArgsConstructor;
|
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
@Data
|
|
||||||
@Builder
|
|
||||||
@NoArgsConstructor
|
|
||||||
@AllArgsConstructor
|
|
||||||
public class EmployeeFullInfoResponseDto {
|
|
||||||
private String name;
|
|
||||||
private String photoUrl;
|
|
||||||
private Map<String, BookingInfo> booking;
|
|
||||||
|
|
||||||
@Data
|
|
||||||
@Builder
|
|
||||||
@NoArgsConstructor
|
|
||||||
@AllArgsConstructor
|
|
||||||
public static class BookingInfo {
|
|
||||||
private Long id;
|
|
||||||
private String place;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,13 +1,14 @@
|
|||||||
package com.example.nto.entity;
|
package com.example.nto.entity;
|
||||||
|
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.FetchType;
|
||||||
|
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;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -20,45 +21,15 @@ import java.util.Map;
|
|||||||
@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;
|
||||||
|
|
||||||
private LocalDate date;
|
private LocalDate date;
|
||||||
|
|
||||||
@ManyToOne(targetEntity = Place.class, fetch = FetchType.LAZY)
|
@ManyToOne(targetEntity = Place.class, fetch = FetchType.LAZY)
|
||||||
@JoinColumn(name = "place_id")
|
@JoinColumn(name = "place_id")
|
||||||
private Place place;
|
private Place place;
|
||||||
@ManyToOne(fetch = FetchType.LAZY)
|
|
||||||
@JoinColumn(name = "employee_id")
|
|
||||||
private Employee employee;
|
private Employee employee;
|
||||||
|
|
||||||
|
|
||||||
public Place getPlace() {
|
|
||||||
return place;
|
|
||||||
}
|
|
||||||
public LocalDate getDate() {
|
|
||||||
return date;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Object getId() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Employee getEmployee() {
|
|
||||||
return employee;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setDate(LocalDate date) {
|
|
||||||
this.date = date;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setPlace(Place place) {
|
|
||||||
this.place = place;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setEmployee(Map<String, Object> employee) {
|
|
||||||
this.employee = (Employee) employee;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
package com.example.nto.entity;
|
package com.example.nto.entity;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
@@ -19,12 +19,9 @@ import java.util.List;
|
|||||||
@Builder
|
@Builder
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@Entity
|
|
||||||
@Table(name="employee")
|
|
||||||
public class Employee {
|
public class Employee {
|
||||||
@Id
|
|
||||||
private long id;
|
|
||||||
|
|
||||||
|
private long id;
|
||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
@@ -33,26 +30,5 @@ public class Employee {
|
|||||||
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;
|
||||||
|
|
||||||
public List<Booking> getBookingList() {
|
|
||||||
return bookingList;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Object getCode() {
|
|
||||||
return code;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Object getPhotoUrl() {
|
|
||||||
return photoUrl;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Object getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Object getId() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
package com.example.nto.entity;
|
package com.example.nto.entity;
|
||||||
|
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.GeneratedValue;
|
||||||
|
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;
|
||||||
@@ -17,26 +19,11 @@ 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)
|
||||||
private long id;
|
private long id;
|
||||||
@Column(name = "place_name")
|
|
||||||
private String place;
|
private String place;
|
||||||
|
|
||||||
public Place(int id, String place) {
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public Object getPlace() {
|
|
||||||
return place;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Long getId() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +0,0 @@
|
|||||||
package com.example.nto.excepation;
|
|
||||||
|
|
||||||
public class EmployeeNotFoundException extends RuntimeException{
|
|
||||||
public EmployeeNotFoundException(String msg) {
|
|
||||||
super(msg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
package com.example.nto.excepation;
|
|
||||||
|
|
||||||
public class IllegalArgumentException extends RuntimeException {
|
|
||||||
public IllegalArgumentException(String message) {
|
|
||||||
super(message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,21 +1,10 @@
|
|||||||
package com.example.nto.repository;
|
package com.example.nto.repository;
|
||||||
|
|
||||||
import com.example.nto.entity.Booking;
|
|
||||||
import com.example.nto.entity.Employee;
|
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
|
||||||
import org.springframework.stereotype.Repository;
|
|
||||||
|
|
||||||
import java.time.LocalDate;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO: ДОРАБОТАТЬ в рамках задания
|
* TODO: ДОРАБОТАТЬ в рамках задания
|
||||||
* =================================
|
* =================================
|
||||||
* МОЖНО: Добавлять методы, аннотации, зависимости
|
* МОЖНО: Добавлять методы, аннотации, зависимости
|
||||||
* НЕЛЬЗЯ: Изменять название класса и пакета
|
* НЕЛЬЗЯ: Изменять название класса и пакета
|
||||||
*/
|
*/
|
||||||
@Repository
|
public interface BookingRepository {
|
||||||
public interface BookingRepository extends JpaRepository<Booking, Long> {
|
|
||||||
List<Booking> findByEmployee(Employee employee);
|
|
||||||
List<Booking> findByDateBetween(LocalDate start, LocalDate end);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,16 +1,10 @@
|
|||||||
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;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO: ДОРАБОТАТЬ в рамках задания
|
* TODO: ДОРАБОТАТЬ в рамках задания
|
||||||
* =================================
|
* =================================
|
||||||
* МОЖНО: Добавлять методы, аннотации, зависимости
|
* МОЖНО: Добавлять методы, аннотации, зависимости
|
||||||
* НЕЛЬЗЯ: Изменять название класса и пакета
|
* НЕЛЬЗЯ: Изменять название класса и пакета
|
||||||
*/
|
*/
|
||||||
@Repository
|
public interface EmployeeRepository {
|
||||||
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
|
|
||||||
Employee findByCode(String code);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,10 @@
|
|||||||
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: ДОРАБОТАТЬ в рамках задания
|
||||||
* =================================
|
* =================================
|
||||||
* МОЖНО: Добавлять методы, аннотации, зависимости
|
* МОЖНО: Добавлять методы, аннотации, зависимости
|
||||||
* НЕЛЬЗЯ: Изменять название класса и пакета
|
* НЕЛЬЗЯ: Изменять название класса и пакета
|
||||||
*/
|
*/
|
||||||
@Repository
|
public interface PlaceRepository {
|
||||||
public interface PlaceRepository extends JpaRepository<Place, Long> {
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
package com.example.nto.service;
|
package com.example.nto.service;
|
||||||
|
|
||||||
import org.springframework.stereotype.Service;
|
/**
|
||||||
import java.util.Map;
|
* TODO: ДОРАБОТАТЬ в рамках задания
|
||||||
|
* =================================
|
||||||
@Service
|
* МОЖНО: Добавлять методы, аннотации, зависимости
|
||||||
|
* НЕЛЬЗЯ: Изменять название класса и пакета
|
||||||
|
*/
|
||||||
public interface BookingService {
|
public interface BookingService {
|
||||||
Map<String, Object> findAvailableBookings(String employeeCode);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
package com.example.nto.service;
|
package com.example.nto.service;
|
||||||
import com.example.nto.entity.Employee;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO: ДОРАБОТАТЬ в рамках задания
|
* TODO: ДОРАБОТАТЬ в рамках задания
|
||||||
@@ -8,7 +6,5 @@ import org.springframework.stereotype.Service;
|
|||||||
* МОЖНО: Добавлять методы, аннотации, зависимости
|
* МОЖНО: Добавлять методы, аннотации, зависимости
|
||||||
* НЕЛЬЗЯ: Изменять название класса и пакета
|
* НЕЛЬЗЯ: Изменять название класса и пакета
|
||||||
*/
|
*/
|
||||||
@Service
|
|
||||||
public interface EmployeeService {
|
public interface EmployeeService {
|
||||||
Employee getEmployeeWithBookings(String code);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,24 +0,0 @@
|
|||||||
package com.example.nto.service;
|
|
||||||
import com.example.nto.entity.Place;
|
|
||||||
import com.example.nto.repository.PlaceRepository;
|
|
||||||
import jakarta.annotation.PostConstruct;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
@Service
|
|
||||||
public class PlaceServise {
|
|
||||||
private final PlaceRepository placeRepository;
|
|
||||||
public PlaceServise(PlaceRepository placeRepository) {
|
|
||||||
this.placeRepository = placeRepository;
|
|
||||||
}
|
|
||||||
|
|
||||||
@PostConstruct
|
|
||||||
public void Places() {
|
|
||||||
if(placeRepository.count() == 0) {
|
|
||||||
placeRepository.save(new Place(1,"K-19"));
|
|
||||||
placeRepository.save(new Place(2,"M-16"));
|
|
||||||
placeRepository.save(new Place(3,"T-1"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,63 +1,12 @@
|
|||||||
package com.example.nto.service.impl;
|
package com.example.nto.service.impl;
|
||||||
|
|
||||||
import com.example.nto.entity.Booking;
|
|
||||||
import com.example.nto.entity.Employee;
|
|
||||||
import com.example.nto.entity.Place;
|
|
||||||
import com.example.nto.excepation.EmployeeNotFoundException;
|
|
||||||
import com.example.nto.repository.BookingRepository;
|
|
||||||
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 org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
import java.time.LocalDate;
|
/**
|
||||||
import java.util.*;
|
* TODO: ДОРАБОТАТЬ в рамках задания
|
||||||
|
* =================================
|
||||||
@Service
|
* МОЖНО: Добавлять методы, аннотации, зависимости
|
||||||
|
* НЕЛЬЗЯ: Изменять название класса и пакета
|
||||||
|
*/
|
||||||
public class BookingServiceImpl implements BookingService {
|
public class BookingServiceImpl implements BookingService {
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private BookingRepository bookingRepository;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private PlaceRepository placeRepository;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private EmployeeService employeeService;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Map<String, Object> findAvailableBookings(String employeeCode) {
|
|
||||||
if (employeeCode == null || employeeCode.isEmpty()) {
|
|
||||||
throw new IllegalArgumentException("Employee code cannot be null or empty");
|
|
||||||
}
|
}
|
||||||
Employee employee = employeeService.getEmployeeWithBookings(employeeCode);
|
|
||||||
if (employee == null) {
|
|
||||||
throw new EmployeeNotFoundException("Employee not found with code: " + employeeCode);
|
|
||||||
}
|
|
||||||
LocalDate today = LocalDate.now();
|
|
||||||
LocalDate endDate = today.plusDays(3);
|
|
||||||
List<Booking> bookings = bookingRepository.findByDateBetween(today, endDate);
|
|
||||||
List<Place> places = placeRepository.findAll();
|
|
||||||
Map<String, List<Map<String, Object>>> result = new LinkedHashMap<>();
|
|
||||||
for (int i = 0; i <= 3; i++) {
|
|
||||||
LocalDate date = today.plusDays(i);
|
|
||||||
List<Map<String, Object>> freePlaces = new ArrayList<>();
|
|
||||||
for (Place place : places) {
|
|
||||||
boolean isBooked = bookings.stream()
|
|
||||||
.anyMatch(b -> b.getDate().isEqual(date) && b.getPlace().getId() == place.getId());
|
|
||||||
if (!isBooked) {
|
|
||||||
Map<String, Object> placeInfo = new LinkedHashMap<>();
|
|
||||||
placeInfo.put("id", place.getId());
|
|
||||||
placeInfo.put("place", place.getPlace());
|
|
||||||
freePlaces.add(placeInfo);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
result.put(date.toString(), freePlaces);
|
|
||||||
}
|
|
||||||
return Collections.unmodifiableMap(result);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +1,6 @@
|
|||||||
package com.example.nto.service.impl;
|
package com.example.nto.service.impl;
|
||||||
import com.example.nto.entity.Employee;
|
|
||||||
import com.example.nto.excepation.EmployeeNotFoundException;
|
|
||||||
import com.example.nto.repository.BookingRepository;
|
|
||||||
import com.example.nto.repository.EmployeeRepository;
|
|
||||||
import com.example.nto.service.EmployeeService;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
|
import com.example.nto.service.EmployeeService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO: ДОРАБОТАТЬ в рамках задания
|
* TODO: ДОРАБОТАТЬ в рамках задания
|
||||||
@@ -14,20 +8,5 @@ import org.springframework.stereotype.Service;
|
|||||||
* МОЖНО: Добавлять методы, аннотации, зависимости
|
* МОЖНО: Добавлять методы, аннотации, зависимости
|
||||||
* НЕЛЬЗЯ: Изменять название класса и пакета
|
* НЕЛЬЗЯ: Изменять название класса и пакета
|
||||||
*/
|
*/
|
||||||
@Service
|
|
||||||
public class EmployeeServiceImpl implements EmployeeService {
|
public class EmployeeServiceImpl implements EmployeeService {
|
||||||
@Autowired
|
|
||||||
private EmployeeRepository employeeRepository;
|
|
||||||
@Override
|
|
||||||
public Employee getEmployeeWithBookings(String code) {
|
|
||||||
if (code == null || code.isEmpty()) {
|
|
||||||
throw new IllegalArgumentException("Employee code cannot be null or empty");
|
|
||||||
}
|
|
||||||
Employee employee = employeeRepository.findByCode(code);
|
|
||||||
if (employee == null) {
|
|
||||||
throw new EmployeeNotFoundException("Employee not found with code: " + code);
|
|
||||||
}
|
|
||||||
return employee;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user