From a6954c20134f6d257945700d19dce7bee199e033 Mon Sep 17 00:00:00 2001 From: ci-bot Date: Mon, 24 Nov 2025 17:17:06 +0000 Subject: [PATCH 01/20] Update .gitea/workflows/workflow.yml --- .gitea/workflows/workflow.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitea/workflows/workflow.yml b/.gitea/workflows/workflow.yml index 20fe015..5690186 100644 --- a/.gitea/workflows/workflow.yml +++ b/.gitea/workflows/workflow.yml @@ -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 -- 2.34.1 From af09df8047ba2b6a8c4075f6cc5051b5e93ca68a Mon Sep 17 00:00:00 2001 From: Bhumi Shah Date: Fri, 5 Dec 2025 20:31:13 +0300 Subject: [PATCH 02/20] Win --- src/main/java/com/example/nto/App.java | 13 ++ .../nto/controller/BookingController.java | 111 ++++++++++++++++++ .../nto/controller/EmployeeController.java | 50 +++++++- .../EmployeeFullInfoResponseDto.java | 27 +++++ .../java/com/example/nto/entity/Booking.java | 43 +++++-- .../java/com/example/nto/entity/Employee.java | 28 ++++- .../java/com/example/nto/entity/Place.java | 25 +++- .../nto/excepation/ConflictException.java | 7 ++ .../excepation/EmployeeNotFoundException.java | 7 ++ .../excepation/IllegalArgumentException.java | 7 ++ .../nto/repository/BookingRepository.java | 15 ++- .../nto/repository/EmployeeRepository.java | 8 +- .../nto/repository/PlaceRepository.java | 7 +- .../example/nto/service/BookingService.java | 13 +- .../example/nto/service/EmployeeService.java | 8 ++ .../com/example/nto/service/PlaceServise.java | 25 ++++ .../nto/service/impl/BookingServiceImpl.java | 64 +++++++++- .../nto/service/impl/EmployeeServiceImpl.java | 33 +++++- 18 files changed, 458 insertions(+), 33 deletions(-) create mode 100644 src/main/java/com/example/nto/controller/EmployeeFullInfoResponseDto.java create mode 100644 src/main/java/com/example/nto/excepation/ConflictException.java create mode 100644 src/main/java/com/example/nto/excepation/EmployeeNotFoundException.java create mode 100644 src/main/java/com/example/nto/excepation/IllegalArgumentException.java create mode 100644 src/main/java/com/example/nto/service/PlaceServise.java diff --git a/src/main/java/com/example/nto/App.java b/src/main/java/com/example/nto/App.java index e453f89..65401fc 100644 --- a/src/main/java/com/example/nto/App.java +++ b/src/main/java/com/example/nto/App.java @@ -1,4 +1,8 @@ package com.example.nto; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; /** * TODO: ДОРАБОТАТЬ в рамках задания @@ -6,7 +10,16 @@ package com.example.nto; * МОЖНО: Добавлять методы, аннотации, зависимости * НЕЛЬЗЯ: Изменять название класса и пакета */ +@SpringBootApplication public class App { public static void main(String[] args) { + SpringApplication.run(App.class , args); + } + @RestController + public static class HomeController { + @GetMapping("/") + public String home() { + return "Сервер работает!"; + } } } diff --git a/src/main/java/com/example/nto/controller/BookingController.java b/src/main/java/com/example/nto/controller/BookingController.java index 9885f84..562837d 100644 --- a/src/main/java/com/example/nto/controller/BookingController.java +++ b/src/main/java/com/example/nto/controller/BookingController.java @@ -1,10 +1,121 @@ package com.example.nto.controller; +import com.example.nto.entity.Booking; +import com.example.nto.entity.Employee; +import com.example.nto.entity.Place; +import com.example.nto.excepation.EmployeeNotFoundException; +import com.example.nto.repository.BookingRepository; +import com.example.nto.repository.PlaceRepository; +import com.example.nto.service.BookingService; +import com.example.nto.service.EmployeeService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.format.annotation.DateTimeFormat; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.time.LocalDate; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Map; + /** * TODO: ДОРАБОТАТЬ в рамках задания * ================================= * МОЖНО: Добавлять методы, аннотации, зависимости * НЕЛЬЗЯ: Изменять название класса и пакета */ +@RestController +@RequestMapping("/api") public class BookingController { + @Autowired + private BookingService bookingService; + @Autowired + private PlaceRepository placeRepository; + @Autowired + private BookingRepository bookingRepository; + @Autowired + private EmployeeService employeeService; + + @GetMapping("/{code}/booking") + public ResponseEntity getAvailableBookings(@PathVariable String code) { + try { + Map response = bookingService.findAvailableBookings(code); + return ResponseEntity.ok(response); + } catch (IllegalArgumentException e) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage()); + } catch (EmployeeNotFoundException e) { + return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(e.getMessage()); + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Unexpected error"); + } + } + + @PostMapping("/{code}/book") + public ResponseEntity createBooking( + @PathVariable String code, + @RequestParam Long placeId, + @RequestParam String date + ) { + try { + // Проверка обязательных параметров + if (placeId == null || date == null || date.isEmpty()) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST) + .body("Place ID and date must be provided"); + } + Employee employee = employeeService.getEmployeeWithBookings(code); + if (employee == null) { + return ResponseEntity.status(HttpStatus.UNAUTHORIZED) + .body("Employee not found"); + } + + // Получаем место + Place place = placeRepository.findById(placeId).orElse(null); + if (place == null) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST) + .body("Place not found"); + } + + // Парсим дату + LocalDate bookingDate; + try { + bookingDate = LocalDate.parse(date); + } catch (Exception ex) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST) + .body("Invalid date format. Use yyyy-MM-dd"); + } + + // Проверяем, свободно ли место на эту дату + boolean exists = bookingRepository.findByDateBetween(bookingDate, bookingDate) + .stream() + .anyMatch(b -> b.getPlace().getId() == placeId); + if (exists) { + return ResponseEntity.status(HttpStatus.CONFLICT) + .body("Place already booked for this date"); + } + Booking booking = new Booking(); + booking.setEmployee((Map) employee); + booking.setPlace(place); + booking.setDate(bookingDate); + bookingRepository.save(booking); + + // Формируем ответ + Map response = new LinkedHashMap<>(); + response.put("id", booking.getId()); + response.put("date", booking.getDate()); + response.put("placeId", place.getId()); + response.put("employeeId", employee.getId()); + + return ResponseEntity.status(HttpStatus.CREATED).body(response); + + } catch (java.lang.IllegalArgumentException e) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage()); + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Unexpected error"); + } + } } + + + + diff --git a/src/main/java/com/example/nto/controller/EmployeeController.java b/src/main/java/com/example/nto/controller/EmployeeController.java index 47658f9..e218396 100644 --- a/src/main/java/com/example/nto/controller/EmployeeController.java +++ b/src/main/java/com/example/nto/controller/EmployeeController.java @@ -1,10 +1,58 @@ package com.example.nto.controller; +import com.example.nto.entity.Booking; +import com.example.nto.entity.Employee; +import com.example.nto.excepation.EmployeeNotFoundException; +import com.example.nto.repository.BookingRepository; +import com.example.nto.repository.EmployeeRepository; +import com.example.nto.service.EmployeeService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + /** * TODO: ДОРАБОТАТЬ в рамках задания * ================================= * МОЖНО: Добавлять методы, аннотации, зависимости * НЕЛЬЗЯ: Изменять название класса и пакета */ +@RestController +@RequestMapping("/api") public class EmployeeController { -} + + @Autowired + private EmployeeService employeeService; + @GetMapping("/{code}/auth") + public ResponseEntity checkAuth( + @PathVariable String code, + @RequestHeader(value = "Authorization", required = false) String authHeader) { + if (authHeader == null || !authHeader.equals("Bearer valid-token")) { + return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build(); + } + try { + employeeService. getEmployeeWithBookings(code); + return ResponseEntity.ok().build(); + } catch (IllegalArgumentException e) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST).build(); + } catch (EmployeeNotFoundException e) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).build(); + } + } + + @GetMapping("/{code}/info") + public ResponseEntity getEmployeeInfo(@PathVariable String code) { + try { + Employee response = employeeService.getEmployeeWithBookings(code); + return ResponseEntity.ok(response); + } catch (IllegalArgumentException e) { + return ResponseEntity.status(400).body(e.getMessage()); + } catch (EmployeeNotFoundException e) { + return ResponseEntity.status(404).body(e.getMessage()); + } + } +} \ No newline at end of file diff --git a/src/main/java/com/example/nto/controller/EmployeeFullInfoResponseDto.java b/src/main/java/com/example/nto/controller/EmployeeFullInfoResponseDto.java new file mode 100644 index 0000000..5cf8c08 --- /dev/null +++ b/src/main/java/com/example/nto/controller/EmployeeFullInfoResponseDto.java @@ -0,0 +1,27 @@ +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 booking; + + @Data + @Builder + @NoArgsConstructor + @AllArgsConstructor + public static class BookingInfo { + private Long id; + private String place; + } +} \ No newline at end of file diff --git a/src/main/java/com/example/nto/entity/Booking.java b/src/main/java/com/example/nto/entity/Booking.java index 21c1981..c7b37eb 100644 --- a/src/main/java/com/example/nto/entity/Booking.java +++ b/src/main/java/com/example/nto/entity/Booking.java @@ -1,14 +1,13 @@ 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; import lombok.NoArgsConstructor; import java.time.LocalDate; +import java.util.Map; /** @@ -21,15 +20,45 @@ import java.time.LocalDate; @Builder @NoArgsConstructor @AllArgsConstructor +@Entity +@Table(name="booking") public class Booking { - + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) private long id; - private LocalDate date; - @ManyToOne(targetEntity = Place.class, fetch = FetchType.LAZY) @JoinColumn(name = "place_id") private Place place; - + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "employee_id") private Employee employee; + + + public Place getPlace() { + return place; + } + public LocalDate getDate() { + return date; + } + + public Object getId() { + return id; + } + + public Employee getEmployee() { + return employee; + } + + public void setDate(LocalDate date) { + this.date = date; + } + + public void setPlace(Place place) { + this.place = place; + } + + public void setEmployee(Map employee) { + this.employee = (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..14249f0 100644 --- a/src/main/java/com/example/nto/entity/Employee.java +++ b/src/main/java/com/example/nto/entity/Employee.java @@ -1,11 +1,11 @@ package com.example.nto.entity; +import com.fasterxml.jackson.annotation.JsonIgnore; import jakarta.persistence.*; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; - import java.util.List; @@ -19,10 +19,13 @@ import java.util.List; @Builder @NoArgsConstructor @AllArgsConstructor +@Entity +@Table(name="employee") public class Employee { - + @Id private long id; + private String name; private String code; @@ -30,5 +33,26 @@ public class Employee { private String photoUrl; @OneToMany(mappedBy = "employee", cascade = CascadeType.ALL, fetch = FetchType.LAZY) + @JsonIgnore private List bookingList; + + public List getBookingList() { + return bookingList; + } + + public Object getCode() { + return code; + } + + public Object getPhotoUrl() { + return photoUrl; + } + + public Object getName() { + return name; + } + + public Object getId() { + return id; + } } diff --git a/src/main/java/com/example/nto/entity/Place.java b/src/main/java/com/example/nto/entity/Place.java index 00c253b..32947c7 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; @@ -19,11 +17,26 @@ import lombok.NoArgsConstructor; @Builder @NoArgsConstructor @AllArgsConstructor +@Entity +@Table(name="place") public class Place { - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) private long id; - + @Column(name = "place_name") private String place; + + public Place(int id, String place) { + } + + + public Object getPlace() { + return place; + } + + public Long getId() { + return id; + } } + + + diff --git a/src/main/java/com/example/nto/excepation/ConflictException.java b/src/main/java/com/example/nto/excepation/ConflictException.java new file mode 100644 index 0000000..2c6ad3b --- /dev/null +++ b/src/main/java/com/example/nto/excepation/ConflictException.java @@ -0,0 +1,7 @@ +package com.example.nto.excepation; + +public class ConflictException extends RuntimeException { + public ConflictException(String message) { + super(message); + } +} diff --git a/src/main/java/com/example/nto/excepation/EmployeeNotFoundException.java b/src/main/java/com/example/nto/excepation/EmployeeNotFoundException.java new file mode 100644 index 0000000..c59e73a --- /dev/null +++ b/src/main/java/com/example/nto/excepation/EmployeeNotFoundException.java @@ -0,0 +1,7 @@ +package com.example.nto.excepation; + +public class EmployeeNotFoundException extends RuntimeException{ + public EmployeeNotFoundException(String msg) { + super(msg); + } +} diff --git a/src/main/java/com/example/nto/excepation/IllegalArgumentException.java b/src/main/java/com/example/nto/excepation/IllegalArgumentException.java new file mode 100644 index 0000000..00cc715 --- /dev/null +++ b/src/main/java/com/example/nto/excepation/IllegalArgumentException.java @@ -0,0 +1,7 @@ +package com.example.nto.excepation; + +public class IllegalArgumentException extends RuntimeException { + public IllegalArgumentException(String message) { + super(message); + } +} diff --git a/src/main/java/com/example/nto/repository/BookingRepository.java b/src/main/java/com/example/nto/repository/BookingRepository.java index 303bb54..f2876d4 100644 --- a/src/main/java/com/example/nto/repository/BookingRepository.java +++ b/src/main/java/com/example/nto/repository/BookingRepository.java @@ -1,10 +1,23 @@ package com.example.nto.repository; +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.stereotype.Repository; + +import java.time.LocalDate; +import java.util.List; +import java.util.Optional; + /** * TODO: ДОРАБОТАТЬ в рамках задания * ================================= * МОЖНО: Добавлять методы, аннотации, зависимости * НЕЛЬЗЯ: Изменять название класса и пакета */ -public interface BookingRepository { +@Repository +public interface BookingRepository extends JpaRepository { + List findByEmployee(Employee employee); + List findByDateBetween(LocalDate start, LocalDate end); } diff --git a/src/main/java/com/example/nto/repository/EmployeeRepository.java b/src/main/java/com/example/nto/repository/EmployeeRepository.java index 210d29c..b0e5b28 100644 --- a/src/main/java/com/example/nto/repository/EmployeeRepository.java +++ b/src/main/java/com/example/nto/repository/EmployeeRepository.java @@ -1,10 +1,16 @@ package com.example.nto.repository; +import com.example.nto.entity.Employee; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + /** * TODO: ДОРАБОТАТЬ в рамках задания * ================================= * МОЖНО: Добавлять методы, аннотации, зависимости * НЕЛЬЗЯ: Изменять название класса и пакета */ -public interface EmployeeRepository { +@Repository +public interface EmployeeRepository extends JpaRepository { + Employee findByCode(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..425dacb 100644 --- a/src/main/java/com/example/nto/repository/PlaceRepository.java +++ b/src/main/java/com/example/nto/repository/PlaceRepository.java @@ -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 { } diff --git a/src/main/java/com/example/nto/service/BookingService.java b/src/main/java/com/example/nto/service/BookingService.java index 31ec148..6629e95 100644 --- a/src/main/java/com/example/nto/service/BookingService.java +++ b/src/main/java/com/example/nto/service/BookingService.java @@ -1,10 +1,11 @@ package com.example.nto.service; -/** - * TODO: ДОРАБОТАТЬ в рамках задания - * ================================= - * МОЖНО: Добавлять методы, аннотации, зависимости - * НЕЛЬЗЯ: Изменять название класса и пакета - */ +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Map; + +@Service public interface BookingService { + Map findAvailableBookings(String employeeCode); } diff --git a/src/main/java/com/example/nto/service/EmployeeService.java b/src/main/java/com/example/nto/service/EmployeeService.java index cccd209..35f7b21 100644 --- a/src/main/java/com/example/nto/service/EmployeeService.java +++ b/src/main/java/com/example/nto/service/EmployeeService.java @@ -1,4 +1,10 @@ package com.example.nto.service; +import com.example.nto.entity.Booking; +import com.example.nto.entity.Employee; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Map; /** * TODO: ДОРАБОТАТЬ в рамках задания @@ -6,5 +12,7 @@ package com.example.nto.service; * МОЖНО: Добавлять методы, аннотации, зависимости * НЕЛЬЗЯ: Изменять название класса и пакета */ +@Service public interface EmployeeService { + Employee getEmployeeWithBookings(String code); } diff --git a/src/main/java/com/example/nto/service/PlaceServise.java b/src/main/java/com/example/nto/service/PlaceServise.java new file mode 100644 index 0000000..4ee1351 --- /dev/null +++ b/src/main/java/com/example/nto/service/PlaceServise.java @@ -0,0 +1,25 @@ +package com.example.nto.service; +import com.example.nto.entity.Place; +import com.example.nto.repository.PlaceRepository; +import jakarta.annotation.PostConstruct; +import org.springframework.stereotype.Service; + +@Service +public class PlaceServise { + private final PlaceRepository placeRepository; + + public PlaceServise(PlaceRepository placeRepository) { + this.placeRepository = placeRepository; + } + + @PostConstruct + public void Places() { + if(placeRepository.count() == 0) { + placeRepository.save(new Place(1,"K-19")); + placeRepository.save(new Place(2,"M-16")); + placeRepository.save(new Place(3,"T-1")); + } + } + +} + 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..341abcf 100644 --- a/src/main/java/com/example/nto/service/impl/BookingServiceImpl.java +++ b/src/main/java/com/example/nto/service/impl/BookingServiceImpl.java @@ -1,12 +1,64 @@ package com.example.nto.service.impl; +import com.example.nto.entity.Booking; +import com.example.nto.entity.Employee; +import com.example.nto.entity.Place; +import com.example.nto.excepation.EmployeeNotFoundException; +import com.example.nto.repository.BookingRepository; +import com.example.nto.repository.PlaceRepository; import com.example.nto.service.BookingService; +import com.example.nto.service.EmployeeService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; -/** - * TODO: ДОРАБОТАТЬ в рамках задания - * ================================= - * МОЖНО: Добавлять методы, аннотации, зависимости - * НЕЛЬЗЯ: Изменять название класса и пакета - */ +import java.time.LocalDate; +import java.util.*; + +@Service public class BookingServiceImpl implements BookingService { + + @Autowired + private BookingRepository bookingRepository; + + @Autowired + private PlaceRepository placeRepository; + + @Autowired + private EmployeeService employeeService; + + @Override + public Map findAvailableBookings(String employeeCode) { + if (employeeCode == null || employeeCode.isEmpty()) { + throw new IllegalArgumentException("Employee code cannot be null or empty"); + } + Employee employee = employeeService.getEmployeeWithBookings(employeeCode); + if (employee == null) { + throw new EmployeeNotFoundException("Employee not found with code: " + employeeCode); + } + + LocalDate today = LocalDate.now(); + LocalDate endDate = today.plusDays(3); + List bookings = bookingRepository.findByDateBetween(today, endDate); + List places = placeRepository.findAll(); + Map>> result = new LinkedHashMap<>(); + for (int i = 0; i <= 3; i++) { + LocalDate date = today.plusDays(i); + List> freePlaces = new ArrayList<>(); + for (Place place : places) { + boolean isBooked = bookings.stream() + .anyMatch(b -> b.getDate().isEqual(date) && b.getPlace().getId() == place.getId()); + if (!isBooked) { + Map placeInfo = new LinkedHashMap<>(); + placeInfo.put("id", place.getId()); + placeInfo.put("place", place.getPlace()); + freePlaces.add(placeInfo); + } + } + result.put(date.toString(), freePlaces); + } + return Collections.unmodifiableMap(result); + } } + + + 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..8b330fc 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,15 @@ package com.example.nto.service.impl; - +import com.example.nto.entity.Booking; +import com.example.nto.entity.Employee; +import com.example.nto.excepation.EmployeeNotFoundException; +import com.example.nto.repository.BookingRepository; +import com.example.nto.repository.EmployeeRepository; import com.example.nto.service.EmployeeService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + /** * TODO: ДОРАБОТАТЬ в рамках задания @@ -8,5 +17,25 @@ import com.example.nto.service.EmployeeService; * МОЖНО: Добавлять методы, аннотации, зависимости * НЕЛЬЗЯ: Изменять название класса и пакета */ +@Service public class EmployeeServiceImpl implements EmployeeService { -} + + @Autowired + private EmployeeRepository employeeRepository; + + @Autowired + private BookingRepository bookingRepository; + + @Override + public Employee getEmployeeWithBookings(String code) { + if (code == null || code.isEmpty()) { + throw new IllegalArgumentException("Employee code cannot be null or empty"); + } + Employee employee = employeeRepository.findByCode(code); + if (employee == null) { + throw new EmployeeNotFoundException("Employee not found with code: " + code); + } + return employee; + } + +} \ No newline at end of file -- 2.34.1 From 9f437b83e21697ec939b048b538fd4a4adbfb0db Mon Sep 17 00:00:00 2001 From: Bhumi Shah Date: Fri, 5 Dec 2025 20:52:06 +0300 Subject: [PATCH 03/20] Win --- .../com/example/nto/controller/BookingController.java | 11 ----------- .../example/nto/controller/EmployeeController.java | 2 -- .../com/example/nto/repository/BookingRepository.java | 2 -- .../java/com/example/nto/service/EmployeeService.java | 4 ---- 4 files changed, 19 deletions(-) diff --git a/src/main/java/com/example/nto/controller/BookingController.java b/src/main/java/com/example/nto/controller/BookingController.java index 562837d..8b62191 100644 --- a/src/main/java/com/example/nto/controller/BookingController.java +++ b/src/main/java/com/example/nto/controller/BookingController.java @@ -58,7 +58,6 @@ public class BookingController { @RequestParam String date ) { try { - // Проверка обязательных параметров if (placeId == null || date == null || date.isEmpty()) { return ResponseEntity.status(HttpStatus.BAD_REQUEST) .body("Place ID and date must be provided"); @@ -68,15 +67,11 @@ public class BookingController { return ResponseEntity.status(HttpStatus.UNAUTHORIZED) .body("Employee not found"); } - - // Получаем место Place place = placeRepository.findById(placeId).orElse(null); if (place == null) { return ResponseEntity.status(HttpStatus.BAD_REQUEST) .body("Place not found"); } - - // Парсим дату LocalDate bookingDate; try { bookingDate = LocalDate.parse(date); @@ -84,8 +79,6 @@ public class BookingController { 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); @@ -98,16 +91,12 @@ public class BookingController { booking.setPlace(place); booking.setDate(bookingDate); bookingRepository.save(booking); - - // Формируем ответ Map response = new LinkedHashMap<>(); response.put("id", booking.getId()); response.put("date", booking.getDate()); response.put("placeId", place.getId()); response.put("employeeId", employee.getId()); - return ResponseEntity.status(HttpStatus.CREATED).body(response); - } catch (java.lang.IllegalArgumentException e) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage()); } catch (Exception e) { diff --git a/src/main/java/com/example/nto/controller/EmployeeController.java b/src/main/java/com/example/nto/controller/EmployeeController.java index e218396..6dc674c 100644 --- a/src/main/java/com/example/nto/controller/EmployeeController.java +++ b/src/main/java/com/example/nto/controller/EmployeeController.java @@ -24,7 +24,6 @@ import java.util.Map; @RestController @RequestMapping("/api") public class EmployeeController { - @Autowired private EmployeeService employeeService; @GetMapping("/{code}/auth") @@ -43,7 +42,6 @@ public class EmployeeController { return ResponseEntity.status(HttpStatus.NOT_FOUND).build(); } } - @GetMapping("/{code}/info") public ResponseEntity getEmployeeInfo(@PathVariable String code) { try { diff --git a/src/main/java/com/example/nto/repository/BookingRepository.java b/src/main/java/com/example/nto/repository/BookingRepository.java index f2876d4..7764391 100644 --- a/src/main/java/com/example/nto/repository/BookingRepository.java +++ b/src/main/java/com/example/nto/repository/BookingRepository.java @@ -2,13 +2,11 @@ package com.example.nto.repository; 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.stereotype.Repository; import java.time.LocalDate; import java.util.List; -import java.util.Optional; /** * TODO: ДОРАБОТАТЬ в рамках задания diff --git a/src/main/java/com/example/nto/service/EmployeeService.java b/src/main/java/com/example/nto/service/EmployeeService.java index 35f7b21..287e5b7 100644 --- a/src/main/java/com/example/nto/service/EmployeeService.java +++ b/src/main/java/com/example/nto/service/EmployeeService.java @@ -1,11 +1,7 @@ package com.example.nto.service; -import com.example.nto.entity.Booking; import com.example.nto.entity.Employee; import org.springframework.stereotype.Service; -import java.util.List; -import java.util.Map; - /** * TODO: ДОРАБОТАТЬ в рамках задания * ================================= -- 2.34.1 From 884adb5d6e9bd49882bffa041ecbc1bc9d600c29 Mon Sep 17 00:00:00 2001 From: Bhumi Shah Date: Fri, 5 Dec 2025 20:55:19 +0300 Subject: [PATCH 04/20] Win --- .../com/example/nto/controller/BookingController.java | 2 -- .../com/example/nto/controller/EmployeeController.java | 8 -------- .../com/example/nto/excepation/ConflictException.java | 7 ------- src/main/java/com/example/nto/service/BookingService.java | 2 -- src/main/java/com/example/nto/service/PlaceServise.java | 1 - .../com/example/nto/service/impl/BookingServiceImpl.java | 1 - .../com/example/nto/service/impl/EmployeeServiceImpl.java | 8 -------- 7 files changed, 29 deletions(-) delete mode 100644 src/main/java/com/example/nto/excepation/ConflictException.java diff --git a/src/main/java/com/example/nto/controller/BookingController.java b/src/main/java/com/example/nto/controller/BookingController.java index 8b62191..e1dd017 100644 --- a/src/main/java/com/example/nto/controller/BookingController.java +++ b/src/main/java/com/example/nto/controller/BookingController.java @@ -9,13 +9,11 @@ import com.example.nto.repository.PlaceRepository; import com.example.nto.service.BookingService; import com.example.nto.service.EmployeeService; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.format.annotation.DateTimeFormat; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.time.LocalDate; -import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; diff --git a/src/main/java/com/example/nto/controller/EmployeeController.java b/src/main/java/com/example/nto/controller/EmployeeController.java index 6dc674c..ae01a9a 100644 --- a/src/main/java/com/example/nto/controller/EmployeeController.java +++ b/src/main/java/com/example/nto/controller/EmployeeController.java @@ -1,20 +1,12 @@ package com.example.nto.controller; - -import com.example.nto.entity.Booking; import com.example.nto.entity.Employee; import com.example.nto.excepation.EmployeeNotFoundException; -import com.example.nto.repository.BookingRepository; -import com.example.nto.repository.EmployeeRepository; import com.example.nto.service.EmployeeService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - /** * TODO: ДОРАБОТАТЬ в рамках задания * ================================= diff --git a/src/main/java/com/example/nto/excepation/ConflictException.java b/src/main/java/com/example/nto/excepation/ConflictException.java deleted file mode 100644 index 2c6ad3b..0000000 --- a/src/main/java/com/example/nto/excepation/ConflictException.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.example.nto.excepation; - -public class ConflictException extends RuntimeException { - public ConflictException(String message) { - super(message); - } -} diff --git a/src/main/java/com/example/nto/service/BookingService.java b/src/main/java/com/example/nto/service/BookingService.java index 6629e95..c1c7d39 100644 --- a/src/main/java/com/example/nto/service/BookingService.java +++ b/src/main/java/com/example/nto/service/BookingService.java @@ -1,8 +1,6 @@ package com.example.nto.service; import org.springframework.stereotype.Service; - -import java.util.List; import java.util.Map; @Service diff --git a/src/main/java/com/example/nto/service/PlaceServise.java b/src/main/java/com/example/nto/service/PlaceServise.java index 4ee1351..782f312 100644 --- a/src/main/java/com/example/nto/service/PlaceServise.java +++ b/src/main/java/com/example/nto/service/PlaceServise.java @@ -7,7 +7,6 @@ import org.springframework.stereotype.Service; @Service public class PlaceServise { private final PlaceRepository placeRepository; - public PlaceServise(PlaceRepository placeRepository) { this.placeRepository = placeRepository; } 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 341abcf..c9c83c7 100644 --- a/src/main/java/com/example/nto/service/impl/BookingServiceImpl.java +++ b/src/main/java/com/example/nto/service/impl/BookingServiceImpl.java @@ -35,7 +35,6 @@ public class BookingServiceImpl implements BookingService { if (employee == null) { throw new EmployeeNotFoundException("Employee not found with code: " + employeeCode); } - LocalDate today = LocalDate.now(); LocalDate endDate = today.plusDays(3); List bookings = bookingRepository.findByDateBetween(today, endDate); 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 8b330fc..0557b00 100644 --- a/src/main/java/com/example/nto/service/impl/EmployeeServiceImpl.java +++ b/src/main/java/com/example/nto/service/impl/EmployeeServiceImpl.java @@ -1,5 +1,4 @@ package com.example.nto.service.impl; -import com.example.nto.entity.Booking; import com.example.nto.entity.Employee; import com.example.nto.excepation.EmployeeNotFoundException; import com.example.nto.repository.BookingRepository; @@ -8,8 +7,6 @@ import com.example.nto.service.EmployeeService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import java.util.List; - /** * TODO: ДОРАБОТАТЬ в рамках задания @@ -19,13 +16,8 @@ import java.util.List; */ @Service public class EmployeeServiceImpl implements EmployeeService { - @Autowired private EmployeeRepository employeeRepository; - - @Autowired - private BookingRepository bookingRepository; - @Override public Employee getEmployeeWithBookings(String code) { if (code == null || code.isEmpty()) { -- 2.34.1 From bbd0182ae894091c82914061972801ff7560818d Mon Sep 17 00:00:00 2001 From: Bhumi Shah Date: Fri, 5 Dec 2025 20:56:00 +0300 Subject: [PATCH 05/20] Win --- src/main/java/com/example/nto/App.java | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/main/java/com/example/nto/App.java b/src/main/java/com/example/nto/App.java index 65401fc..ad42870 100644 --- a/src/main/java/com/example/nto/App.java +++ b/src/main/java/com/example/nto/App.java @@ -15,11 +15,4 @@ public class App { public static void main(String[] args) { SpringApplication.run(App.class , args); } - @RestController - public static class HomeController { - @GetMapping("/") - public String home() { - return "Сервер работает!"; - } - } } -- 2.34.1 From 3b64ad9626e33fe2e38b789bf0e604b27479380f Mon Sep 17 00:00:00 2001 From: Bhumi Shah Date: Fri, 5 Dec 2025 23:02:14 +0300 Subject: [PATCH 06/20] 1111 --- .../nto/controller/BookingController.java | 2 +- .../nto/controller/EmployeeController.java | 42 ++++++++++--------- .../EmployeeFullInfoResponseDto.java | 27 ------------ .../java/com/example/nto/entity/Employee.java | 33 +++++---------- .../nto/repository/BookingRepository.java | 1 - .../nto/repository/EmployeeRepository.java | 6 ++- .../example/nto/service/EmployeeService.java | 2 +- .../nto/service/impl/BookingServiceImpl.java | 2 +- .../nto/service/impl/EmployeeServiceImpl.java | 25 +++++------ 9 files changed, 53 insertions(+), 87 deletions(-) delete mode 100644 src/main/java/com/example/nto/controller/EmployeeFullInfoResponseDto.java diff --git a/src/main/java/com/example/nto/controller/BookingController.java b/src/main/java/com/example/nto/controller/BookingController.java index e1dd017..61014f3 100644 --- a/src/main/java/com/example/nto/controller/BookingController.java +++ b/src/main/java/com/example/nto/controller/BookingController.java @@ -60,7 +60,7 @@ public class BookingController { return ResponseEntity.status(HttpStatus.BAD_REQUEST) .body("Place ID and date must be provided"); } - Employee employee = employeeService.getEmployeeWithBookings(code); + Employee employee = employeeService.getEmployeeByCode(code); if (employee == null) { return ResponseEntity.status(HttpStatus.UNAUTHORIZED) .body("Employee not found"); diff --git a/src/main/java/com/example/nto/controller/EmployeeController.java b/src/main/java/com/example/nto/controller/EmployeeController.java index ae01a9a..55ac770 100644 --- a/src/main/java/com/example/nto/controller/EmployeeController.java +++ b/src/main/java/com/example/nto/controller/EmployeeController.java @@ -7,6 +7,10 @@ import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.stream.Collectors; + /** * TODO: ДОРАБОТАТЬ в рамках задания * ================================= @@ -18,31 +22,31 @@ import org.springframework.web.bind.annotation.*; public class EmployeeController { @Autowired private EmployeeService employeeService; - @GetMapping("/{code}/auth") - public ResponseEntity checkAuth( - @PathVariable String code, - @RequestHeader(value = "Authorization", required = false) String authHeader) { - if (authHeader == null || !authHeader.equals("Bearer valid-token")) { - return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build(); - } - try { - employeeService. getEmployeeWithBookings(code); - return ResponseEntity.ok().build(); - } catch (IllegalArgumentException e) { - return ResponseEntity.status(HttpStatus.BAD_REQUEST).build(); - } catch (EmployeeNotFoundException e) { - return ResponseEntity.status(HttpStatus.NOT_FOUND).build(); - } - } @GetMapping("/{code}/info") public ResponseEntity getEmployeeInfo(@PathVariable String code) { try { - Employee response = employeeService.getEmployeeWithBookings(code); + Employee employee = employeeService.getEmployeeByCode(code); + Map response = new LinkedHashMap<>(); + response.put("name", employee.getName()); + response.put("photoUrl", employee.getPhotoUrl()); + Map bookingMap = employee.getBookings().stream() + .collect(Collectors.toMap( + b -> b.getDate().toString(), + b -> Map.of( + "id", b.getId(), + "place", b.getPlace().getPlace() + ), + (oldValue, newValue) -> newValue, + LinkedHashMap::new + )); + response.put("booking", bookingMap); return ResponseEntity.ok(response); } catch (IllegalArgumentException e) { - return ResponseEntity.status(400).body(e.getMessage()); + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage()); } 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"); } } } \ No newline at end of file diff --git a/src/main/java/com/example/nto/controller/EmployeeFullInfoResponseDto.java b/src/main/java/com/example/nto/controller/EmployeeFullInfoResponseDto.java deleted file mode 100644 index 5cf8c08..0000000 --- a/src/main/java/com/example/nto/controller/EmployeeFullInfoResponseDto.java +++ /dev/null @@ -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 booking; - - @Data - @Builder - @NoArgsConstructor - @AllArgsConstructor - public static class BookingInfo { - private Long id; - private String place; - } -} \ No newline at end of file diff --git a/src/main/java/com/example/nto/entity/Employee.java b/src/main/java/com/example/nto/entity/Employee.java index 14249f0..9298591 100644 --- a/src/main/java/com/example/nto/entity/Employee.java +++ b/src/main/java/com/example/nto/entity/Employee.java @@ -6,6 +6,9 @@ import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; + +import java.util.ArrayList; +import java.util.Arrays; import java.util.List; @@ -31,28 +34,12 @@ public class Employee { private String code; private String photoUrl; + @OneToMany(mappedBy = "employee", fetch = FetchType.LAZY, cascade = CascadeType.ALL) + private List bookings = new ArrayList<>(); - @OneToMany(mappedBy = "employee", cascade = CascadeType.ALL, fetch = FetchType.LAZY) - @JsonIgnore - private List bookingList; - - public List getBookingList() { - return bookingList; - } - - public Object getCode() { - return code; - } - - public Object getPhotoUrl() { - return photoUrl; - } - - public Object getName() { - return name; - } - - public Object getId() { - return id; - } + public long getId() { return id; } + public String getName() { return name; } + public String getCode() { return code; } + public String getPhotoUrl() { return photoUrl; } + public List getBookings() { return bookings; } } diff --git a/src/main/java/com/example/nto/repository/BookingRepository.java b/src/main/java/com/example/nto/repository/BookingRepository.java index 7764391..0061eca 100644 --- a/src/main/java/com/example/nto/repository/BookingRepository.java +++ b/src/main/java/com/example/nto/repository/BookingRepository.java @@ -16,6 +16,5 @@ import java.util.List; */ @Repository public interface BookingRepository extends JpaRepository { - List findByEmployee(Employee employee); List findByDateBetween(LocalDate start, LocalDate end); } diff --git a/src/main/java/com/example/nto/repository/EmployeeRepository.java b/src/main/java/com/example/nto/repository/EmployeeRepository.java index b0e5b28..212c682 100644 --- a/src/main/java/com/example/nto/repository/EmployeeRepository.java +++ b/src/main/java/com/example/nto/repository/EmployeeRepository.java @@ -4,6 +4,8 @@ import com.example.nto.entity.Employee; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; +import java.util.Optional; + /** * TODO: ДОРАБОТАТЬ в рамках задания * ================================= @@ -12,5 +14,5 @@ import org.springframework.stereotype.Repository; */ @Repository public interface EmployeeRepository extends JpaRepository { - Employee findByCode(String code); -} + Optional findByCode(String code); +} \ No newline at end of file diff --git a/src/main/java/com/example/nto/service/EmployeeService.java b/src/main/java/com/example/nto/service/EmployeeService.java index 287e5b7..c741635 100644 --- a/src/main/java/com/example/nto/service/EmployeeService.java +++ b/src/main/java/com/example/nto/service/EmployeeService.java @@ -10,5 +10,5 @@ import org.springframework.stereotype.Service; */ @Service public interface EmployeeService { - Employee getEmployeeWithBookings(String code); + 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 c9c83c7..3860ab6 100644 --- a/src/main/java/com/example/nto/service/impl/BookingServiceImpl.java +++ b/src/main/java/com/example/nto/service/impl/BookingServiceImpl.java @@ -31,7 +31,7 @@ public class BookingServiceImpl implements BookingService { if (employeeCode == null || employeeCode.isEmpty()) { throw new IllegalArgumentException("Employee code cannot be null or empty"); } - Employee employee = employeeService.getEmployeeWithBookings(employeeCode); + Employee employee = employeeService.getEmployeeByCode(employeeCode); if (employee == null) { throw new EmployeeNotFoundException("Employee not found with code: " + employeeCode); } 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 0557b00..3fc0dce 100644 --- a/src/main/java/com/example/nto/service/impl/EmployeeServiceImpl.java +++ b/src/main/java/com/example/nto/service/impl/EmployeeServiceImpl.java @@ -4,7 +4,10 @@ import com.example.nto.excepation.EmployeeNotFoundException; import com.example.nto.repository.BookingRepository; import com.example.nto.repository.EmployeeRepository; import com.example.nto.service.EmployeeService; +import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; @@ -15,19 +18,17 @@ import org.springframework.stereotype.Service; * НЕЛЬЗЯ: Изменять название класса и пакета */ @Service +@RequiredArgsConstructor public class EmployeeServiceImpl implements EmployeeService { @Autowired - private EmployeeRepository employeeRepository; - @Override - public Employee getEmployeeWithBookings(String code) { - if (code == null || code.isEmpty()) { - throw new IllegalArgumentException("Employee code cannot be null or empty"); - } - Employee employee = employeeRepository.findByCode(code); - if (employee == null) { - throw new EmployeeNotFoundException("Employee not found with code: " + code); - } - return employee; - } + private EmployeeRepository employeeRepository; + @Override + public Employee getEmployeeByCode(String code) { + if (code == null || code.isEmpty()) { + throw new IllegalArgumentException("Employee code is required"); + } + return employeeRepository.findByCode(code) + .orElseThrow(() -> new EmployeeNotFoundException("Employee not found")); + } } \ No newline at end of file -- 2.34.1 From c4bb98b3212e24d0e8bd14314d8b4ea346c4d4ad Mon Sep 17 00:00:00 2001 From: Bhumi Shah Date: Sat, 6 Dec 2025 08:45:46 +0300 Subject: [PATCH 07/20] 11 --- .../example/nto/controller/EmployeeController.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/main/java/com/example/nto/controller/EmployeeController.java b/src/main/java/com/example/nto/controller/EmployeeController.java index 55ac770..2d8e168 100644 --- a/src/main/java/com/example/nto/controller/EmployeeController.java +++ b/src/main/java/com/example/nto/controller/EmployeeController.java @@ -22,6 +22,18 @@ import java.util.stream.Collectors; public class EmployeeController { @Autowired private EmployeeService employeeService; + @GetMapping("/{code}/auth") + public ResponseEntity checkAuth( + @PathVariable String code) { + try { + employeeService.getEmployeeByCode(code); + return ResponseEntity.ok().build(); + } catch (IllegalArgumentException e) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST).build(); + } catch (EmployeeNotFoundException e) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).build(); + } + } @GetMapping("/{code}/info") public ResponseEntity getEmployeeInfo(@PathVariable String code) { try { -- 2.34.1 From 662b109709ed35b5191d87ed721727c546d8ba17 Mon Sep 17 00:00:00 2001 From: Bhumi Shah Date: Sat, 6 Dec 2025 08:53:56 +0300 Subject: [PATCH 08/20] 112 --- .../java/com/example/nto/controller/EmployeeController.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/example/nto/controller/EmployeeController.java b/src/main/java/com/example/nto/controller/EmployeeController.java index 2d8e168..8875fa3 100644 --- a/src/main/java/com/example/nto/controller/EmployeeController.java +++ b/src/main/java/com/example/nto/controller/EmployeeController.java @@ -23,8 +23,11 @@ public class EmployeeController { @Autowired private EmployeeService employeeService; @GetMapping("/{code}/auth") - public ResponseEntity checkAuth( + public ResponseEntity checkAuth( @PathVariable String code) { + if (code==null){ + throw new IllegalArgumentException("Employee code is required"); + } try { employeeService.getEmployeeByCode(code); return ResponseEntity.ok().build(); -- 2.34.1 From f83af983971b15d8d7213968ff6d513c6d6bb063 Mon Sep 17 00:00:00 2001 From: Bhumi Shah Date: Sat, 6 Dec 2025 16:22:52 +0300 Subject: [PATCH 09/20] 12 --- .../nto/service/impl/BookingServiceImpl.java | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) 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 3860ab6..154b3db 100644 --- a/src/main/java/com/example/nto/service/impl/BookingServiceImpl.java +++ b/src/main/java/com/example/nto/service/impl/BookingServiceImpl.java @@ -5,6 +5,7 @@ import com.example.nto.entity.Employee; import com.example.nto.entity.Place; import com.example.nto.excepation.EmployeeNotFoundException; import com.example.nto.repository.BookingRepository; +import com.example.nto.repository.EmployeeRepository; import com.example.nto.repository.PlaceRepository; import com.example.nto.service.BookingService; import com.example.nto.service.EmployeeService; @@ -25,6 +26,8 @@ public class BookingServiceImpl implements BookingService { @Autowired private EmployeeService employeeService; + @Autowired + private EmployeeRepository employeeRepository; @Override public Map findAvailableBookings(String employeeCode) { @@ -57,7 +60,49 @@ public class BookingServiceImpl implements BookingService { } return Collections.unmodifiableMap(result); } + public Map 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((Map) employee); + booking.setPlace(place); + booking.setDate(bookingDate); + bookingRepository.save(booking); + Map result = new LinkedHashMap<>(); + result.put("id", booking.getId()); + result.put("date", booking.getDate()); + result.put("placeId", place.getId()); + result.put("employeeId", employee.getId()); + + return result; + } } + -- 2.34.1 From 4814b6b1c1a59140e51c302708d52d246336e26d Mon Sep 17 00:00:00 2001 From: Bhumi Shah Date: Sat, 6 Dec 2025 16:57:51 +0300 Subject: [PATCH 10/20] 124 --- .../java/com/example/nto/controller/BookingController.java | 4 ++-- .../java/com/example/nto/controller/EmployeeController.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/example/nto/controller/BookingController.java b/src/main/java/com/example/nto/controller/BookingController.java index 61014f3..8bf8a8e 100644 --- a/src/main/java/com/example/nto/controller/BookingController.java +++ b/src/main/java/com/example/nto/controller/BookingController.java @@ -52,8 +52,8 @@ public class BookingController { @PostMapping("/{code}/book") public ResponseEntity createBooking( @PathVariable String code, - @RequestParam Long placeId, - @RequestParam String date + @RequestBody Long placeId, + @RequestBody String date ) { try { if (placeId == null || date == null || date.isEmpty()) { diff --git a/src/main/java/com/example/nto/controller/EmployeeController.java b/src/main/java/com/example/nto/controller/EmployeeController.java index 8875fa3..a976a6c 100644 --- a/src/main/java/com/example/nto/controller/EmployeeController.java +++ b/src/main/java/com/example/nto/controller/EmployeeController.java @@ -23,14 +23,14 @@ public class EmployeeController { @Autowired private EmployeeService employeeService; @GetMapping("/{code}/auth") - public ResponseEntity checkAuth( + public ResponseEntity checkAuth( @PathVariable String code) { if (code==null){ throw new IllegalArgumentException("Employee code is required"); } try { employeeService.getEmployeeByCode(code); - return ResponseEntity.ok().build(); + return ResponseEntity.ok("Authorized Employee"); } catch (IllegalArgumentException e) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).build(); } catch (EmployeeNotFoundException e) { -- 2.34.1 From 2196f7e418e5a39e2b2e55c96c9bc411f4e38cb9 Mon Sep 17 00:00:00 2001 From: Bhumi Shah Date: Sat, 6 Dec 2025 17:01:14 +0300 Subject: [PATCH 11/20] 14 --- .../java/com/example/nto/controller/BookingController.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/example/nto/controller/BookingController.java b/src/main/java/com/example/nto/controller/BookingController.java index 61014f3..8bf8a8e 100644 --- a/src/main/java/com/example/nto/controller/BookingController.java +++ b/src/main/java/com/example/nto/controller/BookingController.java @@ -52,8 +52,8 @@ public class BookingController { @PostMapping("/{code}/book") public ResponseEntity createBooking( @PathVariable String code, - @RequestParam Long placeId, - @RequestParam String date + @RequestBody Long placeId, + @RequestBody String date ) { try { if (placeId == null || date == null || date.isEmpty()) { -- 2.34.1 From 0cf5cfdf59b0a3bd9ae5e268e7caa8eeabf4352e Mon Sep 17 00:00:00 2001 From: Bhumi Shah Date: Sat, 6 Dec 2025 17:13:28 +0300 Subject: [PATCH 12/20] =?UTF-8?q?=D1=83=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../example/nto/controller/BookingController.java | 12 +++++++++--- src/main/java/com/example/nto/entity/Booking.java | 3 +-- .../example/nto/service/impl/BookingServiceImpl.java | 2 +- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/example/nto/controller/BookingController.java b/src/main/java/com/example/nto/controller/BookingController.java index 8bf8a8e..7a092de 100644 --- a/src/main/java/com/example/nto/controller/BookingController.java +++ b/src/main/java/com/example/nto/controller/BookingController.java @@ -52,10 +52,16 @@ public class BookingController { @PostMapping("/{code}/book") public ResponseEntity createBooking( @PathVariable String code, - @RequestBody Long placeId, - @RequestBody String date + @RequestBody Map body ) { 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()) { return ResponseEntity.status(HttpStatus.BAD_REQUEST) .body("Place ID and date must be provided"); @@ -85,7 +91,7 @@ public class BookingController { .body("Place already booked for this date"); } Booking booking = new Booking(); - booking.setEmployee((Map) employee); + booking.setEmployee(employee); booking.setPlace(place); booking.setDate(bookingDate); bookingRepository.save(booking); diff --git a/src/main/java/com/example/nto/entity/Booking.java b/src/main/java/com/example/nto/entity/Booking.java index c7b37eb..b685e8b 100644 --- a/src/main/java/com/example/nto/entity/Booking.java +++ b/src/main/java/com/example/nto/entity/Booking.java @@ -7,7 +7,6 @@ import lombok.Data; import lombok.NoArgsConstructor; import java.time.LocalDate; -import java.util.Map; /** @@ -58,7 +57,7 @@ public class Booking { this.place = place; } - public void setEmployee(Map employee) { + public void setEmployee(Employee employee) { this.employee = (Employee) employee; } } 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 154b3db..5471c21 100644 --- a/src/main/java/com/example/nto/service/impl/BookingServiceImpl.java +++ b/src/main/java/com/example/nto/service/impl/BookingServiceImpl.java @@ -89,7 +89,7 @@ public class BookingServiceImpl implements BookingService { throw new IllegalArgumentException("Place already booked for this date"); } Booking booking = new Booking(); - booking.setEmployee((Map) employee); + booking.setEmployee((Employee) employee); booking.setPlace(place); booking.setDate(bookingDate); bookingRepository.save(booking); -- 2.34.1 From 18b3e950c1f74f99f563e7366c3370f0de264328 Mon Sep 17 00:00:00 2001 From: Bhumi Shah Date: Sat, 6 Dec 2025 17:14:50 +0300 Subject: [PATCH 13/20] =?UTF-8?q?=D1=83=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/example/nto/entity/Booking.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/main/java/com/example/nto/entity/Booking.java b/src/main/java/com/example/nto/entity/Booking.java index b685e8b..d9f24eb 100644 --- a/src/main/java/com/example/nto/entity/Booking.java +++ b/src/main/java/com/example/nto/entity/Booking.java @@ -57,7 +57,4 @@ public class Booking { this.place = place; } - public void setEmployee(Employee employee) { - this.employee = (Employee) employee; - } } -- 2.34.1 From 5d2d30992efba19a67d3c235310eac4bbdf9742c Mon Sep 17 00:00:00 2001 From: Bhumi Shah Date: Sat, 6 Dec 2025 17:15:20 +0300 Subject: [PATCH 14/20] =?UTF-8?q?=D1=83=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/example/nto/entity/Booking.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/example/nto/entity/Booking.java b/src/main/java/com/example/nto/entity/Booking.java index d9f24eb..8a6f0cd 100644 --- a/src/main/java/com/example/nto/entity/Booking.java +++ b/src/main/java/com/example/nto/entity/Booking.java @@ -56,5 +56,7 @@ public class Booking { public void setPlace(Place place) { this.place = place; } - + public void setEmployee(Employee employee) { + this.employee = (Employee) employee; + } } -- 2.34.1 From d836b0bfe239d4c3b64812f17daecbb47072458f Mon Sep 17 00:00:00 2001 From: Bhumi Shah Date: Sun, 7 Dec 2025 00:48:14 +0300 Subject: [PATCH 15/20] 1234 --- .../nto/controller/BookingController.java | 64 ++++--------------- .../nto/repository/BookingRepository.java | 2 +- .../example/nto/service/BookingService.java | 1 + .../com/example/nto/service/PlaceServise.java | 8 +-- .../nto/service/impl/BookingServiceImpl.java | 26 ++------ .../nto/service/impl/EmployeeServiceImpl.java | 7 +- 6 files changed, 23 insertions(+), 85 deletions(-) diff --git a/src/main/java/com/example/nto/controller/BookingController.java b/src/main/java/com/example/nto/controller/BookingController.java index 7a092de..d89fed4 100644 --- a/src/main/java/com/example/nto/controller/BookingController.java +++ b/src/main/java/com/example/nto/controller/BookingController.java @@ -1,8 +1,5 @@ package com.example.nto.controller; -import com.example.nto.entity.Booking; -import com.example.nto.entity.Employee; -import com.example.nto.entity.Place; import com.example.nto.excepation.EmployeeNotFoundException; import com.example.nto.repository.BookingRepository; import com.example.nto.repository.PlaceRepository; @@ -12,9 +9,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; - -import java.time.LocalDate; -import java.util.LinkedHashMap; import java.util.Map; /** @@ -34,10 +28,12 @@ public class BookingController { private BookingRepository bookingRepository; @Autowired private EmployeeService employeeService; - @GetMapping("/{code}/booking") public ResponseEntity getAvailableBookings(@PathVariable String code) { try { + if (code == null || code.trim().isEmpty()) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Employee code cannot be null or empty"); + } Map response = bookingService.findAvailableBookings(code); return ResponseEntity.ok(response); } catch (IllegalArgumentException e) { @@ -48,61 +44,23 @@ public class BookingController { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Unexpected error"); } } - @PostMapping("/{code}/book") public ResponseEntity createBooking( @PathVariable String code, @RequestBody Map body ) { 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; + 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()) { - return ResponseEntity.status(HttpStatus.BAD_REQUEST) - .body("Place ID and date must be provided"); + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Place ID and date must be provided"); } - Employee employee = employeeService.getEmployeeByCode(code); - if (employee == null) { - return ResponseEntity.status(HttpStatus.UNAUTHORIZED) - .body("Employee not found"); - } - Place place = placeRepository.findById(placeId).orElse(null); - if (place == null) { - return ResponseEntity.status(HttpStatus.BAD_REQUEST) - .body("Place not found"); - } - LocalDate bookingDate; - try { - bookingDate = LocalDate.parse(date); - } catch (Exception ex) { - return ResponseEntity.status(HttpStatus.BAD_REQUEST) - .body("Invalid date format. Use yyyy-MM-dd"); - } - boolean exists = bookingRepository.findByDateBetween(bookingDate, bookingDate) - .stream() - .anyMatch(b -> b.getPlace().getId() == placeId); - if (exists) { - return ResponseEntity.status(HttpStatus.CONFLICT) - .body("Place already booked for this date"); - } - Booking booking = new Booking(); - booking.setEmployee(employee); - booking.setPlace(place); - booking.setDate(bookingDate); - bookingRepository.save(booking); - Map 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) { + String message = bookingService.createBooking(code, placeId, date); + return ResponseEntity.ok(message); + } catch (IllegalArgumentException e) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage()); + } catch (EmployeeNotFoundException e) { + return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(e.getMessage()); } catch (Exception e) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Unexpected error"); } diff --git a/src/main/java/com/example/nto/repository/BookingRepository.java b/src/main/java/com/example/nto/repository/BookingRepository.java index 0061eca..7f13184 100644 --- a/src/main/java/com/example/nto/repository/BookingRepository.java +++ b/src/main/java/com/example/nto/repository/BookingRepository.java @@ -1,7 +1,6 @@ package com.example.nto.repository; import com.example.nto.entity.Booking; -import com.example.nto.entity.Employee; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @@ -17,4 +16,5 @@ import java.util.List; @Repository public interface BookingRepository extends JpaRepository { List findByDateBetween(LocalDate start, LocalDate end); + List findByEmployeeId(long id); } diff --git a/src/main/java/com/example/nto/service/BookingService.java b/src/main/java/com/example/nto/service/BookingService.java index c1c7d39..1d144b9 100644 --- a/src/main/java/com/example/nto/service/BookingService.java +++ b/src/main/java/com/example/nto/service/BookingService.java @@ -6,4 +6,5 @@ import java.util.Map; @Service public interface BookingService { Map findAvailableBookings(String employeeCode); + String createBooking(String code, Long placeId, String date); } diff --git a/src/main/java/com/example/nto/service/PlaceServise.java b/src/main/java/com/example/nto/service/PlaceServise.java index 782f312..53771b0 100644 --- a/src/main/java/com/example/nto/service/PlaceServise.java +++ b/src/main/java/com/example/nto/service/PlaceServise.java @@ -2,15 +2,13 @@ package com.example.nto.service; import com.example.nto.entity.Place; import com.example.nto.repository.PlaceRepository; import jakarta.annotation.PostConstruct; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class PlaceServise { - private final PlaceRepository placeRepository; - public PlaceServise(PlaceRepository placeRepository) { - this.placeRepository = placeRepository; - } - + @Autowired + private PlaceRepository placeRepository; @PostConstruct public void Places() { if(placeRepository.count() == 0) { 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 5471c21..ae7a47d 100644 --- a/src/main/java/com/example/nto/service/impl/BookingServiceImpl.java +++ b/src/main/java/com/example/nto/service/impl/BookingServiceImpl.java @@ -17,18 +17,14 @@ import java.util.*; @Service public class BookingServiceImpl implements BookingService { - @Autowired private BookingRepository bookingRepository; - @Autowired private PlaceRepository placeRepository; - @Autowired private EmployeeService employeeService; @Autowired private EmployeeRepository employeeRepository; - @Override public Map findAvailableBookings(String employeeCode) { if (employeeCode == null || employeeCode.isEmpty()) { @@ -47,8 +43,7 @@ public class BookingServiceImpl implements BookingService { LocalDate date = today.plusDays(i); List> freePlaces = new ArrayList<>(); for (Place place : places) { - boolean isBooked = bookings.stream() - .anyMatch(b -> b.getDate().isEqual(date) && b.getPlace().getId() == place.getId()); + boolean isBooked = bookings.stream().anyMatch(b -> b.getDate().isEqual(date) && b.getPlace().getId() == place.getId()); if (!isBooked) { Map placeInfo = new LinkedHashMap<>(); placeInfo.put("id", place.getId()); @@ -60,31 +55,23 @@ public class BookingServiceImpl implements BookingService { } return Collections.unmodifiableMap(result); } - public Map createBooking(String code, Long placeId, String date) { + 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")); - + 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); + boolean exists = bookingRepository.findByDateBetween(bookingDate, bookingDate).stream().anyMatch(b -> b.getPlace().getId() == placeId); if (exists) { throw new IllegalArgumentException("Place already booked for this date"); } @@ -98,8 +85,7 @@ public class BookingServiceImpl implements BookingService { result.put("date", booking.getDate()); result.put("placeId", place.getId()); result.put("employeeId", employee.getId()); - - return result; + return "the booking was created successfully"; } } 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 3fc0dce..00a30f9 100644 --- a/src/main/java/com/example/nto/service/impl/EmployeeServiceImpl.java +++ b/src/main/java/com/example/nto/service/impl/EmployeeServiceImpl.java @@ -1,13 +1,10 @@ package com.example.nto.service.impl; import com.example.nto.entity.Employee; import com.example.nto.excepation.EmployeeNotFoundException; -import com.example.nto.repository.BookingRepository; import com.example.nto.repository.EmployeeRepository; import com.example.nto.service.EmployeeService; import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; @@ -22,13 +19,11 @@ import org.springframework.stereotype.Service; public class EmployeeServiceImpl implements EmployeeService { @Autowired private EmployeeRepository employeeRepository; - @Override public Employee getEmployeeByCode(String code) { if (code == null || code.isEmpty()) { throw new IllegalArgumentException("Employee code is required"); } - return employeeRepository.findByCode(code) - .orElseThrow(() -> new EmployeeNotFoundException("Employee not found")); + return employeeRepository.findByCode(code).orElseThrow(() -> new EmployeeNotFoundException("Employee not found")); } } \ No newline at end of file -- 2.34.1 From 65d3dd56cd53d04839a96c8648f2968967df92a0 Mon Sep 17 00:00:00 2001 From: Bhumi Shah Date: Sun, 7 Dec 2025 00:48:21 +0300 Subject: [PATCH 16/20] 1234 --- .../nto/controller/EmployeeController.java | 31 ++++++++++--------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/example/nto/controller/EmployeeController.java b/src/main/java/com/example/nto/controller/EmployeeController.java index a976a6c..be274b2 100644 --- a/src/main/java/com/example/nto/controller/EmployeeController.java +++ b/src/main/java/com/example/nto/controller/EmployeeController.java @@ -1,6 +1,8 @@ package com.example.nto.controller; +import com.example.nto.entity.Booking; import com.example.nto.entity.Employee; import com.example.nto.excepation.EmployeeNotFoundException; +import com.example.nto.repository.BookingRepository; import com.example.nto.service.EmployeeService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; @@ -8,6 +10,7 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.util.LinkedHashMap; +import java.util.List; import java.util.Map; import java.util.stream.Collectors; @@ -22,10 +25,13 @@ import java.util.stream.Collectors; public class EmployeeController { @Autowired private EmployeeService employeeService; + @Autowired + private BookingRepository bookingRepository; + @GetMapping("/{code}/auth") - public ResponseEntity checkAuth( - @PathVariable String code) { - if (code==null){ + public ResponseEntity checkAuth( + @PathVariable String code) { + if (code == null) { throw new IllegalArgumentException("Employee code is required"); } try { @@ -40,20 +46,16 @@ public class EmployeeController { @GetMapping("/{code}/info") public ResponseEntity getEmployeeInfo(@PathVariable String code) { try { + 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 bookings = bookingRepository.findByEmployeeId(employee.getId()); + Map>> 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 response = new LinkedHashMap<>(); response.put("name", employee.getName()); response.put("photoUrl", employee.getPhotoUrl()); - Map bookingMap = employee.getBookings().stream() - .collect(Collectors.toMap( - b -> b.getDate().toString(), - b -> Map.of( - "id", b.getId(), - "place", b.getPlace().getPlace() - ), - (oldValue, newValue) -> newValue, - LinkedHashMap::new - )); response.put("booking", bookingMap); return ResponseEntity.ok(response); } catch (IllegalArgumentException e) { @@ -61,7 +63,8 @@ public class EmployeeController { } catch (EmployeeNotFoundException e) { return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(e.getMessage()); } catch (Exception e) { - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Unexpected error"); + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) + .body("Unexpected error"); } } } \ No newline at end of file -- 2.34.1 From 46068ca46dbb33bb025d9eca31e458ab2a259844 Mon Sep 17 00:00:00 2001 From: Bhumi Shah Date: Sun, 7 Dec 2025 12:19:53 +0300 Subject: [PATCH 17/20] delet --- .../java/com/example/nto/controller/BookingController.java | 5 +++++ .../example/nto/excepation/EmployeeNotFoundException.java | 4 +--- .../java/com/example/nto/repository/EmployeeRepository.java | 4 +--- src/main/java/com/example/nto/service/BookingService.java | 1 + .../com/example/nto/service/impl/BookingServiceImpl.java | 6 ++++++ 5 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/example/nto/controller/BookingController.java b/src/main/java/com/example/nto/controller/BookingController.java index d89fed4..9b4dd45 100644 --- a/src/main/java/com/example/nto/controller/BookingController.java +++ b/src/main/java/com/example/nto/controller/BookingController.java @@ -65,6 +65,11 @@ public class BookingController { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Unexpected error"); } } + @DeleteMapping("/{id}") + public ResponseEntity deleteBooking(@PathVariable Long id) { + bookingService.deleteBooking(id); + return ResponseEntity.noContent().build(); + } } diff --git a/src/main/java/com/example/nto/excepation/EmployeeNotFoundException.java b/src/main/java/com/example/nto/excepation/EmployeeNotFoundException.java index c59e73a..aa58269 100644 --- a/src/main/java/com/example/nto/excepation/EmployeeNotFoundException.java +++ b/src/main/java/com/example/nto/excepation/EmployeeNotFoundException.java @@ -1,7 +1,5 @@ package com.example.nto.excepation; public class EmployeeNotFoundException extends RuntimeException{ - public EmployeeNotFoundException(String msg) { - super(msg); - } + public EmployeeNotFoundException(String message) {super(message);} } diff --git a/src/main/java/com/example/nto/repository/EmployeeRepository.java b/src/main/java/com/example/nto/repository/EmployeeRepository.java index 212c682..38a3825 100644 --- a/src/main/java/com/example/nto/repository/EmployeeRepository.java +++ b/src/main/java/com/example/nto/repository/EmployeeRepository.java @@ -13,6 +13,4 @@ import java.util.Optional; * НЕЛЬЗЯ: Изменять название класса и пакета */ @Repository -public interface EmployeeRepository extends JpaRepository { - Optional findByCode(String code); -} \ No newline at end of file +public interface EmployeeRepository extends JpaRepository {Optional findByCode(String code);} \ No newline at end of file diff --git a/src/main/java/com/example/nto/service/BookingService.java b/src/main/java/com/example/nto/service/BookingService.java index 1d144b9..cdaf573 100644 --- a/src/main/java/com/example/nto/service/BookingService.java +++ b/src/main/java/com/example/nto/service/BookingService.java @@ -7,4 +7,5 @@ import java.util.Map; public interface BookingService { Map findAvailableBookings(String employeeCode); String createBooking(String code, Long placeId, String date); + void deleteBooking(long id); } 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 ae7a47d..69da11a 100644 --- a/src/main/java/com/example/nto/service/impl/BookingServiceImpl.java +++ b/src/main/java/com/example/nto/service/impl/BookingServiceImpl.java @@ -87,6 +87,12 @@ public class BookingServiceImpl implements BookingService { 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); + } } -- 2.34.1 From 3f1c5dbca8f074a009e2f31e94bb158f086ebadc Mon Sep 17 00:00:00 2001 From: Bhumi Shah Date: Sun, 7 Dec 2025 18:51:18 +0300 Subject: [PATCH 18/20] delet1 --- .../com/example/nto/controller/EmployeeController.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/example/nto/controller/EmployeeController.java b/src/main/java/com/example/nto/controller/EmployeeController.java index be274b2..78bf916 100644 --- a/src/main/java/com/example/nto/controller/EmployeeController.java +++ b/src/main/java/com/example/nto/controller/EmployeeController.java @@ -52,7 +52,13 @@ public class EmployeeController { } Employee employee = employeeService.getEmployeeByCode(code); List bookings = bookingRepository.findByEmployeeId(employee.getId()); - Map>> 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> bookingMap = new LinkedHashMap<>(); + + for (Booking b : bookings) { + bookingMap.put( + b.getDate().toString(), + Map.of("id", b.getId(), "place", b.getPlace().getPlace())); + } Map response = new LinkedHashMap<>(); response.put("name", employee.getName()); response.put("photoUrl", employee.getPhotoUrl()); @@ -67,4 +73,5 @@ public class EmployeeController { .body("Unexpected error"); } } + } \ No newline at end of file -- 2.34.1 From 3c3ef45d78a510d2a85419f4696394719f66f6ed Mon Sep 17 00:00:00 2001 From: Bhumi Shah Date: Mon, 8 Dec 2025 18:34:44 +0300 Subject: [PATCH 19/20] delet12 --- .../example/nto/controller/EmployeeController.java | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/example/nto/controller/EmployeeController.java b/src/main/java/com/example/nto/controller/EmployeeController.java index 78bf916..a94aacb 100644 --- a/src/main/java/com/example/nto/controller/EmployeeController.java +++ b/src/main/java/com/example/nto/controller/EmployeeController.java @@ -52,13 +52,7 @@ public class EmployeeController { } Employee employee = employeeService.getEmployeeByCode(code); List bookings = bookingRepository.findByEmployeeId(employee.getId()); - Map> bookingMap = new LinkedHashMap<>(); - - for (Booking b : bookings) { - bookingMap.put( - b.getDate().toString(), - Map.of("id", b.getId(), "place", b.getPlace().getPlace())); - } + Map>> 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 response = new LinkedHashMap<>(); response.put("name", employee.getName()); response.put("photoUrl", employee.getPhotoUrl()); @@ -73,5 +67,4 @@ public class EmployeeController { .body("Unexpected error"); } } - -} \ No newline at end of file +} -- 2.34.1 From c43e4b18fb5e27755d397ec110cd06114750e7be Mon Sep 17 00:00:00 2001 From: Bhumi Shah Date: Mon, 8 Dec 2025 18:36:42 +0300 Subject: [PATCH 20/20] delet123 --- src/main/java/com/example/nto/entity/Employee.java | 1 - .../com/example/nto/excepation/EmployeeNotFoundException.java | 4 +++- .../java/com/example/nto/repository/EmployeeRepository.java | 4 +++- .../java/com/example/nto/service/impl/BookingServiceImpl.java | 4 +++- 4 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/example/nto/entity/Employee.java b/src/main/java/com/example/nto/entity/Employee.java index 9298591..19e13dc 100644 --- a/src/main/java/com/example/nto/entity/Employee.java +++ b/src/main/java/com/example/nto/entity/Employee.java @@ -41,5 +41,4 @@ public class Employee { public String getName() { return name; } public String getCode() { return code; } public String getPhotoUrl() { return photoUrl; } - public List getBookings() { return bookings; } } diff --git a/src/main/java/com/example/nto/excepation/EmployeeNotFoundException.java b/src/main/java/com/example/nto/excepation/EmployeeNotFoundException.java index aa58269..7de98f7 100644 --- a/src/main/java/com/example/nto/excepation/EmployeeNotFoundException.java +++ b/src/main/java/com/example/nto/excepation/EmployeeNotFoundException.java @@ -1,5 +1,7 @@ package com.example.nto.excepation; public class EmployeeNotFoundException extends RuntimeException{ - public EmployeeNotFoundException(String message) {super(message);} + public EmployeeNotFoundException(String message) { + super(message); + } } diff --git a/src/main/java/com/example/nto/repository/EmployeeRepository.java b/src/main/java/com/example/nto/repository/EmployeeRepository.java index 38a3825..212c682 100644 --- a/src/main/java/com/example/nto/repository/EmployeeRepository.java +++ b/src/main/java/com/example/nto/repository/EmployeeRepository.java @@ -13,4 +13,6 @@ import java.util.Optional; * НЕЛЬЗЯ: Изменять название класса и пакета */ @Repository -public interface EmployeeRepository extends JpaRepository {Optional findByCode(String code);} \ No newline at end of file +public interface EmployeeRepository extends JpaRepository { + Optional findByCode(String code); +} \ No newline at end of file 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 69da11a..cb07759 100644 --- a/src/main/java/com/example/nto/service/impl/BookingServiceImpl.java +++ b/src/main/java/com/example/nto/service/impl/BookingServiceImpl.java @@ -71,7 +71,9 @@ public class BookingServiceImpl implements BookingService { } 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); + boolean exists = bookingRepository.findByDateBetween(bookingDate, bookingDate).stream().anyMatch( + b -> b.getPlace() + .getId() == placeId); if (exists) { throw new IllegalArgumentException("Place already booked for this date"); } -- 2.34.1