From 7e286a3a7b9f5a509aa27949497cfd4077b47cb9 Mon Sep 17 00:00:00 2001 From: Maksim Date: Thu, 4 Dec 2025 17:20:04 +0600 Subject: [PATCH] complet --- src/main/java/com/example/nto/App.java | 5 ++ .../java/com/example/nto/DTO/BookingDTO.java | 13 +++ .../com/example/nto/DTO/BookingRequest.java | 13 +++ .../java/com/example/nto/DTO/EmployeeDTO.java | 15 ++++ .../java/com/example/nto/DTO/PlaceDTO.java | 11 +++ .../nto/controller/BookingController.java | 54 ++++++++++++ .../nto/controller/EmployeeController.java | 47 ++++++++++ .../java/com/example/nto/entity/Booking.java | 13 +-- .../java/com/example/nto/entity/Employee.java | 11 ++- .../java/com/example/nto/entity/Place.java | 7 +- .../BookingAlreadyExistsException.java | 7 ++ .../exceptions/EmployeeNotFoundException.java | 7 ++ .../exceptions/PlaceNotFoundException.java | 7 ++ .../handlers/GlobalExceptionHandler.java | 26 ++++++ .../nto/repository/BookingRepository.java | 14 ++- .../nto/repository/EmployeeRepository.java | 13 ++- .../nto/repository/PlaceRepository.java | 12 ++- .../example/nto/service/BookingService.java | 14 +++ .../example/nto/service/EmployeeService.java | 7 ++ .../nto/service/impl/BookingServiceImpl.java | 88 +++++++++++++++++++ .../nto/service/impl/EmployeeServiceImpl.java | 22 +++++ .../com/example/nto/utils/MapperEnToDTO.java | 40 +++++++++ 22 files changed, 431 insertions(+), 15 deletions(-) create mode 100644 src/main/java/com/example/nto/DTO/BookingDTO.java create mode 100644 src/main/java/com/example/nto/DTO/BookingRequest.java create mode 100644 src/main/java/com/example/nto/DTO/EmployeeDTO.java create mode 100644 src/main/java/com/example/nto/DTO/PlaceDTO.java create mode 100644 src/main/java/com/example/nto/exceptions/BookingAlreadyExistsException.java create mode 100644 src/main/java/com/example/nto/exceptions/EmployeeNotFoundException.java create mode 100644 src/main/java/com/example/nto/exceptions/PlaceNotFoundException.java create mode 100644 src/main/java/com/example/nto/exceptions/handlers/GlobalExceptionHandler.java create mode 100644 src/main/java/com/example/nto/utils/MapperEnToDTO.java diff --git a/src/main/java/com/example/nto/App.java b/src/main/java/com/example/nto/App.java index e453f89..e5435c1 100644 --- a/src/main/java/com/example/nto/App.java +++ b/src/main/java/com/example/nto/App.java @@ -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); } } diff --git a/src/main/java/com/example/nto/DTO/BookingDTO.java b/src/main/java/com/example/nto/DTO/BookingDTO.java new file mode 100644 index 0000000..90a346d --- /dev/null +++ b/src/main/java/com/example/nto/DTO/BookingDTO.java @@ -0,0 +1,13 @@ +package com.example.nto.DTO; + +import lombok.Data; +import lombok.RequiredArgsConstructor; + +import java.time.LocalDate; + +@Data +@RequiredArgsConstructor +public class BookingDTO { + private Long id; + private PlaceDTO place; +} diff --git a/src/main/java/com/example/nto/DTO/BookingRequest.java b/src/main/java/com/example/nto/DTO/BookingRequest.java new file mode 100644 index 0000000..d82c2c3 --- /dev/null +++ b/src/main/java/com/example/nto/DTO/BookingRequest.java @@ -0,0 +1,13 @@ +package com.example.nto.DTO; + +import lombok.Data; +import lombok.RequiredArgsConstructor; + +import java.time.LocalDate; + +@Data +@RequiredArgsConstructor +public class BookingRequest { + private LocalDate date; + private Long placeId; +} diff --git a/src/main/java/com/example/nto/DTO/EmployeeDTO.java b/src/main/java/com/example/nto/DTO/EmployeeDTO.java new file mode 100644 index 0000000..e58068c --- /dev/null +++ b/src/main/java/com/example/nto/DTO/EmployeeDTO.java @@ -0,0 +1,15 @@ +package com.example.nto.DTO; + +import lombok.Data; +import lombok.RequiredArgsConstructor; + +import java.util.Map; +import java.util.TreeMap; + +@Data +@RequiredArgsConstructor +public class EmployeeDTO { + private String name; + private String photoUrl; + private Map bookings = new TreeMap<>(); +} diff --git a/src/main/java/com/example/nto/DTO/PlaceDTO.java b/src/main/java/com/example/nto/DTO/PlaceDTO.java new file mode 100644 index 0000000..d1d5e2e --- /dev/null +++ b/src/main/java/com/example/nto/DTO/PlaceDTO.java @@ -0,0 +1,11 @@ +package com.example.nto.DTO; + +import lombok.Data; +import lombok.RequiredArgsConstructor; + +@Data +@RequiredArgsConstructor +public class PlaceDTO { + private Long id; + private String name; +} diff --git a/src/main/java/com/example/nto/controller/BookingController.java b/src/main/java/com/example/nto/controller/BookingController.java index 9885f84..67fdccb 100644 --- a/src/main/java/com/example/nto/controller/BookingController.java +++ b/src/main/java/com/example/nto/controller/BookingController.java @@ -1,10 +1,64 @@ package com.example.nto.controller; +import com.example.nto.DTO.BookingDTO; +import com.example.nto.DTO.BookingRequest; +import com.example.nto.DTO.PlaceDTO; +import com.example.nto.entity.Booking; +import com.example.nto.entity.Place; +import com.example.nto.exceptions.BookingAlreadyExistsException; +import com.example.nto.exceptions.EmployeeNotFoundException; +import com.example.nto.service.BookingService; +import io.swagger.v3.oas.annotations.parameters.RequestBody; +import lombok.RequiredArgsConstructor; +import org.springframework.cglib.core.Local; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.time.LocalDate; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + /** * TODO: ДОРАБОТАТЬ в рамках задания * ================================= * МОЖНО: Добавлять методы, аннотации, зависимости * НЕЛЬЗЯ: Изменять название класса и пакета */ +@RestController +@RequestMapping("/api/{code}") +@RequiredArgsConstructor public class BookingController { + + private final BookingService bookingService; + + + @GetMapping("/booking") + public ResponseEntity>> getBooking(@PathVariable String code){ + try { + Map> availablePlaces = bookingService.freeBookingPlace(code); + return ResponseEntity.ok(availablePlaces); + } catch (EmployeeNotFoundException e) { + return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build(); + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.BAD_GATEWAY).build(); + } + } + + @PostMapping("/book") + public ResponseEntity createBooking(@PathVariable String code, @RequestBody BookingRequest request){ + try { + LocalDate date = request.getDate(); + Long placeId = request.getPlaceId(); + BookingDTO bookingDTO = bookingService.createBooking(code, date, placeId); + return ResponseEntity.status(HttpStatus.CREATED).build(); + } catch (EmployeeNotFoundException e) { + return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build(); + } catch (BookingAlreadyExistsException e) { + return ResponseEntity.status(HttpStatus.CONFLICT).build(); + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.BAD_GATEWAY).build(); + } + } } diff --git a/src/main/java/com/example/nto/controller/EmployeeController.java b/src/main/java/com/example/nto/controller/EmployeeController.java index 47658f9..1502b1d 100644 --- a/src/main/java/com/example/nto/controller/EmployeeController.java +++ b/src/main/java/com/example/nto/controller/EmployeeController.java @@ -1,10 +1,57 @@ package com.example.nto.controller; +import com.example.nto.DTO.EmployeeDTO; +import com.example.nto.entity.Booking; +import com.example.nto.entity.Employee; +import com.example.nto.exceptions.EmployeeNotFoundException; +import com.example.nto.service.EmployeeService; +import com.example.nto.utils.MapperEnToDTO; +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.*; + /** * TODO: ДОРАБОТАТЬ в рамках задания * ================================= * МОЖНО: Добавлять методы, аннотации, зависимости * НЕЛЬЗЯ: Изменять название класса и пакета */ +@RestController +@RequestMapping("/api/{code}") +@RequiredArgsConstructor public class EmployeeController { + private final EmployeeService employeeService; + + @GetMapping("/auth") + public ResponseEntity auth(@PathVariable String code){ + try { + Employee employee = employeeService.getEmployeeByCode(code); + return ResponseEntity.ok().build(); + } catch (EmployeeNotFoundException e) { + return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build(); + } catch (Exception e){ + return ResponseEntity.status(HttpStatus.BAD_REQUEST).build(); + } + } + + @GetMapping("/info") + public ResponseEntity info(@PathVariable String code) { + try { + Employee employee = employeeService.getEmployeeByCode(code); + + EmployeeDTO response = MapperEnToDTO.toEmployeeInfoDto(employee); + + return ResponseEntity.ok(response); + } catch (EmployeeNotFoundException e) { + return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build(); + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST).build(); + } + } } diff --git a/src/main/java/com/example/nto/entity/Booking.java b/src/main/java/com/example/nto/entity/Booking.java index 21c1981..bd391d5 100644 --- a/src/main/java/com/example/nto/entity/Booking.java +++ b/src/main/java/com/example/nto/entity/Booking.java @@ -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; @@ -17,19 +15,24 @@ import java.time.LocalDate; * МОЖНО: Добавлять методы, аннотации, зависимости * НЕЛЬЗЯ: Изменять название класса и пакета */ +@Entity @Data @Builder @NoArgsConstructor @AllArgsConstructor +@Table(name = "booking") public class Booking { - + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) private long id; - + @Column(name="date") private LocalDate date; @ManyToOne(targetEntity = Place.class, fetch = FetchType.LAZY) @JoinColumn(name = "place_id") private Place place; + @ManyToOne(fetch = FetchType.EAGER) + @JoinColumn(name = "employee_id") private Employee employee; } diff --git a/src/main/java/com/example/nto/entity/Employee.java b/src/main/java/com/example/nto/entity/Employee.java index a52102b..1899244 100644 --- a/src/main/java/com/example/nto/entity/Employee.java +++ b/src/main/java/com/example/nto/entity/Employee.java @@ -15,18 +15,21 @@ import java.util.List; * МОЖНО: Добавлять методы, аннотации, зависимости * НЕЛЬЗЯ: Изменять название класса и пакета */ +@Entity @Data @Builder @NoArgsConstructor @AllArgsConstructor +@Table(name="employee") public class Employee { - + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) private long id; - + @Column(name="name") private String name; - + @Column(name="code") private String code; - + @Column(name="photoUrl") private String photoUrl; @OneToMany(mappedBy = "employee", cascade = CascadeType.ALL, fetch = FetchType.LAZY) diff --git a/src/main/java/com/example/nto/entity/Place.java b/src/main/java/com/example/nto/entity/Place.java index 00c253b..1d5f319 100644 --- a/src/main/java/com/example/nto/entity/Place.java +++ b/src/main/java/com/example/nto/entity/Place.java @@ -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; @@ -15,15 +13,18 @@ import lombok.NoArgsConstructor; * МОЖНО: Добавлять методы, аннотации, зависимости * НЕЛЬЗЯ: Изменять название класса и пакета */ +@Entity @Data @Builder @NoArgsConstructor @AllArgsConstructor +@Table(name="place") public class Place { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private long id; + @Column(name="place_name") private String place; } diff --git a/src/main/java/com/example/nto/exceptions/BookingAlreadyExistsException.java b/src/main/java/com/example/nto/exceptions/BookingAlreadyExistsException.java new file mode 100644 index 0000000..7c18a4f --- /dev/null +++ b/src/main/java/com/example/nto/exceptions/BookingAlreadyExistsException.java @@ -0,0 +1,7 @@ +package com.example.nto.exceptions; + +public class BookingAlreadyExistsException extends RuntimeException { + public BookingAlreadyExistsException(String message) { + super(message); + } +} diff --git a/src/main/java/com/example/nto/exceptions/EmployeeNotFoundException.java b/src/main/java/com/example/nto/exceptions/EmployeeNotFoundException.java new file mode 100644 index 0000000..05cac48 --- /dev/null +++ b/src/main/java/com/example/nto/exceptions/EmployeeNotFoundException.java @@ -0,0 +1,7 @@ +package com.example.nto.exceptions; + +public class EmployeeNotFoundException extends RuntimeException { + public EmployeeNotFoundException(String message) { + super(message); + } +} diff --git a/src/main/java/com/example/nto/exceptions/PlaceNotFoundException.java b/src/main/java/com/example/nto/exceptions/PlaceNotFoundException.java new file mode 100644 index 0000000..aab9b4f --- /dev/null +++ b/src/main/java/com/example/nto/exceptions/PlaceNotFoundException.java @@ -0,0 +1,7 @@ +package com.example.nto.exceptions; + +public class PlaceNotFoundException extends RuntimeException { + public PlaceNotFoundException(String message) { + super(message); + } +} diff --git a/src/main/java/com/example/nto/exceptions/handlers/GlobalExceptionHandler.java b/src/main/java/com/example/nto/exceptions/handlers/GlobalExceptionHandler.java new file mode 100644 index 0000000..a4e2703 --- /dev/null +++ b/src/main/java/com/example/nto/exceptions/handlers/GlobalExceptionHandler.java @@ -0,0 +1,26 @@ +package com.example.nto.exceptions.handlers; + +import com.example.nto.exceptions.BookingAlreadyExistsException; +import com.example.nto.exceptions.EmployeeNotFoundException; +import com.example.nto.exceptions.PlaceNotFoundException; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ControllerAdvice +public class GlobalExceptionHandler { + @ExceptionHandler(EmployeeNotFoundException.class) + public ResponseEntity handlerEmployeeNotFoundException(EmployeeNotFoundException e) { + return new ResponseEntity<>(e.getMessage(), HttpStatus.UNAUTHORIZED); + } + @ExceptionHandler(PlaceNotFoundException.class) + public ResponseEntity handlerPlaceNotFoundException(PlaceNotFoundException e){ + return new ResponseEntity<>(e.getMessage(), HttpStatus.UNAUTHORIZED); + } + @ExceptionHandler(BookingAlreadyExistsException.class) + public ResponseEntity handlerBookingAlreadyExistsException(BookingAlreadyExistsException e){ + return new ResponseEntity<>(e.getMessage(), HttpStatus.CONFLICT); + } +} diff --git a/src/main/java/com/example/nto/repository/BookingRepository.java b/src/main/java/com/example/nto/repository/BookingRepository.java index 303bb54..c7dce44 100644 --- a/src/main/java/com/example/nto/repository/BookingRepository.java +++ b/src/main/java/com/example/nto/repository/BookingRepository.java @@ -1,10 +1,22 @@ 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; +import java.util.Optional; + /** * TODO: ДОРАБОТАТЬ в рамках задания * ================================= * МОЖНО: Добавлять методы, аннотации, зависимости * НЕЛЬЗЯ: Изменять название класса и пакета */ -public interface BookingRepository { +@Repository +public interface BookingRepository extends JpaRepository { + Optional findBookingByDate(LocalDate localDate); + List findBookingByDateBetween(LocalDate startDate, LocalDate endDate); + Optional findBookingByDateAndPlaceId(LocalDate date, Long placeId); } diff --git a/src/main/java/com/example/nto/repository/EmployeeRepository.java b/src/main/java/com/example/nto/repository/EmployeeRepository.java index 210d29c..cab66d9 100644 --- a/src/main/java/com/example/nto/repository/EmployeeRepository.java +++ b/src/main/java/com/example/nto/repository/EmployeeRepository.java @@ -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.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; +import org.springframework.stereotype.Repository; + +import java.util.Optional; + /** * TODO: ДОРАБОТАТЬ в рамках задания * ================================= * МОЖНО: Добавлять методы, аннотации, зависимости * НЕЛЬЗЯ: Изменять название класса и пакета */ -public interface EmployeeRepository { +@Repository +public interface EmployeeRepository extends JpaRepository { + Optional findEmployeeByCode(String code); } + diff --git a/src/main/java/com/example/nto/repository/PlaceRepository.java b/src/main/java/com/example/nto/repository/PlaceRepository.java index d3bea1d..5ad5ea0 100644 --- a/src/main/java/com/example/nto/repository/PlaceRepository.java +++ b/src/main/java/com/example/nto/repository/PlaceRepository.java @@ -1,10 +1,20 @@ package com.example.nto.repository; +import com.example.nto.entity.Place; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.List; +import java.util.Optional; + /** * TODO: ДОРАБОТАТЬ в рамках задания * ================================= * МОЖНО: Добавлять методы, аннотации, зависимости * НЕЛЬЗЯ: Изменять название класса и пакета */ -public interface PlaceRepository { +@Repository +public interface PlaceRepository extends JpaRepository { + Optional findPlaceById(Long id); + List findAll(); } diff --git a/src/main/java/com/example/nto/service/BookingService.java b/src/main/java/com/example/nto/service/BookingService.java index 31ec148..79b6bd0 100644 --- a/src/main/java/com/example/nto/service/BookingService.java +++ b/src/main/java/com/example/nto/service/BookingService.java @@ -1,10 +1,24 @@ package com.example.nto.service; +import com.example.nto.DTO.BookingDTO; +import com.example.nto.entity.Booking; +import com.example.nto.entity.Employee; +import com.example.nto.entity.Place; +import org.springframework.stereotype.Service; + +import java.time.LocalDate; +import java.util.List; +import java.util.Map; +import java.util.Optional; + /** * TODO: ДОРАБОТАТЬ в рамках задания * ================================= * МОЖНО: Добавлять методы, аннотации, зависимости * НЕЛЬЗЯ: Изменять название класса и пакета */ + public interface BookingService { + Map> freeBookingPlace(String code); + BookingDTO createBooking(String code, LocalDate date, Long placeId); } diff --git a/src/main/java/com/example/nto/service/EmployeeService.java b/src/main/java/com/example/nto/service/EmployeeService.java index cccd209..591781b 100644 --- a/src/main/java/com/example/nto/service/EmployeeService.java +++ b/src/main/java/com/example/nto/service/EmployeeService.java @@ -1,10 +1,17 @@ package com.example.nto.service; +import com.example.nto.entity.Employee; +import org.springframework.stereotype.Service; + +import java.util.Optional; + /** * TODO: ДОРАБОТАТЬ в рамках задания * ================================= * МОЖНО: Добавлять методы, аннотации, зависимости * НЕЛЬЗЯ: Изменять название класса и пакета */ + public interface EmployeeService { + Employee getEmployeeByCode(String code); } diff --git a/src/main/java/com/example/nto/service/impl/BookingServiceImpl.java b/src/main/java/com/example/nto/service/impl/BookingServiceImpl.java index d24b244..878855e 100644 --- a/src/main/java/com/example/nto/service/impl/BookingServiceImpl.java +++ b/src/main/java/com/example/nto/service/impl/BookingServiceImpl.java @@ -1,6 +1,26 @@ package com.example.nto.service.impl; +import com.example.nto.DTO.BookingDTO; +import com.example.nto.DTO.PlaceDTO; +import com.example.nto.entity.Booking; +import com.example.nto.entity.Employee; +import com.example.nto.entity.Place; +import com.example.nto.exceptions.BookingAlreadyExistsException; +import com.example.nto.exceptions.EmployeeNotFoundException; +import com.example.nto.exceptions.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 com.example.nto.service.EmployeeService; +import com.example.nto.utils.MapperEnToDTO; +import lombok.RequiredArgsConstructor; +import org.springframework.cglib.core.Local; +import org.springframework.stereotype.Service; + +import java.time.LocalDate; +import java.util.*; +import java.util.stream.Collectors; /** * TODO: ДОРАБОТАТЬ в рамках задания @@ -8,5 +28,73 @@ import com.example.nto.service.BookingService; * МОЖНО: Добавлять методы, аннотации, зависимости * НЕЛЬЗЯ: Изменять название класса и пакета */ +@Service +@RequiredArgsConstructor public class BookingServiceImpl implements BookingService { + + private static final int DAYS_COUNT = 3; + private final BookingRepository bookingRepository; + private final EmployeeRepository employeeRepository; + private final PlaceRepository placeRepository; + + + @Override + public Map> freeBookingPlace(String code) { + Optional employee = employeeRepository.findEmployeeByCode(code); + if(employee.isEmpty()){ + throw new EmployeeNotFoundException("Employee with " + code + " not found!"); + } + + LocalDate startTime = LocalDate.now(); + LocalDate endTime = startTime.plusDays(DAYS_COUNT); + + List allBookings = bookingRepository.findBookingByDateBetween(startTime, endTime); + List allPlaces = placeRepository.findAll(); + Map> availablePlacesByDate = new HashMap<>(); + for (LocalDate date = startTime; !date.isAfter(endTime); date = date.plusDays(1)) { + LocalDate currentDate = date; + + Set bookedPlaceIds = allBookings.stream() + .filter(b -> b.getDate().equals(currentDate)) + .map(b -> b.getPlace().getId()) + .collect(Collectors.toSet()); + + List freePlaces = allPlaces.stream() + .filter(p -> !bookedPlaceIds.contains(p.getId())) + .collect(Collectors.toList()); + availablePlacesByDate.put(currentDate, freePlaces); + } + + return availablePlacesByDate; + } + + @Override + public BookingDTO createBooking(String code, LocalDate date, Long placeId) { + + Optional employee = employeeRepository.findEmployeeByCode(code); + + if(employee.isEmpty()){ + throw new EmployeeNotFoundException("Employee with " + code + " not found!"); + } + + Optional place = placeRepository.findPlaceById(placeId); + + if(place.isEmpty()){ + throw new PlaceNotFoundException("Place with " + placeId + " not found!"); + } + + Optional booking = bookingRepository.findBookingByDateAndPlaceId(date, placeId); + if(booking.isPresent()){ + throw new BookingAlreadyExistsException("Booking with " + date + " and " + placeId + " already exists!"); + } + + Booking booking1 = new Booking(); + booking1.setDate(date); + booking1.setPlace(place.get()); + booking1.setEmployee(employee.get()); + + return MapperEnToDTO.toBookingInfoDto(bookingRepository.save(booking1)); + } + + } diff --git a/src/main/java/com/example/nto/service/impl/EmployeeServiceImpl.java b/src/main/java/com/example/nto/service/impl/EmployeeServiceImpl.java index f8125e5..8cb0b48 100644 --- a/src/main/java/com/example/nto/service/impl/EmployeeServiceImpl.java +++ b/src/main/java/com/example/nto/service/impl/EmployeeServiceImpl.java @@ -1,6 +1,13 @@ package com.example.nto.service.impl; +import com.example.nto.entity.Employee; +import com.example.nto.exceptions.EmployeeNotFoundException; +import com.example.nto.repository.EmployeeRepository; import com.example.nto.service.EmployeeService; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.util.Optional; /** * TODO: ДОРАБОТАТЬ в рамках задания @@ -8,5 +15,20 @@ import com.example.nto.service.EmployeeService; * МОЖНО: Добавлять методы, аннотации, зависимости * НЕЛЬЗЯ: Изменять название класса и пакета */ +@Service +@RequiredArgsConstructor public class EmployeeServiceImpl implements EmployeeService { + + private final EmployeeRepository employeeRepository; + + @Override + public Employee getEmployeeByCode(String code) { + Optional employee = employeeRepository.findEmployeeByCode(code); + + if(employee.isEmpty()){ + throw new EmployeeNotFoundException("Employee with code " + code + " not found!"); + } + + return employee.get(); + } } diff --git a/src/main/java/com/example/nto/utils/MapperEnToDTO.java b/src/main/java/com/example/nto/utils/MapperEnToDTO.java new file mode 100644 index 0000000..1a2e331 --- /dev/null +++ b/src/main/java/com/example/nto/utils/MapperEnToDTO.java @@ -0,0 +1,40 @@ +package com.example.nto.utils; + +import com.example.nto.DTO.BookingDTO; +import com.example.nto.DTO.EmployeeDTO; +import com.example.nto.DTO.PlaceDTO; +import com.example.nto.entity.Booking; +import com.example.nto.entity.Employee; +import com.example.nto.entity.Place; + +public class MapperEnToDTO { + public static EmployeeDTO toEmployeeInfoDto(Employee employee) { + EmployeeDTO dto = new EmployeeDTO(); + dto.setName(employee.getName()); + dto.setPhotoUrl(employee.getPhotoUrl()); + + if (employee.getBookingList() != null) { + for (Booking booking : employee.getBookingList()) { + BookingDTO bookingDto = toBookingInfoDto(booking); + dto.getBookings().put(booking.getDate().toString(), bookingDto); + } + } + + return dto; + } + public static BookingDTO toBookingInfoDto(Booking booking) { + BookingDTO dto = new BookingDTO(); + dto.setId(booking.getId()); + dto.setPlace(toPlaceDto(booking.getPlace())); + return dto; + } + + public static PlaceDTO toPlaceDto(Place place) { + if (place == null) return null; + + PlaceDTO dto = new PlaceDTO(); + dto.setId(place.getId()); + dto.setName(place.getPlace()); + return dto; + } +}