Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| cda75b85f8 | |||
| bbe7d1ea6c | |||
| a6954c2013 |
@@ -23,7 +23,7 @@ jobs:
|
||||
GITEA_HEAD_REF: ${{ gitea.event.pull_request.head.ref }}
|
||||
|
||||
- name: Checkout tests
|
||||
run: python3 /opt/scripts/copy-tests.py --repo-url "Olympic/NTO-2025-Android-TeamTask-tests" --branch "main" --task-type "spring"
|
||||
run: python3 /opt/scripts/copy-tests.py --repo-url "Olympic/NTO-2025-Backend-TeamTask-tests" --branch "main" --task-type "spring"
|
||||
|
||||
- name: Run tests
|
||||
run: mvn test
|
||||
|
||||
@@ -1,12 +1,17 @@
|
||||
package com.example.nto;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
/**
|
||||
* TODO: ДОРАБОТАТЬ в рамках задания
|
||||
* =================================
|
||||
* МОЖНО: Добавлять методы, аннотации, зависимости
|
||||
* НЕЛЬЗЯ: Изменять название класса и пакета
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class App {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(App.class, args);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.example.nto.Exception;
|
||||
|
||||
public class BookingConflictException extends RuntimeException {
|
||||
public BookingConflictException(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,30 @@
|
||||
package com.example.nto.Exception;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
@RestControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
@ExceptionHandler(EmployeeNotFoundException.class)
|
||||
public ResponseEntity<String> EmployeeNotFoundException(EmployeeNotFoundException ex) {
|
||||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(ex.getMessage());
|
||||
}
|
||||
|
||||
@ExceptionHandler(PlaceNotFoundException.class)
|
||||
public ResponseEntity<String> PlaceNotFoundException(PlaceNotFoundException ex) {
|
||||
return ResponseEntity.badRequest().body("Что-то пошло не так");
|
||||
}
|
||||
|
||||
@ExceptionHandler(BookingConflictException.class)
|
||||
public ResponseEntity<String> BookingConflictException(BookingConflictException ex) {
|
||||
return ResponseEntity.status(HttpStatus.CONFLICT).body(ex.getMessage());
|
||||
}
|
||||
|
||||
@ExceptionHandler(Exception.class)
|
||||
public ResponseEntity<String> Exception(Exception ex) {
|
||||
return ResponseEntity.badRequest().body("Что-то пошло не так");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.example.nto.Exception;
|
||||
|
||||
public class PlaceNotFoundException extends RuntimeException {
|
||||
public PlaceNotFoundException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,59 @@
|
||||
package com.example.nto.controller;
|
||||
|
||||
import com.example.nto.entity.Place;
|
||||
import com.example.nto.service.BookingService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
/**
|
||||
* TODO: ДОРАБОТАТЬ в рамках задания
|
||||
* =================================
|
||||
* МОЖНО: Добавлять методы, аннотации, зависимости
|
||||
* НЕЛЬЗЯ: Изменять название класса и пакета
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/api/{code}")
|
||||
@RequiredArgsConstructor
|
||||
public class BookingController {
|
||||
private final BookingService bookingService;
|
||||
|
||||
@GetMapping("/booking")
|
||||
public ResponseEntity<?> getAvailableBooking(@PathVariable String code) {
|
||||
Map<LocalDate, List<Place>> places = bookingService.getAvailablePlaces(code);
|
||||
|
||||
Map<String, List<Map<String, Object>>> result = new TreeMap<>();
|
||||
for (Map.Entry<LocalDate, List<Place>> e : places.entrySet()) {
|
||||
List<Map<String, Object>> placeList = e.getValue().stream().map(p -> {
|
||||
Map<String, Object> m = new HashMap<>();
|
||||
m.put("id", p.getId());
|
||||
m.put("place", p.getPlaceName());
|
||||
return m;
|
||||
}).toList();
|
||||
result.put(e.getKey().toString(), placeList);
|
||||
}
|
||||
return ResponseEntity.ok(result);
|
||||
}
|
||||
|
||||
@PostMapping("/book")
|
||||
public ResponseEntity<?> createBooking(@PathVariable String code, @RequestBody Map<String, Object> request) {
|
||||
// try {
|
||||
String dateStr = (String) request.get("date");
|
||||
Long placeID = Long.valueOf(String.valueOf(request.get("placeID")));
|
||||
LocalDate date = LocalDate.parse(dateStr);
|
||||
|
||||
bookingService.createBooking(code, date, placeID);
|
||||
return ResponseEntity.status(201).build();
|
||||
// }
|
||||
// catch (Exception ex) {
|
||||
// return ResponseEntity.badRequest().body("Что-то пошло не так");
|
||||
// }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,10 +1,60 @@
|
||||
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.service.BookingService;
|
||||
import com.example.nto.service.EmployeeService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
/**
|
||||
* TODO: ДОРАБОТАТЬ в рамках задания
|
||||
* =================================
|
||||
* МОЖНО: Добавлять методы, аннотации, зависимости
|
||||
* НЕЛЬЗЯ: Изменять название класса и пакета
|
||||
*/
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/{code}")
|
||||
@RequiredArgsConstructor
|
||||
public class EmployeeController {
|
||||
private final EmployeeService employeeService;
|
||||
|
||||
@GetMapping("/auth")
|
||||
public ResponseEntity<Void> auth(@PathVariable String code) {
|
||||
employeeService.findByCode(code);
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
|
||||
@GetMapping("/info")
|
||||
public ResponseEntity<?> getInfo(@PathVariable String code) {
|
||||
Employee employee = employeeService.findByCode(code);
|
||||
|
||||
// Формирование booking map (дата -> booking details)
|
||||
Map<String, Object> response = new HashMap<>();
|
||||
response.put("name", employee.getName());
|
||||
response.put("photoUrl", employee.getPhotoUrl());
|
||||
|
||||
Map<String, Map<String, Object>> bookingMap = new HashMap<>();
|
||||
if (employee.getBookingList() != null) {
|
||||
for (Booking bk : employee.getBookingList()) {
|
||||
Map<String, Object> bookingDetails = new HashMap<>();
|
||||
bookingDetails.put("id", bk.getId());
|
||||
bookingDetails.put("place", bk.getPlaceId()); // можно заменить на placeName, если нужно join
|
||||
bookingMap.put(bk.getDate().toString(), bookingDetails);
|
||||
}
|
||||
}
|
||||
response.put("booking", bookingMap);
|
||||
|
||||
return ResponseEntity.ok(response);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package com.example.nto.entity;
|
||||
|
||||
import jakarta.persistence.FetchType;
|
||||
import jakarta.persistence.JoinColumn;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
import jakarta.persistence.*;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
@@ -21,15 +19,20 @@ import java.time.LocalDate;
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Entity
|
||||
@Table(name = "booking")
|
||||
public class Booking {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
private long id;
|
||||
|
||||
@Column(name = "date")
|
||||
private LocalDate date;
|
||||
|
||||
@ManyToOne(targetEntity = Place.class, fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "place_id")
|
||||
private Place place;
|
||||
@Column(name = "place_id")
|
||||
private Long placeId;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "employee_id")
|
||||
private Employee employee;
|
||||
}
|
||||
}
|
||||
@@ -19,14 +19,21 @@ import java.util.List;
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Entity
|
||||
@Table(name = "employee")
|
||||
public class Employee {
|
||||
|
||||
private long id;
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column(name = "name")
|
||||
private String name;
|
||||
|
||||
@Column(name = "code")
|
||||
private String code;
|
||||
|
||||
@Column(name = "photo_url")
|
||||
private String photoUrl;
|
||||
|
||||
@OneToMany(mappedBy = "employee", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
package com.example.nto.entity;
|
||||
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.*;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
@@ -19,11 +17,13 @@ import lombok.NoArgsConstructor;
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Entity
|
||||
@Table(name = "place")
|
||||
public class Place {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private long id;
|
||||
private Long id;
|
||||
|
||||
private String place;
|
||||
}
|
||||
@Column(name = "place_name")
|
||||
private String placeName;
|
||||
}
|
||||
@@ -1,10 +1,24 @@
|
||||
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: ДОРАБОТАТЬ в рамках задания
|
||||
* =================================
|
||||
* МОЖНО: Добавлять методы, аннотации, зависимости
|
||||
* НЕЛЬЗЯ: Изменять название класса и пакета
|
||||
*/
|
||||
public interface BookingRepository {
|
||||
}
|
||||
@Repository
|
||||
|
||||
public interface BookingRepository extends JpaRepository<Booking, Long> {
|
||||
List<Booking> findByDateBetween(LocalDate startDate, LocalDate endDate);
|
||||
|
||||
List<Booking> findByDate(LocalDate date);
|
||||
|
||||
boolean existsByDateAndPlaceId(LocalDate date, Long placeId);
|
||||
}
|
||||
@@ -1,10 +1,21 @@
|
||||
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: ДОРАБОТАТЬ в рамках задания
|
||||
* =================================
|
||||
* МОЖНО: Добавлять методы, аннотации, зависимости
|
||||
* НЕЛЬЗЯ: Изменять название класса и пакета
|
||||
*/
|
||||
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;
|
||||
|
||||
import com.example.nto.entity.Place;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
/**
|
||||
* TODO: ДОРАБОТАТЬ в рамках задания
|
||||
* =================================
|
||||
* МОЖНО: Добавлять методы, аннотации, зависимости
|
||||
* НЕЛЬЗЯ: Изменять название класса и пакета
|
||||
*/
|
||||
public interface PlaceRepository {
|
||||
}
|
||||
@Repository
|
||||
public interface PlaceRepository extends JpaRepository<Place, Long> {
|
||||
}
|
||||
@@ -1,5 +1,12 @@
|
||||
package com.example.nto.service;
|
||||
|
||||
import com.example.nto.entity.Booking;
|
||||
import com.example.nto.entity.Place;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* TODO: ДОРАБОТАТЬ в рамках задания
|
||||
* =================================
|
||||
@@ -7,4 +14,6 @@ package com.example.nto.service;
|
||||
* НЕЛЬЗЯ: Изменять название класса и пакета
|
||||
*/
|
||||
public interface BookingService {
|
||||
Map<LocalDate, List<Place>> getAvailablePlaces(String code);
|
||||
Booking createBooking(String code, LocalDate date, Long placeId);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
package com.example.nto.service;
|
||||
|
||||
import com.example.nto.entity.Employee;
|
||||
import com.example.nto.repository.EmployeeRepository;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* TODO: ДОРАБОТАТЬ в рамках задания
|
||||
* =================================
|
||||
@@ -7,4 +12,5 @@ package com.example.nto.service;
|
||||
* НЕЛЬЗЯ: Изменять название класса и пакета
|
||||
*/
|
||||
public interface EmployeeService {
|
||||
}
|
||||
public Employee findByCode(String code);
|
||||
}
|
||||
@@ -1,6 +1,26 @@
|
||||
package com.example.nto.service.impl;
|
||||
|
||||
import com.example.nto.Exception.BookingConflictException;
|
||||
import com.example.nto.Exception.EmployeeNotFoundException;
|
||||
import com.example.nto.Exception.PlaceNotFoundException;
|
||||
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.EmployeeService;
|
||||
import jakarta.transaction.Transactional;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* TODO: ДОРАБОТАТЬ в рамках задания
|
||||
@@ -8,5 +28,63 @@ import com.example.nto.service.BookingService;
|
||||
* МОЖНО: Добавлять методы, аннотации, зависимости
|
||||
* НЕЛЬЗЯ: Изменять название класса и пакета
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
@Service
|
||||
public class BookingServiceImpl implements BookingService {
|
||||
private final BookingRepository bookingRepository;
|
||||
private final PlaceRepository placeRepository;
|
||||
private final EmployeeService employeeService;
|
||||
|
||||
// Максимальное количество дней вперед для брони из конфигурации (например 3)
|
||||
private static final int DAYS_AHEAD = 3;
|
||||
private final EmployeeRepository employeeRepository;
|
||||
|
||||
public Map<LocalDate, List<Place>> getAvailablePlaces(String code) {
|
||||
Employee employee = employeeService.findByCode(code);
|
||||
LocalDate today = LocalDate.now();
|
||||
LocalDate endDate = today.plusDays(DAYS_AHEAD);
|
||||
|
||||
List<Booking> allBookings = bookingRepository.findByDateBetween(today, endDate);
|
||||
|
||||
List<Place> allPlaces = placeRepository.findAll();
|
||||
Map<LocalDate, List<Place>> availablePlacesByDate = new HashMap<>();
|
||||
for (LocalDate date = today; !date.isAfter(endDate); date = date.plusDays(1)) {
|
||||
LocalDate currentDate = date;
|
||||
// Найти занятые места в этот день
|
||||
Set<Long> bookedPlaceIds = allBookings.stream()
|
||||
.filter(b -> b.getDate().equals(currentDate))
|
||||
.map(Booking::getPlaceId)
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
// Оставшиеся места свободны
|
||||
List<Place> freePlaces = allPlaces.stream()
|
||||
.filter(p -> !bookedPlaceIds.contains(p.getId()))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
availablePlacesByDate.put(currentDate, freePlaces);
|
||||
}
|
||||
|
||||
return availablePlacesByDate;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Booking createBooking(String code, LocalDate date, Long placeId) {
|
||||
|
||||
if (employeeRepository.findByCode(code).isEmpty()) throw new EmployeeNotFoundException("Кода не существует");
|
||||
if (!placeRepository.existsById(placeId)) {
|
||||
throw new PlaceNotFoundException("Место не существует");
|
||||
}
|
||||
|
||||
if (bookingRepository.existsByDateAndPlaceId(date, placeId)) {
|
||||
throw new BookingConflictException("Уже забронировано");
|
||||
}
|
||||
|
||||
Employee employee = employeeService.findByCode(code);
|
||||
Booking booking = new Booking();
|
||||
booking.setDate(date);
|
||||
booking.setPlaceId(placeId);
|
||||
booking.setEmployee(employee);
|
||||
|
||||
return bookingRepository.save(booking);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
package com.example.nto.service.impl;
|
||||
|
||||
import com.example.nto.Exception.EmployeeNotFoundException;
|
||||
import com.example.nto.entity.Employee;
|
||||
import com.example.nto.repository.EmployeeRepository;
|
||||
import com.example.nto.service.EmployeeService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* TODO: ДОРАБОТАТЬ в рамках задания
|
||||
@@ -8,5 +13,13 @@ import com.example.nto.service.EmployeeService;
|
||||
* МОЖНО: Добавлять методы, аннотации, зависимости
|
||||
* НЕЛЬЗЯ: Изменять название класса и пакета
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class EmployeeServiceImpl implements EmployeeService {
|
||||
private final EmployeeRepository employeeRepository;
|
||||
|
||||
public Employee findByCode(String code) {
|
||||
return employeeRepository.findByCode(code)
|
||||
.orElseThrow(() -> new EmployeeNotFoundException("Кода не существует"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user