forked from Olympic/NTO-2025-Backend-TeamTask
Compare commits
21 Commits
af09df8047
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c43e4b18fb | ||
|
|
3c3ef45d78 | ||
|
|
3f1c5dbca8 | ||
|
|
46068ca46d | ||
|
|
65d3dd56cd | ||
|
|
d836b0bfe2 | ||
|
|
5d2d30992e | ||
|
|
18b3e950c1 | ||
|
|
0cf5cfdf59 | ||
|
|
ae1cfa3a32 | ||
|
|
2196f7e418 | ||
|
|
4814b6b1c1 | ||
|
|
f83af98397 | ||
|
|
662b109709 | ||
|
|
c4bb98b321 | ||
|
|
3b64ad9626 | ||
| f6acec1b98 | |||
|
|
bbd0182ae8 | ||
|
|
884adb5d6e | ||
|
|
9f437b83e2 | ||
| 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` - бронирование успешно создано
|
||||||
|
|
||||||
|
|
||||||
## 📊 Пример данных
|
## 📊 Пример данных
|
||||||
|
|||||||
@@ -15,11 +15,4 @@ public class App {
|
|||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(App.class , args);
|
SpringApplication.run(App.class , args);
|
||||||
}
|
}
|
||||||
@RestController
|
|
||||||
public static class HomeController {
|
|
||||||
@GetMapping("/")
|
|
||||||
public String home() {
|
|
||||||
return "Сервер работает!";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,22 +1,14 @@
|
|||||||
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.excepation.EmployeeNotFoundException;
|
||||||
import com.example.nto.repository.BookingRepository;
|
import com.example.nto.repository.BookingRepository;
|
||||||
import com.example.nto.repository.PlaceRepository;
|
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 com.example.nto.service.EmployeeService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.format.annotation.DateTimeFormat;
|
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.LinkedHashMap;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -36,10 +28,12 @@ public class BookingController {
|
|||||||
private BookingRepository bookingRepository;
|
private BookingRepository bookingRepository;
|
||||||
@Autowired
|
@Autowired
|
||||||
private EmployeeService employeeService;
|
private EmployeeService employeeService;
|
||||||
|
|
||||||
@GetMapping("/{code}/booking")
|
@GetMapping("/{code}/booking")
|
||||||
public ResponseEntity<?> getAvailableBookings(@PathVariable String code) {
|
public ResponseEntity<?> getAvailableBookings(@PathVariable String code) {
|
||||||
try {
|
try {
|
||||||
|
if (code == null || code.trim().isEmpty()) {
|
||||||
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Employee code cannot be null or empty");
|
||||||
|
}
|
||||||
Map<String, Object> response = bookingService.findAvailableBookings(code);
|
Map<String, Object> response = bookingService.findAvailableBookings(code);
|
||||||
return ResponseEntity.ok(response);
|
return ResponseEntity.ok(response);
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
@@ -50,70 +44,32 @@ public class BookingController {
|
|||||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Unexpected error");
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Unexpected error");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/{code}/book")
|
@PostMapping("/{code}/book")
|
||||||
public ResponseEntity<?> createBooking(
|
public ResponseEntity<?> createBooking(
|
||||||
@PathVariable String code,
|
@PathVariable String code,
|
||||||
@RequestParam Long placeId,
|
@RequestBody Map<String, Object> body
|
||||||
@RequestParam String date
|
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
// Проверка обязательных параметров
|
Long placeId = body.get("placeId") != null ? Long.valueOf(body.get("placeId").toString()) : null;
|
||||||
|
String date = body.get("date") != null ? body.get("date").toString() : null;
|
||||||
if (placeId == null || date == null || date.isEmpty()) {
|
if (placeId == null || date == null || date.isEmpty()) {
|
||||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Place ID and date must be provided");
|
||||||
.body("Place ID and date must be provided");
|
|
||||||
}
|
}
|
||||||
Employee employee = employeeService.getEmployeeWithBookings(code);
|
String message = bookingService.createBooking(code, placeId, date);
|
||||||
if (employee == null) {
|
return ResponseEntity.ok(message);
|
||||||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
|
} catch (IllegalArgumentException e) {
|
||||||
.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());
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
|
||||||
|
} catch (EmployeeNotFoundException e) {
|
||||||
|
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(e.getMessage());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Unexpected error");
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Unexpected error");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@DeleteMapping("/{id}")
|
||||||
|
public ResponseEntity<Void> deleteBooking(@PathVariable Long id) {
|
||||||
|
bookingService.deleteBooking(id);
|
||||||
|
return ResponseEntity.noContent().build();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,19 +1,18 @@
|
|||||||
package com.example.nto.controller;
|
package com.example.nto.controller;
|
||||||
|
|
||||||
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.excepation.EmployeeNotFoundException;
|
import com.example.nto.excepation.EmployeeNotFoundException;
|
||||||
import com.example.nto.repository.BookingRepository;
|
import com.example.nto.repository.BookingRepository;
|
||||||
import com.example.nto.repository.EmployeeRepository;
|
|
||||||
import com.example.nto.service.EmployeeService;
|
import com.example.nto.service.EmployeeService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO: ДОРАБОТАТЬ в рамках задания
|
* TODO: ДОРАБОТАТЬ в рамках задания
|
||||||
@@ -24,35 +23,48 @@ import java.util.Map;
|
|||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/api")
|
@RequestMapping("/api")
|
||||||
public class EmployeeController {
|
public class EmployeeController {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private EmployeeService employeeService;
|
private EmployeeService employeeService;
|
||||||
|
@Autowired
|
||||||
|
private BookingRepository bookingRepository;
|
||||||
|
|
||||||
@GetMapping("/{code}/auth")
|
@GetMapping("/{code}/auth")
|
||||||
public ResponseEntity<Void> checkAuth(
|
public ResponseEntity<String> checkAuth(
|
||||||
@PathVariable String code,
|
@PathVariable String code) {
|
||||||
@RequestHeader(value = "Authorization", required = false) String authHeader) {
|
if (code == null) {
|
||||||
if (authHeader == null || !authHeader.equals("Bearer valid-token")) {
|
throw new IllegalArgumentException("Employee code is required");
|
||||||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
|
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
employeeService. getEmployeeWithBookings(code);
|
employeeService.getEmployeeByCode(code);
|
||||||
return ResponseEntity.ok().build();
|
return ResponseEntity.ok("Authorized Employee");
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
|
||||||
} catch (EmployeeNotFoundException e) {
|
} catch (EmployeeNotFoundException e) {
|
||||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/{code}/info")
|
@GetMapping("/{code}/info")
|
||||||
public ResponseEntity<?> getEmployeeInfo(@PathVariable String code) {
|
public ResponseEntity<?> getEmployeeInfo(@PathVariable String code) {
|
||||||
try {
|
try {
|
||||||
Employee response = employeeService.getEmployeeWithBookings(code);
|
if (code == null || code.trim().isEmpty()) {
|
||||||
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
|
||||||
|
.body("Employee code cannot be null or empty");
|
||||||
|
}
|
||||||
|
Employee employee = employeeService.getEmployeeByCode(code);
|
||||||
|
List<Booking> bookings = bookingRepository.findByEmployeeId(employee.getId());
|
||||||
|
Map<String, List<Map<String, Object>>> bookingMap = bookings.stream().collect(Collectors.groupingBy(b -> b.getDate().toString(),LinkedHashMap::new,Collectors.mapping(b -> Map.of("id", b.getId(),"place", b.getPlace().getPlace()), Collectors.toList())));
|
||||||
|
Map<String, Object> response = new LinkedHashMap<>();
|
||||||
|
response.put("name", employee.getName());
|
||||||
|
response.put("photoUrl", employee.getPhotoUrl());
|
||||||
|
response.put("booking", bookingMap);
|
||||||
return ResponseEntity.ok(response);
|
return ResponseEntity.ok(response);
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
return ResponseEntity.status(400).body(e.getMessage());
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
|
||||||
} catch (EmployeeNotFoundException e) {
|
} catch (EmployeeNotFoundException e) {
|
||||||
return ResponseEntity.status(404).body(e.getMessage());
|
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(e.getMessage());
|
||||||
|
} catch (Exception e) {
|
||||||
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||||
|
.body("Unexpected error");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -7,7 +7,6 @@ import lombok.Data;
|
|||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -57,8 +56,7 @@ public class Booking {
|
|||||||
public void setPlace(Place place) {
|
public void setPlace(Place place) {
|
||||||
this.place = place;
|
this.place = place;
|
||||||
}
|
}
|
||||||
|
public void setEmployee(Employee employee) {
|
||||||
public void setEmployee(Map<String, Object> employee) {
|
|
||||||
this.employee = (Employee) employee;
|
this.employee = (Employee) employee;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,9 @@ import lombok.AllArgsConstructor;
|
|||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
@@ -31,28 +34,11 @@ public class Employee {
|
|||||||
private String code;
|
private String code;
|
||||||
|
|
||||||
private String photoUrl;
|
private String photoUrl;
|
||||||
|
@OneToMany(mappedBy = "employee", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
|
||||||
|
private List<Booking> bookings = new ArrayList<>();
|
||||||
|
|
||||||
@OneToMany(mappedBy = "employee", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
|
public long getId() { return id; }
|
||||||
@JsonIgnore
|
public String getName() { return name; }
|
||||||
private List<Booking> bookingList;
|
public String getCode() { return code; }
|
||||||
|
public String getPhotoUrl() { return photoUrl; }
|
||||||
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,7 +0,0 @@
|
|||||||
package com.example.nto.excepation;
|
|
||||||
|
|
||||||
public class ConflictException extends RuntimeException {
|
|
||||||
public ConflictException(String message) {
|
|
||||||
super(message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
package com.example.nto.excepation;
|
package com.example.nto.excepation;
|
||||||
|
|
||||||
public class EmployeeNotFoundException extends RuntimeException{
|
public class EmployeeNotFoundException extends RuntimeException{
|
||||||
public EmployeeNotFoundException(String msg) {
|
public EmployeeNotFoundException(String message) {
|
||||||
super(msg);
|
super(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,11 @@
|
|||||||
package com.example.nto.repository;
|
package com.example.nto.repository;
|
||||||
|
|
||||||
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 org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO: ДОРАБОТАТЬ в рамках задания
|
* TODO: ДОРАБОТАТЬ в рамках задания
|
||||||
@@ -18,6 +15,6 @@ import java.util.Optional;
|
|||||||
*/
|
*/
|
||||||
@Repository
|
@Repository
|
||||||
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> findByDateBetween(LocalDate start, LocalDate end);
|
||||||
|
List<Booking> findByEmployeeId(long id);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ import com.example.nto.entity.Employee;
|
|||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO: ДОРАБОТАТЬ в рамках задания
|
* TODO: ДОРАБОТАТЬ в рамках задания
|
||||||
* =================================
|
* =================================
|
||||||
@@ -12,5 +14,5 @@ import org.springframework.stereotype.Repository;
|
|||||||
*/
|
*/
|
||||||
@Repository
|
@Repository
|
||||||
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
|
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
|
||||||
Employee findByCode(String code);
|
Optional<Employee> findByCode(String code);
|
||||||
}
|
}
|
||||||
@@ -1,11 +1,11 @@
|
|||||||
package com.example.nto.service;
|
package com.example.nto.service;
|
||||||
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public interface BookingService {
|
public interface BookingService {
|
||||||
Map<String, Object> findAvailableBookings(String employeeCode);
|
Map<String, Object> findAvailableBookings(String employeeCode);
|
||||||
|
String createBooking(String code, Long placeId, String date);
|
||||||
|
void deleteBooking(long id);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,7 @@
|
|||||||
package com.example.nto.service;
|
package com.example.nto.service;
|
||||||
import com.example.nto.entity.Booking;
|
|
||||||
import com.example.nto.entity.Employee;
|
import com.example.nto.entity.Employee;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO: ДОРАБОТАТЬ в рамках задания
|
* TODO: ДОРАБОТАТЬ в рамках задания
|
||||||
* =================================
|
* =================================
|
||||||
@@ -14,5 +10,5 @@ import java.util.Map;
|
|||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
public interface EmployeeService {
|
public interface EmployeeService {
|
||||||
Employee getEmployeeWithBookings(String code);
|
Employee getEmployeeByCode(String code);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,16 +2,13 @@ package com.example.nto.service;
|
|||||||
import com.example.nto.entity.Place;
|
import com.example.nto.entity.Place;
|
||||||
import com.example.nto.repository.PlaceRepository;
|
import com.example.nto.repository.PlaceRepository;
|
||||||
import jakarta.annotation.PostConstruct;
|
import jakarta.annotation.PostConstruct;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class PlaceServise {
|
public class PlaceServise {
|
||||||
private final PlaceRepository placeRepository;
|
@Autowired
|
||||||
|
private PlaceRepository placeRepository;
|
||||||
public PlaceServise(PlaceRepository placeRepository) {
|
|
||||||
this.placeRepository = placeRepository;
|
|
||||||
}
|
|
||||||
|
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
public void Places() {
|
public void Places() {
|
||||||
if(placeRepository.count() == 0) {
|
if(placeRepository.count() == 0) {
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import com.example.nto.entity.Employee;
|
|||||||
import com.example.nto.entity.Place;
|
import com.example.nto.entity.Place;
|
||||||
import com.example.nto.excepation.EmployeeNotFoundException;
|
import com.example.nto.excepation.EmployeeNotFoundException;
|
||||||
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.repository.PlaceRepository;
|
||||||
import com.example.nto.service.BookingService;
|
import com.example.nto.service.BookingService;
|
||||||
import com.example.nto.service.EmployeeService;
|
import com.example.nto.service.EmployeeService;
|
||||||
@@ -16,26 +17,23 @@ import java.util.*;
|
|||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class BookingServiceImpl implements BookingService {
|
public class BookingServiceImpl implements BookingService {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private BookingRepository bookingRepository;
|
private BookingRepository bookingRepository;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private PlaceRepository placeRepository;
|
private PlaceRepository placeRepository;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private EmployeeService employeeService;
|
private EmployeeService employeeService;
|
||||||
|
@Autowired
|
||||||
|
private EmployeeRepository employeeRepository;
|
||||||
@Override
|
@Override
|
||||||
public Map<String, Object> findAvailableBookings(String employeeCode) {
|
public Map<String, Object> findAvailableBookings(String employeeCode) {
|
||||||
if (employeeCode == null || employeeCode.isEmpty()) {
|
if (employeeCode == null || employeeCode.isEmpty()) {
|
||||||
throw new IllegalArgumentException("Employee code cannot be null or empty");
|
throw new IllegalArgumentException("Employee code cannot be null or empty");
|
||||||
}
|
}
|
||||||
Employee employee = employeeService.getEmployeeWithBookings(employeeCode);
|
Employee employee = employeeService.getEmployeeByCode(employeeCode);
|
||||||
if (employee == null) {
|
if (employee == null) {
|
||||||
throw new EmployeeNotFoundException("Employee not found with code: " + employeeCode);
|
throw new EmployeeNotFoundException("Employee not found with code: " + employeeCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
LocalDate today = LocalDate.now();
|
LocalDate today = LocalDate.now();
|
||||||
LocalDate endDate = today.plusDays(3);
|
LocalDate endDate = today.plusDays(3);
|
||||||
List<Booking> bookings = bookingRepository.findByDateBetween(today, endDate);
|
List<Booking> bookings = bookingRepository.findByDateBetween(today, endDate);
|
||||||
@@ -45,8 +43,7 @@ public class BookingServiceImpl implements BookingService {
|
|||||||
LocalDate date = today.plusDays(i);
|
LocalDate date = today.plusDays(i);
|
||||||
List<Map<String, Object>> freePlaces = new ArrayList<>();
|
List<Map<String, Object>> freePlaces = new ArrayList<>();
|
||||||
for (Place place : places) {
|
for (Place place : places) {
|
||||||
boolean isBooked = bookings.stream()
|
boolean isBooked = bookings.stream().anyMatch(b -> b.getDate().isEqual(date) && b.getPlace().getId() == place.getId());
|
||||||
.anyMatch(b -> b.getDate().isEqual(date) && b.getPlace().getId() == place.getId());
|
|
||||||
if (!isBooked) {
|
if (!isBooked) {
|
||||||
Map<String, Object> placeInfo = new LinkedHashMap<>();
|
Map<String, Object> placeInfo = new LinkedHashMap<>();
|
||||||
placeInfo.put("id", place.getId());
|
placeInfo.put("id", place.getId());
|
||||||
@@ -58,7 +55,48 @@ public class BookingServiceImpl implements BookingService {
|
|||||||
}
|
}
|
||||||
return Collections.unmodifiableMap(result);
|
return Collections.unmodifiableMap(result);
|
||||||
}
|
}
|
||||||
|
public String createBooking(String code, Long placeId, String date) {
|
||||||
|
|
||||||
|
if (code == null || code.isBlank()) {
|
||||||
|
throw new IllegalArgumentException("Employee code is empty");
|
||||||
|
}
|
||||||
|
if (placeId == null || date == null || date.isBlank()) {
|
||||||
|
throw new IllegalArgumentException("Place ID and date must be provided");
|
||||||
|
}
|
||||||
|
Employee employee = employeeRepository.findByCode(code).orElseThrow(() -> new EmployeeNotFoundException("Employee not found"));
|
||||||
|
Place place = placeRepository.findById(placeId).orElseThrow(() -> new IllegalArgumentException("Place not found"));
|
||||||
|
LocalDate bookingDate;
|
||||||
|
try {
|
||||||
|
bookingDate = LocalDate.parse(date);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new IllegalArgumentException("Invalid date format. Use yyyy-MM-dd");
|
||||||
|
}
|
||||||
|
boolean exists = bookingRepository.findByDateBetween(bookingDate, bookingDate).stream().anyMatch(
|
||||||
|
b -> b.getPlace()
|
||||||
|
.getId() == placeId);
|
||||||
|
if (exists) {
|
||||||
|
throw new IllegalArgumentException("Place already booked for this date");
|
||||||
|
}
|
||||||
|
Booking booking = new Booking();
|
||||||
|
booking.setEmployee((Employee) employee);
|
||||||
|
booking.setPlace(place);
|
||||||
|
booking.setDate(bookingDate);
|
||||||
|
bookingRepository.save(booking);
|
||||||
|
Map<String, Object> result = new LinkedHashMap<>();
|
||||||
|
result.put("id", booking.getId());
|
||||||
|
result.put("date", booking.getDate());
|
||||||
|
result.put("placeId", place.getId());
|
||||||
|
result.put("employeeId", employee.getId());
|
||||||
|
return "the booking was created successfully";
|
||||||
|
}
|
||||||
|
public void deleteBooking(long id) {
|
||||||
|
if (!bookingRepository.existsById(id)) {
|
||||||
|
throw new IllegalArgumentException("Booking not found with id: " + id);
|
||||||
|
}
|
||||||
|
bookingRepository.deleteById(id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +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.Employee;
|
||||||
import com.example.nto.excepation.EmployeeNotFoundException;
|
import com.example.nto.excepation.EmployeeNotFoundException;
|
||||||
import com.example.nto.repository.BookingRepository;
|
|
||||||
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.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO: ДОРАБОТАТЬ в рамках задания
|
* TODO: ДОРАБОТАТЬ в рамках задания
|
||||||
@@ -18,24 +15,15 @@ import java.util.List;
|
|||||||
* НЕЛЬЗЯ: Изменять название класса и пакета
|
* НЕЛЬЗЯ: Изменять название класса и пакета
|
||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
public class EmployeeServiceImpl implements EmployeeService {
|
public class EmployeeServiceImpl implements EmployeeService {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private EmployeeRepository employeeRepository;
|
private EmployeeRepository employeeRepository;
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private BookingRepository bookingRepository;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Employee getEmployeeWithBookings(String code) {
|
public Employee getEmployeeByCode(String code) {
|
||||||
if (code == null || code.isEmpty()) {
|
if (code == null || code.isEmpty()) {
|
||||||
throw new IllegalArgumentException("Employee code cannot be null or empty");
|
throw new IllegalArgumentException("Employee code is required");
|
||||||
}
|
}
|
||||||
Employee employee = employeeRepository.findByCode(code);
|
return employeeRepository.findByCode(code).orElseThrow(() -> new EmployeeNotFoundException("Employee not found"));
|
||||||
if (employee == null) {
|
|
||||||
throw new EmployeeNotFoundException("Employee not found with code: " + code);
|
|
||||||
}
|
|
||||||
return employee;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user