Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0000cb6da1 | |||
| af8267a22a | |||
| 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,38 @@
|
||||
package com.example.nto.controller;
|
||||
|
||||
import com.example.nto.dto.BookRequestDto;
|
||||
import com.example.nto.dto.BookingRecordDto;
|
||||
import com.example.nto.dto.PlaceRecordDto;
|
||||
import com.example.nto.service.BookingService;
|
||||
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: ДОРАБОТАТЬ в рамках задания
|
||||
* =================================
|
||||
* МОЖНО: Добавлять методы, аннотации, зависимости
|
||||
* НЕЛЬЗЯ: Изменять название класса и пакета
|
||||
*/
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@RequestMapping("/api")
|
||||
public class BookingController {
|
||||
private final BookingService bookingService;
|
||||
|
||||
@GetMapping("/{code}/booking")
|
||||
Map<LocalDate, List<PlaceRecordDto>> booking(@PathVariable("code") String code) {
|
||||
return bookingService.booking(code);
|
||||
}
|
||||
|
||||
@PostMapping("/{code}/book")
|
||||
ResponseEntity<Void> book(@PathVariable("code") String code, @RequestBody BookRequestDto bookRequest) {
|
||||
bookingService.book(code, bookRequest);
|
||||
return new ResponseEntity<>(null, HttpStatus.CREATED);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,33 @@
|
||||
package com.example.nto.controller;
|
||||
|
||||
import com.example.nto.dto.EmployeeInfoWithBookingDto;
|
||||
import com.example.nto.service.EmployeeService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
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: ДОРАБОТАТЬ в рамках задания
|
||||
* =================================
|
||||
* МОЖНО: Добавлять методы, аннотации, зависимости
|
||||
* НЕЛЬЗЯ: Изменять название класса и пакета
|
||||
*/
|
||||
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@RequestMapping("/api")
|
||||
public class EmployeeController {
|
||||
private final EmployeeService employeeService;
|
||||
|
||||
@GetMapping("/{code}/auth")
|
||||
void auth(@PathVariable("code") String code) {
|
||||
employeeService.auth(code);
|
||||
}
|
||||
|
||||
@GetMapping("/{code}/info")
|
||||
EmployeeInfoWithBookingDto info(@PathVariable("code") String code) {
|
||||
return employeeService.info(code);
|
||||
}
|
||||
}
|
||||
|
||||
17
src/main/java/com/example/nto/dto/BookRequestDto.java
Normal file
17
src/main/java/com/example/nto/dto/BookRequestDto.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package com.example.nto.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class BookRequestDto {
|
||||
private LocalDate date;
|
||||
private long placeID;
|
||||
}
|
||||
13
src/main/java/com/example/nto/dto/BookedPlacesDto.java
Normal file
13
src/main/java/com/example/nto/dto/BookedPlacesDto.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package com.example.nto.dto;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
public class BookedPlacesDto {
|
||||
private LocalDate date;
|
||||
private String place;
|
||||
}
|
||||
14
src/main/java/com/example/nto/dto/BookingRecordDto.java
Normal file
14
src/main/java/com/example/nto/dto/BookingRecordDto.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package com.example.nto.dto;
|
||||
|
||||
import com.example.nto.entity.Place;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
public class BookingRecordDto {
|
||||
private long id;
|
||||
private String place;
|
||||
}
|
||||
18
src/main/java/com/example/nto/dto/BookingWithDateDto.java
Normal file
18
src/main/java/com/example/nto/dto/BookingWithDateDto.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package com.example.nto.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class BookingWithDateDto {
|
||||
private LocalDate date;
|
||||
private long id;
|
||||
private String place;
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.example.nto.dto;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.Map;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
public class EmployeeInfoWithBookingDto {
|
||||
private String name;
|
||||
private String photoUrl;
|
||||
private Map<LocalDate, BookingRecordDto> booking;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.example.nto.dto;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
public class EmployeeNameAndPhotoDto {
|
||||
private String name;
|
||||
private String photoUrl;
|
||||
}
|
||||
11
src/main/java/com/example/nto/dto/PlaceRecordDto.java
Normal file
11
src/main/java/com/example/nto/dto/PlaceRecordDto.java
Normal file
@@ -0,0 +1,11 @@
|
||||
package com.example.nto.dto;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
public class PlaceRecordDto {
|
||||
private long id;
|
||||
private String place;
|
||||
}
|
||||
19
src/main/java/com/example/nto/dto/PlaceWithDateDto.java
Normal file
19
src/main/java/com/example/nto/dto/PlaceWithDateDto.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package com.example.nto.dto;
|
||||
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
@Data
|
||||
public class PlaceWithDateDto {
|
||||
private LocalDate date;
|
||||
private long id;
|
||||
private String place;
|
||||
|
||||
public PlaceWithDateDto(java.sql.Date date, long id, String place) {
|
||||
this.date = date.toLocalDate();
|
||||
this.id = id;
|
||||
this.place = place;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.example.nto.dto.mapper;
|
||||
|
||||
import com.example.nto.dto.BookingRecordDto;
|
||||
import com.example.nto.dto.BookingWithDateDto;
|
||||
import lombok.experimental.UtilityClass;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
@UtilityClass
|
||||
public class ListBookingWithDateDtoMapper {
|
||||
public static Map<LocalDate, BookingRecordDto> toMapLocalDateBookingRecordDto(List<BookingWithDateDto> d) {
|
||||
Map<LocalDate, BookingRecordDto> res = new TreeMap<>();
|
||||
d.forEach(bookingWithDate -> {
|
||||
res.put(bookingWithDate.getDate(), BookingRecordDto.builder()
|
||||
.id(bookingWithDate.getId())
|
||||
.place(bookingWithDate.getPlace())
|
||||
.build());
|
||||
});
|
||||
return res;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.example.nto.dto.mapper;
|
||||
|
||||
import com.example.nto.dto.PlaceRecordDto;
|
||||
import com.example.nto.dto.PlaceWithDateDto;
|
||||
import lombok.experimental.UtilityClass;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@UtilityClass
|
||||
public class ListPlaceWithDateDtoMapper {
|
||||
public static Map<LocalDate, List<PlaceRecordDto>> toMapLocalDateListPlaceRecordDto(List<PlaceWithDateDto> d) {
|
||||
if (d == null) {
|
||||
return new HashMap<>();
|
||||
}
|
||||
|
||||
return d.stream()
|
||||
.collect(Collectors.groupingBy(
|
||||
PlaceWithDateDto::getDate,
|
||||
Collectors.mapping(
|
||||
placeWithDate -> PlaceRecordDto.builder()
|
||||
.id(placeWithDate.getId())
|
||||
.place(placeWithDate.getPlace())
|
||||
.build(),
|
||||
Collectors.toList()
|
||||
)
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -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,25 @@ import java.time.LocalDate;
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Entity
|
||||
@Table(name = "booking")
|
||||
public class Booking {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@Column(name = "id")
|
||||
private long id;
|
||||
|
||||
@Column(name = "date")
|
||||
private LocalDate date;
|
||||
|
||||
@ManyToOne(targetEntity = Place.class, fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "place_id")
|
||||
private Place place;
|
||||
|
||||
@ManyToOne(targetEntity = Employee.class, fetch = FetchType.LAZY)
|
||||
@JoinColumn(
|
||||
name = "employee_id",
|
||||
referencedColumnName = "id"
|
||||
)
|
||||
private Employee employee;
|
||||
}
|
||||
|
||||
@@ -19,14 +19,21 @@ import java.util.List;
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Entity
|
||||
@Table(name = "employee")
|
||||
public class Employee {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@Column(name = "id")
|
||||
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,14 @@ import lombok.NoArgsConstructor;
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Entity
|
||||
@Table(name = "place")
|
||||
public class Place {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
@Column(name = "id")
|
||||
private long id;
|
||||
|
||||
@Column(name = "place_name")
|
||||
private String place;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.example.nto.exception;
|
||||
|
||||
public class AlreadyBookingForThisDateException extends RuntimeException {
|
||||
public AlreadyBookingForThisDateException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.example.nto.exception;
|
||||
|
||||
public class EmployeeNotFoundByCodeException extends RuntimeException {
|
||||
public EmployeeNotFoundByCodeException(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.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
|
||||
@ControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
@ExceptionHandler(EmployeeNotFoundByCodeException.class)
|
||||
ResponseEntity<String> employeeNotFoundByCodeExceptionHandler(EmployeeNotFoundByCodeException e) {
|
||||
return new ResponseEntity<>(e.getMessage(), HttpStatus.UNAUTHORIZED);
|
||||
}
|
||||
|
||||
@ExceptionHandler(PlaceAlreadyBookedException.class)
|
||||
ResponseEntity<String> placeAlreadyBookedExceptionHandler(PlaceAlreadyBookedException e) {
|
||||
return new ResponseEntity<>(e.getMessage(), HttpStatus.CONFLICT);
|
||||
}
|
||||
|
||||
@ExceptionHandler(PlaceNotFoundException.class)
|
||||
ResponseEntity<String> placeNotFoundExceptionHandler(PlaceNotFoundException e) {
|
||||
return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
|
||||
@ExceptionHandler(AlreadyBookingForThisDateException.class)
|
||||
ResponseEntity<String> alreadyBookingForThisDateExceptionHandler(AlreadyBookingForThisDateException e) {
|
||||
return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.example.nto.exception;
|
||||
|
||||
public class PlaceAlreadyBookedException extends RuntimeException {
|
||||
public PlaceAlreadyBookedException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.example.nto.exception;
|
||||
|
||||
public class PlaceNotFoundException extends RuntimeException {
|
||||
public PlaceNotFoundException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,31 @@
|
||||
package com.example.nto.repository;
|
||||
|
||||
import com.example.nto.dto.BookingWithDateDto;
|
||||
import com.example.nto.entity.Booking;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
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> {
|
||||
@Query("select new com.example.nto.dto.BookingWithDateDto(b.date, b.place.id, b.place.place) from Booking b where b.employee.code = :code order by b.date")
|
||||
List<BookingWithDateDto> getBookingsByCode(@Param("code") String code);
|
||||
|
||||
@Query("select count(b) > 0 from Booking b where b.date = :date and b.employee.code = :code")
|
||||
boolean hasBookingForDate(@Param("code") String code, @Param("date") LocalDate date);
|
||||
|
||||
@Modifying
|
||||
@Query(value = "insert into booking (date, place_id, employee_id) select :date, :placeId, e.id from employee e where e.code = :code", nativeQuery = true)
|
||||
void bookPlace(@Param("code") String code, @Param("date") LocalDate date, @Param("placeId") long placeId);
|
||||
}
|
||||
|
||||
@@ -1,10 +1,23 @@
|
||||
package com.example.nto.repository;
|
||||
|
||||
import com.example.nto.dto.EmployeeNameAndPhotoDto;
|
||||
import com.example.nto.entity.Employee;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
/**
|
||||
* TODO: ДОРАБОТАТЬ в рамках задания
|
||||
* =================================
|
||||
* МОЖНО: Добавлять методы, аннотации, зависимости
|
||||
* НЕЛЬЗЯ: Изменять название класса и пакета
|
||||
*/
|
||||
public interface EmployeeRepository {
|
||||
@Repository
|
||||
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
|
||||
@Query("select count(e) > 0 from Employee e where e.code = :code")
|
||||
boolean isExist(@Param("code") String code);
|
||||
|
||||
@Query("select e.name, e.photoUrl from Employee e where e.code = :code")
|
||||
EmployeeNameAndPhotoDto getNameAndPhoto(@Param("code") String code);
|
||||
}
|
||||
|
||||
@@ -1,10 +1,55 @@
|
||||
package com.example.nto.repository;
|
||||
|
||||
import com.example.nto.dto.BookedPlacesDto;
|
||||
import com.example.nto.dto.PlaceRecordDto;
|
||||
import com.example.nto.dto.PlaceWithDateDto;
|
||||
import com.example.nto.entity.Place;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* TODO: ДОРАБОТАТЬ в рамках задания
|
||||
* =================================
|
||||
* МОЖНО: Добавлять методы, аннотации, зависимости
|
||||
* НЕЛЬЗЯ: Изменять название класса и пакета
|
||||
*/
|
||||
public interface PlaceRepository {
|
||||
@Repository
|
||||
public interface PlaceRepository extends JpaRepository<Place, Long> {
|
||||
/*with cte as (
|
||||
select (now() + s*('1day'::interval)) ::date d
|
||||
from generate_series(0,3) s
|
||||
)
|
||||
select dates.d as date, p.id as id, p.place_name as place
|
||||
from cte dates
|
||||
cross join place p
|
||||
left join booking b on
|
||||
p.id = b.place_id
|
||||
and b.date = dates.d
|
||||
where b.date is null
|
||||
order by 2, 1*/
|
||||
|
||||
@Query(value = "WITH cte AS (" +
|
||||
" SELECT DATEADD('DAY', s, CAST(:fromDate AS DATE)) AS d" +
|
||||
" FROM SYSTEM_RANGE(0, 3) t(s)" +
|
||||
")" +
|
||||
" SELECT dates.d AS date, p.id AS id, p.place_name AS place" +
|
||||
" FROM cte dates" +
|
||||
" CROSS JOIN place p" +
|
||||
" LEFT JOIN booking b ON p.id = b.place_id AND b.date = dates.d" +
|
||||
" WHERE b.date IS NULL" +
|
||||
" ORDER BY dates.d, p.id",
|
||||
nativeQuery = true)
|
||||
List<PlaceWithDateDto> getFreePlaces(@Param("fromDate") LocalDate fromDate);
|
||||
|
||||
@Query("select count(b) = 0 from Booking b where b.place.id = :placeId and b.date = :date")
|
||||
boolean isFree(@Param("date") LocalDate date, @Param("placeId") long placeId);
|
||||
|
||||
@Query("select count(p) > 0 from Place p where p.id = :placeId")
|
||||
boolean isExist(@Param("placeId") long placeId);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
package com.example.nto.service;
|
||||
|
||||
import com.example.nto.dto.BookRequestDto;
|
||||
import com.example.nto.dto.PlaceRecordDto;
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* TODO: ДОРАБОТАТЬ в рамках задания
|
||||
* =================================
|
||||
@@ -7,4 +13,6 @@ package com.example.nto.service;
|
||||
* НЕЛЬЗЯ: Изменять название класса и пакета
|
||||
*/
|
||||
public interface BookingService {
|
||||
Map<LocalDate, List<PlaceRecordDto>> booking(String code);
|
||||
void book(String code, BookRequestDto bookRequest);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.example.nto.service;
|
||||
|
||||
import com.example.nto.dto.EmployeeInfoWithBookingDto;
|
||||
|
||||
/**
|
||||
* TODO: ДОРАБОТАТЬ в рамках задания
|
||||
* =================================
|
||||
@@ -7,4 +9,6 @@ package com.example.nto.service;
|
||||
* НЕЛЬЗЯ: Изменять название класса и пакета
|
||||
*/
|
||||
public interface EmployeeService {
|
||||
void auth(String code);
|
||||
EmployeeInfoWithBookingDto info(String code);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,27 @@
|
||||
package com.example.nto.service.impl;
|
||||
|
||||
import com.example.nto.dto.BookRequestDto;
|
||||
import com.example.nto.dto.PlaceRecordDto;
|
||||
import com.example.nto.dto.PlaceWithDateDto;
|
||||
import com.example.nto.exception.AlreadyBookingForThisDateException;
|
||||
import com.example.nto.exception.EmployeeNotFoundByCodeException;
|
||||
import com.example.nto.exception.PlaceAlreadyBookedException;
|
||||
import com.example.nto.exception.PlaceNotFoundException;
|
||||
import com.example.nto.repository.BookingRepository;
|
||||
import com.example.nto.repository.EmployeeRepository;
|
||||
import com.example.nto.repository.PlaceRepository;
|
||||
import com.example.nto.service.BookingService;
|
||||
import jakarta.transaction.Transactional;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import static com.example.nto.dto.mapper.ListPlaceWithDateDtoMapper.toMapLocalDateListPlaceRecordDto;
|
||||
|
||||
/**
|
||||
* TODO: ДОРАБОТАТЬ в рамках задания
|
||||
@@ -8,5 +29,56 @@ import com.example.nto.service.BookingService;
|
||||
* МОЖНО: Добавлять методы, аннотации, зависимости
|
||||
* НЕЛЬЗЯ: Изменять название класса и пакета
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class BookingServiceImpl implements BookingService {
|
||||
private final EmployeeRepository employeeRepository;
|
||||
private final PlaceRepository placeRepository;
|
||||
private final BookingRepository bookingRepository;
|
||||
|
||||
@Override
|
||||
public Map<LocalDate, List<PlaceRecordDto>> booking(String code) {
|
||||
if (!employeeRepository.isExist(code)) {
|
||||
throw new EmployeeNotFoundByCodeException("employee with code " + code + " not found");
|
||||
}
|
||||
|
||||
LocalDate currentDate = LocalDate.now();
|
||||
|
||||
List<PlaceWithDateDto> freePlaces = placeRepository.getFreePlaces(currentDate);
|
||||
|
||||
Map<LocalDate, List<PlaceRecordDto>> result = new TreeMap<>();
|
||||
|
||||
for (int i = 0; i <= 3; i++) {
|
||||
result.put(currentDate.plusDays(i), new ArrayList<>());
|
||||
}
|
||||
for (PlaceWithDateDto freePlace : freePlaces) {
|
||||
result.get(freePlace.getDate()).add(PlaceRecordDto.builder()
|
||||
.id(freePlace.getId())
|
||||
.place(freePlace.getPlace())
|
||||
.build());
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void book(String code, BookRequestDto bookRequest) {
|
||||
if (!employeeRepository.isExist(code)) {
|
||||
throw new EmployeeNotFoundByCodeException("employee with code " + code + " not found");
|
||||
}
|
||||
|
||||
if (!placeRepository.isExist(bookRequest.getPlaceID())) {
|
||||
throw new PlaceNotFoundException("place with id " + bookRequest.getPlaceID() + " is not found");
|
||||
}
|
||||
if (bookingRepository.hasBookingForDate(code, bookRequest.getDate())) {
|
||||
throw new AlreadyBookingForThisDateException("employee with code " + code + " already book place on date " + bookRequest.getDate().toString());
|
||||
}
|
||||
if (!placeRepository.isFree(bookRequest.getDate(), bookRequest.getPlaceID())) {
|
||||
throw new PlaceAlreadyBookedException("place with id " + bookRequest.getPlaceID() + " is already booked, please choose another one");
|
||||
}
|
||||
|
||||
bookingRepository.bookPlace(code, bookRequest.getDate(), bookRequest.getPlaceID());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,15 @@
|
||||
package com.example.nto.service.impl;
|
||||
|
||||
import com.example.nto.dto.EmployeeInfoWithBookingDto;
|
||||
import com.example.nto.dto.EmployeeNameAndPhotoDto;
|
||||
import com.example.nto.exception.EmployeeNotFoundByCodeException;
|
||||
import com.example.nto.repository.BookingRepository;
|
||||
import com.example.nto.repository.EmployeeRepository;
|
||||
import com.example.nto.service.EmployeeService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import static com.example.nto.dto.mapper.ListBookingWithDateDtoMapper.toMapLocalDateBookingRecordDto;
|
||||
|
||||
/**
|
||||
* TODO: ДОРАБОТАТЬ в рамках задания
|
||||
@@ -8,5 +17,31 @@ import com.example.nto.service.EmployeeService;
|
||||
* МОЖНО: Добавлять методы, аннотации, зависимости
|
||||
* НЕЛЬЗЯ: Изменять название класса и пакета
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class EmployeeServiceImpl implements EmployeeService {
|
||||
private final EmployeeRepository employeeRepository;
|
||||
private final BookingRepository bookingRepository;
|
||||
|
||||
@Override
|
||||
public void auth(String code) {
|
||||
if (!employeeRepository.isExist(code)) {
|
||||
throw new EmployeeNotFoundByCodeException("employee with code " + code + " not found");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public EmployeeInfoWithBookingDto info(String code) {
|
||||
if (!employeeRepository.isExist(code)) {
|
||||
throw new EmployeeNotFoundByCodeException("employee with code " + code + " not found");
|
||||
}
|
||||
|
||||
EmployeeNameAndPhotoDto nameAndPhoto = employeeRepository.getNameAndPhoto(code);
|
||||
|
||||
return EmployeeInfoWithBookingDto.builder()
|
||||
.name(nameAndPhoto.getName())
|
||||
.photoUrl(nameAndPhoto.getPhotoUrl())
|
||||
.booking(toMapLocalDateBookingRecordDto(bookingRepository.getBookingsByCode(code)))
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user