From 8205be1f3f804d8dda1c44be402c07501e83a8c0 Mon Sep 17 00:00:00 2001 From: lynxwq2 Date: Sun, 23 Nov 2025 16:25:12 +0300 Subject: [PATCH 01/25] test --- src/main/java/com/example/nto/App.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/com/example/nto/App.java b/src/main/java/com/example/nto/App.java index e453f89..d9d071b 100644 --- a/src/main/java/com/example/nto/App.java +++ b/src/main/java/com/example/nto/App.java @@ -8,5 +8,6 @@ package com.example.nto; */ public class App { public static void main(String[] args) { + System.out.println("Hello"); } } -- 2.34.1 From 404aead9ecc69b98dd0f90802b59f72c50b8a6af Mon Sep 17 00:00:00 2001 From: lynxwq2 Date: Mon, 24 Nov 2025 17:17:19 +0300 Subject: [PATCH 02/25] =?UTF-8?q?+=D0=B7=D0=B0=D0=BF=D1=83=D1=81=D0=BA=20?= =?UTF-8?q?=D0=BF=D1=80=D0=B8=D0=BB=D0=BE=D0=B6=D0=B5=D0=BD=D0=B8=D1=8F=20?= =?UTF-8?q?=D0=BD=D0=B0=D1=87=D0=B0=D0=BB=D0=BE=20=D1=80=D0=B5=D0=B0=D0=BB?= =?UTF-8?q?=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D0=B8=20=D0=B1=D1=80=D0=BE=D0=BD?= =?UTF-8?q?=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/example/nto/App.java | 6 +++++- .../com/example/nto/controller/BookingController.java | 9 +++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/example/nto/App.java b/src/main/java/com/example/nto/App.java index d9d071b..e5435c1 100644 --- a/src/main/java/com/example/nto/App.java +++ b/src/main/java/com/example/nto/App.java @@ -1,13 +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) { - System.out.println("Hello"); + SpringApplication.run(App.class, args); } } diff --git a/src/main/java/com/example/nto/controller/BookingController.java b/src/main/java/com/example/nto/controller/BookingController.java index 9885f84..71dd7f3 100644 --- a/src/main/java/com/example/nto/controller/BookingController.java +++ b/src/main/java/com/example/nto/controller/BookingController.java @@ -1,10 +1,19 @@ package com.example.nto.controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + /** * TODO: ДОРАБОТАТЬ в рамках задания * ================================= * МОЖНО: Добавлять методы, аннотации, зависимости * НЕЛЬЗЯ: Изменять название класса и пакета */ +@RestController public class BookingController { + @GetMapping("/booking") + public String booking(@RequestParam(defaultValue = "")String id){ + return "booking session started " + id; + } } -- 2.34.1 From 0ea199cd6bc879792f409009daaf3f4da331538b Mon Sep 17 00:00:00 2001 From: lynxwq2 Date: Tue, 25 Nov 2025 20:37:10 +0300 Subject: [PATCH 03/25] +beginning auth --- .../example/nto/controller/EmployeeController.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/main/java/com/example/nto/controller/EmployeeController.java b/src/main/java/com/example/nto/controller/EmployeeController.java index 47658f9..c26177b 100644 --- a/src/main/java/com/example/nto/controller/EmployeeController.java +++ b/src/main/java/com/example/nto/controller/EmployeeController.java @@ -1,10 +1,23 @@ package com.example.nto.controller; +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.net.http.HttpResponse; + /** * TODO: ДОРАБОТАТЬ в рамках задания * ================================= * МОЖНО: Добавлять методы, аннотации, зависимости * НЕЛЬЗЯ: Изменять название класса и пакета */ +@RestController +@RequestMapping("code") public class EmployeeController { + @GetMapping("/auth/{id}") + public int isAuth(@PathVariable int id){ + return id; + } } -- 2.34.1 From 5295d49a44139d90dc46bad6089740f69d6b5156 Mon Sep 17 00:00:00 2001 From: lynxwq2 Date: Wed, 26 Nov 2025 16:04:30 +0300 Subject: [PATCH 04/25] =?UTF-8?q?=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7?= =?UTF-8?q?=D0=B0=D1=86=D0=B8=D1=8F=20get,=20=D0=BD=D0=BE=20=D0=BF=D0=BE?= =?UTF-8?q?=D0=BA=D0=B0=20=D0=B5=D1=89=D1=91=20=D1=81=20=D0=BE=D1=88=D0=B8?= =?UTF-8?q?=D0=B1=D0=BA=D0=B0=D0=BC=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nto/controller/EmployeeController.java | 22 ++++++++++++++---- .../java/com/example/nto/entity/Employee.java | 21 +++++++++++++++++ .../nto/repository/EmployeeRepository.java | 12 +++++++++- .../example/nto/service/EmployeeService.java | 7 ++++++ .../nto/service/impl/EmployeeServiceImpl.java | 23 +++++++++++++++++++ 5 files changed, 80 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/example/nto/controller/EmployeeController.java b/src/main/java/com/example/nto/controller/EmployeeController.java index c26177b..4f4e972 100644 --- a/src/main/java/com/example/nto/controller/EmployeeController.java +++ b/src/main/java/com/example/nto/controller/EmployeeController.java @@ -1,11 +1,12 @@ package com.example.nto.controller; -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 com.example.nto.entity.Employee; +import com.example.nto.service.EmployeeService; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.*; import java.net.http.HttpResponse; +import java.util.List; /** * TODO: ДОРАБОТАТЬ в рамках задания @@ -20,4 +21,17 @@ public class EmployeeController { public int isAuth(@PathVariable int id){ return id; } + + private final EmployeeService employeeService; + + public EmployeeController(EmployeeService employeeService) { + + this.employeeService = employeeService; + } + + @GetMapping + @ResponseStatus(code = HttpStatus.OK) + public List getAll() { + return employeeService.getAll(); + } } diff --git a/src/main/java/com/example/nto/entity/Employee.java b/src/main/java/com/example/nto/entity/Employee.java index a52102b..da118cf 100644 --- a/src/main/java/com/example/nto/entity/Employee.java +++ b/src/main/java/com/example/nto/entity/Employee.java @@ -31,4 +31,25 @@ public class Employee { @OneToMany(mappedBy = "employee", cascade = CascadeType.ALL, fetch = FetchType.LAZY) private List bookingList; + + public Employee(long id, String name){ + this.id = id; + this.name = name; + } + + public long getId() { + return id; + } + + public String getName() { + return name; + } + + public void setId(long id) { + this.id = id; + } + + public void setName(String name) { + this.name = name; + } } diff --git a/src/main/java/com/example/nto/repository/EmployeeRepository.java b/src/main/java/com/example/nto/repository/EmployeeRepository.java index 210d29c..a3a3865 100644 --- a/src/main/java/com/example/nto/repository/EmployeeRepository.java +++ b/src/main/java/com/example/nto/repository/EmployeeRepository.java @@ -1,10 +1,20 @@ package com.example.nto.repository; +import com.example.nto.entity.Employee; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +import java.util.Optional; + /** * TODO: ДОРАБОТАТЬ в рамках задания * ================================= * МОЖНО: Добавлять методы, аннотации, зависимости * НЕЛЬЗЯ: Изменять название класса и пакета */ -public interface EmployeeRepository { + +@Repository +public interface EmployeeRepository extends JpaRepository { + Optional findById(long id); + } diff --git a/src/main/java/com/example/nto/service/EmployeeService.java b/src/main/java/com/example/nto/service/EmployeeService.java index cccd209..5a0dd91 100644 --- a/src/main/java/com/example/nto/service/EmployeeService.java +++ b/src/main/java/com/example/nto/service/EmployeeService.java @@ -1,5 +1,9 @@ package com.example.nto.service; +import com.example.nto.entity.Employee; + +import java.util.List; + /** * TODO: ДОРАБОТАТЬ в рамках задания * ================================= @@ -7,4 +11,7 @@ package com.example.nto.service; * НЕЛЬЗЯ: Изменять название класса и пакета */ public interface EmployeeService { + List getAll(); + + Employee getById(long id); } 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..14b2bec 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,11 @@ package com.example.nto.service.impl; +import com.example.nto.entity.Employee; +import com.example.nto.repository.EmployeeRepository; import com.example.nto.service.EmployeeService; +import org.springframework.stereotype.Service; + +import java.util.List; /** * TODO: ДОРАБОТАТЬ в рамках задания @@ -8,5 +13,23 @@ import com.example.nto.service.EmployeeService; * МОЖНО: Добавлять методы, аннотации, зависимости * НЕЛЬЗЯ: Изменять название класса и пакета */ +@Service public class EmployeeServiceImpl implements EmployeeService { + private final EmployeeRepository employeeRepository; + + public EmployeeServiceImpl(EmployeeRepository employeeRepository) { + this.employeeRepository = employeeRepository; + } + + @Override + public List getAll() { + + + return employeeRepository.findAll(); + } + + @Override + public Employee getById(long id) { + return employeeRepository.findById(id); + } } -- 2.34.1 From 18172e173186bfcb7397e9070348511935b682b1 Mon Sep 17 00:00:00 2001 From: lynxwq2 Date: Thu, 27 Nov 2025 17:15:08 +0300 Subject: [PATCH 05/25] :( --- .../nto/controller/BookingController.java | 14 +++++++--- .../nto/controller/EmployeeController.java | 15 +++++------ .../java/com/example/nto/entity/Booking.java | 10 ++++--- .../java/com/example/nto/entity/Employee.java | 20 +++----------- .../java/com/example/nto/entity/Place.java | 2 ++ .../nto/repository/BookingRepository.java | 10 ++++++- .../nto/repository/EmployeeRepository.java | 4 ++- .../example/nto/service/BookingService.java | 3 +++ .../example/nto/service/EmployeeService.java | 3 ++- .../nto/service/impl/BookingServiceImpl.java | 26 +++++++++++++++++-- .../nto/service/impl/EmployeeServiceImpl.java | 7 ++--- 11 files changed, 74 insertions(+), 40 deletions(-) diff --git a/src/main/java/com/example/nto/controller/BookingController.java b/src/main/java/com/example/nto/controller/BookingController.java index 71dd7f3..c4b3ccb 100644 --- a/src/main/java/com/example/nto/controller/BookingController.java +++ b/src/main/java/com/example/nto/controller/BookingController.java @@ -1,9 +1,13 @@ package com.example.nto.controller; +import com.example.nto.entity.Booking; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import java.time.LocalDate; + /** * TODO: ДОРАБОТАТЬ в рамках задания * ================================= @@ -11,9 +15,11 @@ import org.springframework.web.bind.annotation.RestController; * НЕЛЬЗЯ: Изменять название класса и пакета */ @RestController +@RequestMapping("code") public class BookingController { - @GetMapping("/booking") - public String booking(@RequestParam(defaultValue = "")String id){ - return "booking session started " + id; - } +// @GetMapping("/booking") +// public Booking booking(@RequestParam(defaultValue = "")String id){ +// return new Booking(id, ); +// } + } diff --git a/src/main/java/com/example/nto/controller/EmployeeController.java b/src/main/java/com/example/nto/controller/EmployeeController.java index 4f4e972..646600c 100644 --- a/src/main/java/com/example/nto/controller/EmployeeController.java +++ b/src/main/java/com/example/nto/controller/EmployeeController.java @@ -17,10 +17,10 @@ import java.util.List; @RestController @RequestMapping("code") public class EmployeeController { - @GetMapping("/auth/{id}") - public int isAuth(@PathVariable int id){ - return id; - } +// @GetMapping("/auth/{id}") +// public int isAuth(@PathVariable int id){ +// return id; +// } private final EmployeeService employeeService; @@ -29,9 +29,8 @@ public class EmployeeController { this.employeeService = employeeService; } - @GetMapping - @ResponseStatus(code = HttpStatus.OK) - public List getAll() { - return employeeService.getAll(); + @GetMapping("/{id}/auth") + public HttpStatus Isauth(@PathVariable long id){ + return HttpStatus.OK; } } diff --git a/src/main/java/com/example/nto/entity/Booking.java b/src/main/java/com/example/nto/entity/Booking.java index 21c1981..a7f38cf 100644 --- a/src/main/java/com/example/nto/entity/Booking.java +++ b/src/main/java/com/example/nto/entity/Booking.java @@ -1,12 +1,11 @@ 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 org.springframework.data.annotation.Id; import java.time.LocalDate; @@ -21,8 +20,9 @@ import java.time.LocalDate; @Builder @NoArgsConstructor @AllArgsConstructor +@Entity public class Booking { - + @Id private long id; private LocalDate date; @@ -31,5 +31,7 @@ public class Booking { @JoinColumn(name = "place_id") private Place place; + @ManyToOne(targetEntity = Employee.class, fetch = FetchType.LAZY) + @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 da118cf..265e27d 100644 --- a/src/main/java/com/example/nto/entity/Employee.java +++ b/src/main/java/com/example/nto/entity/Employee.java @@ -19,8 +19,11 @@ import java.util.List; @Builder @NoArgsConstructor @AllArgsConstructor +@Entity public class Employee { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) private long id; private String name; @@ -32,24 +35,9 @@ public class Employee { @OneToMany(mappedBy = "employee", cascade = CascadeType.ALL, fetch = FetchType.LAZY) private List bookingList; - public Employee(long id, String name){ + public Employee(long id, String name) { this.id = id; this.name = name; } - public long getId() { - return id; - } - - public String getName() { - return name; - } - - public void setId(long id) { - this.id = id; - } - - public void setName(String name) { - this.name = name; - } } diff --git a/src/main/java/com/example/nto/entity/Place.java b/src/main/java/com/example/nto/entity/Place.java index 00c253b..1bbd9bb 100644 --- a/src/main/java/com/example/nto/entity/Place.java +++ b/src/main/java/com/example/nto/entity/Place.java @@ -1,5 +1,6 @@ package com.example.nto.entity; +import jakarta.persistence.Entity; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; @@ -19,6 +20,7 @@ import lombok.NoArgsConstructor; @Builder @NoArgsConstructor @AllArgsConstructor +@Entity public class Place { @Id diff --git a/src/main/java/com/example/nto/repository/BookingRepository.java b/src/main/java/com/example/nto/repository/BookingRepository.java index 303bb54..ca90213 100644 --- a/src/main/java/com/example/nto/repository/BookingRepository.java +++ b/src/main/java/com/example/nto/repository/BookingRepository.java @@ -1,10 +1,18 @@ 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.util.Optional; + /** * TODO: ДОРАБОТАТЬ в рамках задания * ================================= * МОЖНО: Добавлять методы, аннотации, зависимости * НЕЛЬЗЯ: Изменять название класса и пакета */ -public interface BookingRepository { +@Repository +public interface BookingRepository extends JpaRepository { + Optional findByName(String name); } diff --git a/src/main/java/com/example/nto/repository/EmployeeRepository.java b/src/main/java/com/example/nto/repository/EmployeeRepository.java index a3a3865..7eee94f 100644 --- a/src/main/java/com/example/nto/repository/EmployeeRepository.java +++ b/src/main/java/com/example/nto/repository/EmployeeRepository.java @@ -3,6 +3,7 @@ package com.example.nto.repository; import com.example.nto.entity.Employee; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; +import org.springframework.stereotype.Service; import java.util.Optional; @@ -13,8 +14,9 @@ import java.util.Optional; * НЕЛЬЗЯ: Изменять название класса и пакета */ +@Service @Repository public interface EmployeeRepository extends JpaRepository { - Optional findById(long id); + Optional findById(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 31ec148..3c10024 100644 --- a/src/main/java/com/example/nto/service/BookingService.java +++ b/src/main/java/com/example/nto/service/BookingService.java @@ -1,5 +1,7 @@ package com.example.nto.service; +import com.example.nto.entity.Booking; + /** * TODO: ДОРАБОТАТЬ в рамках задания * ================================= @@ -7,4 +9,5 @@ package com.example.nto.service; * НЕЛЬЗЯ: Изменять название класса и пакета */ public interface BookingService { + Booking getBooking(String name); } diff --git a/src/main/java/com/example/nto/service/EmployeeService.java b/src/main/java/com/example/nto/service/EmployeeService.java index 5a0dd91..c0c0a3c 100644 --- a/src/main/java/com/example/nto/service/EmployeeService.java +++ b/src/main/java/com/example/nto/service/EmployeeService.java @@ -3,6 +3,7 @@ package com.example.nto.service; import com.example.nto.entity.Employee; import java.util.List; +import java.util.Optional; /** * TODO: ДОРАБОТАТЬ в рамках задания @@ -13,5 +14,5 @@ import java.util.List; public interface EmployeeService { List getAll(); - Employee getById(long id); + Optional getById(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 d24b244..b3c31cc 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,34 @@ package com.example.nto.service.impl; +import com.example.nto.entity.Booking; +import com.example.nto.repository.BookingRepository; import com.example.nto.service.BookingService; +import java.util.List; + /** * TODO: ДОРАБОТАТЬ в рамках задания * ================================= * МОЖНО: Добавлять методы, аннотации, зависимости * НЕЛЬЗЯ: Изменять название класса и пакета */ -public class BookingServiceImpl implements BookingService { -} +//public class BookingServiceImpl implements BookingService { +// +// +// private final BookingRepository bookingRepository; +// +// public BookingServiceImpl(BookingRepository bookingRepository) { +// this.bookingRepository = bookingRepository; +// } +// +// @Override +// public List getAll() { +// return bookingRepository.findAll(); +// } +// +// @Override +// public Booking getByName(String name) { +// return bookingRepository.findByName(name); +// } + +//} 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 14b2bec..6890a55 100644 --- a/src/main/java/com/example/nto/service/impl/EmployeeServiceImpl.java +++ b/src/main/java/com/example/nto/service/impl/EmployeeServiceImpl.java @@ -3,9 +3,11 @@ package com.example.nto.service.impl; import com.example.nto.entity.Employee; import com.example.nto.repository.EmployeeRepository; import com.example.nto.service.EmployeeService; +import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; import java.util.List; +import java.util.Optional; /** * TODO: ДОРАБОТАТЬ в рамках задания @@ -13,6 +15,7 @@ import java.util.List; * МОЖНО: Добавлять методы, аннотации, зависимости * НЕЛЬЗЯ: Изменять название класса и пакета */ +@Component @Service public class EmployeeServiceImpl implements EmployeeService { private final EmployeeRepository employeeRepository; @@ -23,13 +26,11 @@ public class EmployeeServiceImpl implements EmployeeService { @Override public List getAll() { - - return employeeRepository.findAll(); } @Override - public Employee getById(long id) { + public Optional getById(long id) { return employeeRepository.findById(id); } } -- 2.34.1 From a3a43fe7a0bb39e0d722f9cefd73b731bfc2436a Mon Sep 17 00:00:00 2001 From: lynxwq2 Date: Thu, 27 Nov 2025 18:45:59 +0300 Subject: [PATCH 06/25] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D1=8B=20=D1=84=D0=B0=D0=B9=D0=BB=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/example/nto/entity/Booking.java | 1 - .../nto/repository/BookingRepository.java | 5 ++- .../example/nto/service/BookingService.java | 8 +++- .../nto/service/impl/BookingServiceImpl.java | 42 ++++++++++--------- 4 files changed, 34 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/example/nto/entity/Booking.java b/src/main/java/com/example/nto/entity/Booking.java index a7f38cf..d15b740 100644 --- a/src/main/java/com/example/nto/entity/Booking.java +++ b/src/main/java/com/example/nto/entity/Booking.java @@ -5,7 +5,6 @@ import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; -import org.springframework.data.annotation.Id; import java.time.LocalDate; diff --git a/src/main/java/com/example/nto/repository/BookingRepository.java b/src/main/java/com/example/nto/repository/BookingRepository.java index ca90213..edf53ea 100644 --- a/src/main/java/com/example/nto/repository/BookingRepository.java +++ b/src/main/java/com/example/nto/repository/BookingRepository.java @@ -3,7 +3,9 @@ package com.example.nto.repository; import com.example.nto.entity.Booking; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; +import org.springframework.stereotype.Service; +import java.sql.Date; import java.util.Optional; /** @@ -12,7 +14,8 @@ import java.util.Optional; * МОЖНО: Добавлять методы, аннотации, зависимости * НЕЛЬЗЯ: Изменять название класса и пакета */ +@Service @Repository public interface BookingRepository extends JpaRepository { - Optional findByName(String name); + Optional findById(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 3c10024..c500134 100644 --- a/src/main/java/com/example/nto/service/BookingService.java +++ b/src/main/java/com/example/nto/service/BookingService.java @@ -1,6 +1,10 @@ package com.example.nto.service; import com.example.nto.entity.Booking; +import com.example.nto.entity.Employee; + +import java.util.List; +import java.util.Optional; /** * TODO: ДОРАБОТАТЬ в рамках задания @@ -9,5 +13,7 @@ import com.example.nto.entity.Booking; * НЕЛЬЗЯ: Изменять название класса и пакета */ public interface BookingService { - Booking getBooking(String name); + List getAll(); + Optional getById(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 b3c31cc..a25620f 100644 --- a/src/main/java/com/example/nto/service/impl/BookingServiceImpl.java +++ b/src/main/java/com/example/nto/service/impl/BookingServiceImpl.java @@ -3,8 +3,11 @@ package com.example.nto.service.impl; import com.example.nto.entity.Booking; import com.example.nto.repository.BookingRepository; import com.example.nto.service.BookingService; +import org.springframework.stereotype.Component; +import org.springframework.stereotype.Service; import java.util.List; +import java.util.Optional; /** * TODO: ДОРАБОТАТЬ в рамках задания @@ -12,23 +15,24 @@ import java.util.List; * МОЖНО: Добавлять методы, аннотации, зависимости * НЕЛЬЗЯ: Изменять название класса и пакета */ -//public class BookingServiceImpl implements BookingService { -// -// -// private final BookingRepository bookingRepository; -// -// public BookingServiceImpl(BookingRepository bookingRepository) { -// this.bookingRepository = bookingRepository; -// } -// -// @Override -// public List getAll() { -// return bookingRepository.findAll(); -// } -// -// @Override -// public Booking getByName(String name) { -// return bookingRepository.findByName(name); -// } +@Component +@Service +public class BookingServiceImpl implements BookingService { -//} + private final BookingRepository bookingRepository; + + public BookingServiceImpl(BookingRepository bookingRepository) { + this.bookingRepository = bookingRepository; + } + + @Override + public List getAll() { + return bookingRepository.findAll(); + } + + @Override + public Optional getById(long id) { + return bookingRepository.findById(id); + } + +} -- 2.34.1 From 4a22c9d46677b3279645d5f84c5e8a1d14414d7e Mon Sep 17 00:00:00 2001 From: lynxwq2 Date: Thu, 27 Nov 2025 20:43:12 +0300 Subject: [PATCH 07/25] =?UTF-8?q?.../api/{id}/auth=20-=20=D1=80=D0=B0?= =?UTF-8?q?=D0=B1=D0=BE=D1=82=D0=B0=D0=B5=D1=82=20=3D)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nto/controller/EmployeeController.java | 17 +++++++++++------ .../example/nto/service/EmployeeService.java | 2 ++ .../nto/service/impl/EmployeeServiceImpl.java | 6 ++++++ 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/example/nto/controller/EmployeeController.java b/src/main/java/com/example/nto/controller/EmployeeController.java index 646600c..9b8961f 100644 --- a/src/main/java/com/example/nto/controller/EmployeeController.java +++ b/src/main/java/com/example/nto/controller/EmployeeController.java @@ -2,7 +2,10 @@ package com.example.nto.controller; import com.example.nto.entity.Employee; import com.example.nto.service.EmployeeService; +import com.example.nto.service.impl.EmployeeServiceImpl; +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.net.http.HttpResponse; @@ -15,12 +18,8 @@ import java.util.List; * НЕЛЬЗЯ: Изменять название класса и пакета */ @RestController -@RequestMapping("code") +@RequestMapping("api") public class EmployeeController { -// @GetMapping("/auth/{id}") -// public int isAuth(@PathVariable int id){ -// return id; -// } private final EmployeeService employeeService; @@ -31,6 +30,12 @@ public class EmployeeController { @GetMapping("/{id}/auth") public HttpStatus Isauth(@PathVariable long id){ - return HttpStatus.OK; + if(employeeService.isIdValid(id)){ + return HttpStatus.OK; + } + else + return HttpStatus.NOT_FOUND; } + + } diff --git a/src/main/java/com/example/nto/service/EmployeeService.java b/src/main/java/com/example/nto/service/EmployeeService.java index c0c0a3c..1565317 100644 --- a/src/main/java/com/example/nto/service/EmployeeService.java +++ b/src/main/java/com/example/nto/service/EmployeeService.java @@ -15,4 +15,6 @@ public interface EmployeeService { List getAll(); Optional getById(long id); + + boolean isIdValid(long id); } 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 6890a55..e7488fa 100644 --- a/src/main/java/com/example/nto/service/impl/EmployeeServiceImpl.java +++ b/src/main/java/com/example/nto/service/impl/EmployeeServiceImpl.java @@ -3,6 +3,7 @@ package com.example.nto.service.impl; import com.example.nto.entity.Employee; import com.example.nto.repository.EmployeeRepository; import com.example.nto.service.EmployeeService; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; @@ -20,6 +21,7 @@ import java.util.Optional; public class EmployeeServiceImpl implements EmployeeService { private final EmployeeRepository employeeRepository; + @Autowired public EmployeeServiceImpl(EmployeeRepository employeeRepository) { this.employeeRepository = employeeRepository; } @@ -33,4 +35,8 @@ public class EmployeeServiceImpl implements EmployeeService { public Optional getById(long id) { return employeeRepository.findById(id); } + + public boolean isIdValid(long id){ + return employeeRepository.findById(id).isPresent(); + } } -- 2.34.1 From 94a8cddd516153d482954428914703a9ddd212cd Mon Sep 17 00:00:00 2001 From: lynxwq2 Date: Fri, 28 Nov 2025 17:40:53 +0300 Subject: [PATCH 08/25] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=BE=200=5F=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../example/nto/controller/EmployeeController.java | 11 +++++++---- src/main/java/com/example/nto/entity/Employee.java | 4 +++- .../example/nto/repository/EmployeeRepository.java | 5 ++--- .../java/com/example/nto/service/EmployeeService.java | 5 +++-- .../example/nto/service/impl/EmployeeServiceImpl.java | 8 ++++---- 5 files changed, 19 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 9b8961f..1d6d804 100644 --- a/src/main/java/com/example/nto/controller/EmployeeController.java +++ b/src/main/java/com/example/nto/controller/EmployeeController.java @@ -1,8 +1,10 @@ package com.example.nto.controller; import com.example.nto.entity.Employee; +import com.example.nto.repository.EmployeeRepository; import com.example.nto.service.EmployeeService; import com.example.nto.service.impl.EmployeeServiceImpl; +import org.hibernate.resource.beans.container.spi.BeanLifecycleStrategy; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -28,13 +30,14 @@ public class EmployeeController { this.employeeService = employeeService; } - @GetMapping("/{id}/auth") - public HttpStatus Isauth(@PathVariable long id){ - if(employeeService.isIdValid(id)){ + @GetMapping("/{code}/auth") + public HttpStatus Isauth(@PathVariable String code){ + if(employeeService.isCodeValid(code)){ return HttpStatus.OK; } else - return HttpStatus.NOT_FOUND; + return HttpStatus.UNAUTHORIZED; + } diff --git a/src/main/java/com/example/nto/entity/Employee.java b/src/main/java/com/example/nto/entity/Employee.java index 265e27d..387b6b8 100644 --- a/src/main/java/com/example/nto/entity/Employee.java +++ b/src/main/java/com/example/nto/entity/Employee.java @@ -35,9 +35,11 @@ public class Employee { @OneToMany(mappedBy = "employee", cascade = CascadeType.ALL, fetch = FetchType.LAZY) private List bookingList; - public Employee(long id, String name) { + public Employee(long id, String name, String code, String photoUrl) { this.id = id; this.name = name; + this.code = code; + this.photoUrl = photoUrl; } } diff --git a/src/main/java/com/example/nto/repository/EmployeeRepository.java b/src/main/java/com/example/nto/repository/EmployeeRepository.java index 7eee94f..e6ecbcb 100644 --- a/src/main/java/com/example/nto/repository/EmployeeRepository.java +++ b/src/main/java/com/example/nto/repository/EmployeeRepository.java @@ -16,7 +16,6 @@ import java.util.Optional; @Service @Repository -public interface EmployeeRepository extends JpaRepository { - Optional findById(Long id); - +public interface EmployeeRepository extends JpaRepository { + Optional findByCode(String code); } diff --git a/src/main/java/com/example/nto/service/EmployeeService.java b/src/main/java/com/example/nto/service/EmployeeService.java index 1565317..a949b95 100644 --- a/src/main/java/com/example/nto/service/EmployeeService.java +++ b/src/main/java/com/example/nto/service/EmployeeService.java @@ -14,7 +14,8 @@ import java.util.Optional; public interface EmployeeService { List getAll(); - Optional getById(long id); + Optional getByCode(String code); - boolean isIdValid(long id); + + boolean isCodeValid(String code); } 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 e7488fa..66b2764 100644 --- a/src/main/java/com/example/nto/service/impl/EmployeeServiceImpl.java +++ b/src/main/java/com/example/nto/service/impl/EmployeeServiceImpl.java @@ -32,11 +32,11 @@ public class EmployeeServiceImpl implements EmployeeService { } @Override - public Optional getById(long id) { - return employeeRepository.findById(id); + public Optional getByCode(String code) { + return employeeRepository.findByCode(code); } - public boolean isIdValid(long id){ - return employeeRepository.findById(id).isPresent(); + public boolean isCodeValid(String code){ + return employeeRepository.findByCode(code).isPresent(); } } -- 2.34.1 From 06e3294364da159820461c8aabd97276382c622d Mon Sep 17 00:00:00 2001 From: lynxwq2 Date: Fri, 28 Nov 2025 19:30:17 +0300 Subject: [PATCH 09/25] =?UTF-8?q?=D1=82=D0=B5=D0=BA=D1=81=D1=82=D0=BE?= =?UTF-8?q?=D0=B2=D1=8B=D0=B5=20=D0=BE=D1=82=D0=B2=D0=B5=D1=82=D1=8B=20?= =?UTF-8?q?=D0=B2=20.body?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nto/controller/EmployeeController.java | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/example/nto/controller/EmployeeController.java b/src/main/java/com/example/nto/controller/EmployeeController.java index 1d6d804..f5b82f3 100644 --- a/src/main/java/com/example/nto/controller/EmployeeController.java +++ b/src/main/java/com/example/nto/controller/EmployeeController.java @@ -31,14 +31,20 @@ public class EmployeeController { } @GetMapping("/{code}/auth") - public HttpStatus Isauth(@PathVariable String code){ - if(employeeService.isCodeValid(code)){ - return HttpStatus.OK; + public ResponseEntity Isauth(@PathVariable String code) { + if (employeeService.isCodeValid(code)) { + return ResponseEntity + .status(HttpStatus.OK) + .body(" данный код существует - можно пользоваться приложением"); + } else if (code == null) { + return ResponseEntity + .status(HttpStatus.BAD_REQUEST) + .body(" что-то пошло не так"); + } else { + return ResponseEntity + .status(HttpStatus.UNAUTHORIZED) + .body("кода не существует"); } - else - return HttpStatus.UNAUTHORIZED; - } - } -- 2.34.1 From a13f3ebf338bb637c12bfadec83c32bb37fd7e47 Mon Sep 17 00:00:00 2001 From: lynxwq2 Date: Fri, 28 Nov 2025 19:33:01 +0300 Subject: [PATCH 10/25] =?UTF-8?q?=D1=82=D0=B5=D0=BA=D1=81=D1=82=D0=BE?= =?UTF-8?q?=D0=B2=D1=8B=D0=B5=20=D0=BE=D1=82=D0=B2=D0=B5=D1=82=D1=8B=20?= =?UTF-8?q?=D0=B2=20.body?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/example/nto/controller/EmployeeController.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/example/nto/controller/EmployeeController.java b/src/main/java/com/example/nto/controller/EmployeeController.java index f5b82f3..6eb6670 100644 --- a/src/main/java/com/example/nto/controller/EmployeeController.java +++ b/src/main/java/com/example/nto/controller/EmployeeController.java @@ -35,15 +35,15 @@ public class EmployeeController { if (employeeService.isCodeValid(code)) { return ResponseEntity .status(HttpStatus.OK) - .body(" данный код существует - можно пользоваться приложением"); + .body("данный код существует - можно пользоваться приложением"); } else if (code == null) { return ResponseEntity .status(HttpStatus.BAD_REQUEST) - .body(" что-то пошло не так"); + .body("что-то пошло не так"); } else { return ResponseEntity .status(HttpStatus.UNAUTHORIZED) - .body("кода не существует"); + .body("кода не существует"); } } -- 2.34.1 From 482501b93504e9c4b7e91b4b18aa3ed276a11613 Mon Sep 17 00:00:00 2001 From: lynxwq2 Date: Fri, 28 Nov 2025 21:51:09 +0300 Subject: [PATCH 11/25] =?UTF-8?q?+api/{code}/info(=D0=9F=D0=BE=D1=87?= =?UTF-8?q?=D1=82=D0=B8=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=B5=D1=82?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nto/controller/EmployeeController.java | 32 +++++++++++++------ .../java/com/example/nto/entity/Booking.java | 1 + .../java/com/example/nto/entity/Place.java | 6 +++- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/example/nto/controller/EmployeeController.java b/src/main/java/com/example/nto/controller/EmployeeController.java index 6eb6670..9e601e0 100644 --- a/src/main/java/com/example/nto/controller/EmployeeController.java +++ b/src/main/java/com/example/nto/controller/EmployeeController.java @@ -12,6 +12,8 @@ import org.springframework.web.bind.annotation.*; import java.net.http.HttpResponse; import java.util.List; +import java.util.Map; +import java.util.Optional; /** * TODO: ДОРАБОТАТЬ в рамках задания @@ -31,20 +33,30 @@ public class EmployeeController { } @GetMapping("/{code}/auth") - public ResponseEntity Isauth(@PathVariable String code) { + public HttpStatus Isauth(@PathVariable String code) { if (employeeService.isCodeValid(code)) { - return ResponseEntity - .status(HttpStatus.OK) - .body("данный код существует - можно пользоваться приложением"); + return HttpStatus.OK; + } else if (code == null) { - return ResponseEntity - .status(HttpStatus.BAD_REQUEST) - .body("что-то пошло не так"); + return + HttpStatus.BAD_REQUEST; + } else { - return ResponseEntity - .status(HttpStatus.UNAUTHORIZED) - .body("кода не существует"); + return + HttpStatus.UNAUTHORIZED; } } + @GetMapping("/{code}/info") + public ResponseEntity getInfo(@PathVariable String code){ + + Optional employee = employeeService.getByCode(code); + + if (employee.isPresent()) { + return ResponseEntity.ok(employee.get()); + } else { + return ResponseEntity.notFound().build(); + } + + } } diff --git a/src/main/java/com/example/nto/entity/Booking.java b/src/main/java/com/example/nto/entity/Booking.java index d15b740..d7b56c0 100644 --- a/src/main/java/com/example/nto/entity/Booking.java +++ b/src/main/java/com/example/nto/entity/Booking.java @@ -22,6 +22,7 @@ import java.time.LocalDate; @Entity public class Booking { @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) private long id; private LocalDate date; diff --git a/src/main/java/com/example/nto/entity/Place.java b/src/main/java/com/example/nto/entity/Place.java index 1bbd9bb..8e58ba8 100644 --- a/src/main/java/com/example/nto/entity/Place.java +++ b/src/main/java/com/example/nto/entity/Place.java @@ -19,7 +19,6 @@ import lombok.NoArgsConstructor; @Data @Builder @NoArgsConstructor -@AllArgsConstructor @Entity public class Place { @@ -28,4 +27,9 @@ public class Place { private long id; private String place; + + public Place(long id, String place){ + this.id = id; + this.place = place; + } } -- 2.34.1 From f42e1493a1ec538d18bab42b456fc67595e80116 Mon Sep 17 00:00:00 2001 From: lynxwq2 Date: Tue, 2 Dec 2025 20:28:07 +0300 Subject: [PATCH 12/25] :( --- .../com/example/nto/controller/EmployeeController.java | 9 +++------ src/main/java/com/example/nto/entity/Place.java | 9 ++++----- .../example/nto/service/impl/EmployeeServiceImpl.java | 2 ++ 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/example/nto/controller/EmployeeController.java b/src/main/java/com/example/nto/controller/EmployeeController.java index 9e601e0..db02108 100644 --- a/src/main/java/com/example/nto/controller/EmployeeController.java +++ b/src/main/java/com/example/nto/controller/EmployeeController.java @@ -37,13 +37,10 @@ public class EmployeeController { if (employeeService.isCodeValid(code)) { return HttpStatus.OK; - } else if (code == null) { - return - HttpStatus.BAD_REQUEST; - + } else if (!employeeService.isCodeValid(code)){ + return HttpStatus.UNAUTHORIZED; } else { - return - HttpStatus.UNAUTHORIZED; + return HttpStatus.BAD_REQUEST; } } diff --git a/src/main/java/com/example/nto/entity/Place.java b/src/main/java/com/example/nto/entity/Place.java index 8e58ba8..31b55fd 100644 --- a/src/main/java/com/example/nto/entity/Place.java +++ b/src/main/java/com/example/nto/entity/Place.java @@ -1,9 +1,6 @@ package com.example.nto.entity; -import jakarta.persistence.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; @@ -23,9 +20,11 @@ import lombok.NoArgsConstructor; public class Place { @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id") private long id; + + @Column(name = "place_name") private String place; public Place(long id, String place){ 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 66b2764..c1c752b 100644 --- a/src/main/java/com/example/nto/service/impl/EmployeeServiceImpl.java +++ b/src/main/java/com/example/nto/service/impl/EmployeeServiceImpl.java @@ -39,4 +39,6 @@ public class EmployeeServiceImpl implements EmployeeService { public boolean isCodeValid(String code){ return employeeRepository.findByCode(code).isPresent(); } + + } -- 2.34.1 From 7087390b381c1746f522217ea823d38fdf0e3ad7 Mon Sep 17 00:00:00 2001 From: lynxwq2 Date: Wed, 3 Dec 2025 17:16:15 +0300 Subject: [PATCH 13/25] + place --- .../nto/controller/BookingController.java | 19 ++++++++++--------- .../nto/repository/PlaceRepository.java | 9 ++++++++- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/example/nto/controller/BookingController.java b/src/main/java/com/example/nto/controller/BookingController.java index c4b3ccb..6eb13a0 100644 --- a/src/main/java/com/example/nto/controller/BookingController.java +++ b/src/main/java/com/example/nto/controller/BookingController.java @@ -1,10 +1,8 @@ package com.example.nto.controller; import com.example.nto.entity.Booking; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; +import com.example.nto.service.BookingService; +import org.springframework.web.bind.annotation.*; import java.time.LocalDate; @@ -15,11 +13,14 @@ import java.time.LocalDate; * НЕЛЬЗЯ: Изменять название класса и пакета */ @RestController -@RequestMapping("code") +@RequestMapping("/api") public class BookingController { -// @GetMapping("/booking") -// public Booking booking(@RequestParam(defaultValue = "")String id){ -// return new Booking(id, ); -// } + private final BookingService bookingService; + + @GetMapping("/{code}/info") + public ResponseEntity getEmployeeInfo(@PathVariable String code) { + EmployeeInfoResponse response = employeeService.getEmployeeInfo(code); + return ResponseEntity.ok(response); + } } diff --git a/src/main/java/com/example/nto/repository/PlaceRepository.java b/src/main/java/com/example/nto/repository/PlaceRepository.java index d3bea1d..616ae0d 100644 --- a/src/main/java/com/example/nto/repository/PlaceRepository.java +++ b/src/main/java/com/example/nto/repository/PlaceRepository.java @@ -1,10 +1,17 @@ package com.example.nto.repository; +import com.example.nto.entity.Booking; +import com.example.nto.entity.Place; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.Optional; + /** * TODO: ДОРАБОТАТЬ в рамках задания * ================================= * МОЖНО: Добавлять методы, аннотации, зависимости * НЕЛЬЗЯ: Изменять название класса и пакета */ -public interface PlaceRepository { +public interface PlaceRepository extends JpaRepository { + Optional findPlaceById(long id); } -- 2.34.1 From 6703a935bb5b44cde252bec1b7f91878f71e651a Mon Sep 17 00:00:00 2001 From: lynxwq2 Date: Wed, 3 Dec 2025 19:02:59 +0300 Subject: [PATCH 14/25] 1st without errors --- .../nto/controller/BookingController.java | 18 ++++-------- .../nto/controller/EmployeeController.java | 13 ++------- .../java/com/example/nto/entity/Booking.java | 5 +++- .../java/com/example/nto/entity/Employee.java | 7 ++++- .../nto/repository/BookingRepository.java | 3 ++ .../nto/repository/EmployeeRepository.java | 2 +- .../example/nto/service/EmployeeService.java | 3 ++ .../nto/service/impl/EmployeeServiceImpl.java | 28 +++++++++++++++++++ 8 files changed, 54 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/example/nto/controller/BookingController.java b/src/main/java/com/example/nto/controller/BookingController.java index 6eb13a0..106db06 100644 --- a/src/main/java/com/example/nto/controller/BookingController.java +++ b/src/main/java/com/example/nto/controller/BookingController.java @@ -12,15 +12,9 @@ import java.time.LocalDate; * МОЖНО: Добавлять методы, аннотации, зависимости * НЕЛЬЗЯ: Изменять название класса и пакета */ -@RestController -@RequestMapping("/api") -public class BookingController { - - private final BookingService bookingService; - - @GetMapping("/{code}/info") - public ResponseEntity getEmployeeInfo(@PathVariable String code) { - EmployeeInfoResponse response = employeeService.getEmployeeInfo(code); - return ResponseEntity.ok(response); - } -} +//@RestController +//@RequestMapping("/api") +//public class BookingController { +// +// +//} diff --git a/src/main/java/com/example/nto/controller/EmployeeController.java b/src/main/java/com/example/nto/controller/EmployeeController.java index db02108..7a0514c 100644 --- a/src/main/java/com/example/nto/controller/EmployeeController.java +++ b/src/main/java/com/example/nto/controller/EmployeeController.java @@ -45,15 +45,8 @@ public class EmployeeController { } @GetMapping("/{code}/info") - public ResponseEntity getInfo(@PathVariable String code){ - - Optional employee = employeeService.getByCode(code); - - if (employee.isPresent()) { - return ResponseEntity.ok(employee.get()); - } else { - return ResponseEntity.notFound().build(); - } - + public ResponseEntity> getEmployeeInfo(@PathVariable String code) { + Map response = employeeService.getEmployeeInfo(code); + return ResponseEntity.ok(response); } } diff --git a/src/main/java/com/example/nto/entity/Booking.java b/src/main/java/com/example/nto/entity/Booking.java index d7b56c0..045592b 100644 --- a/src/main/java/com/example/nto/entity/Booking.java +++ b/src/main/java/com/example/nto/entity/Booking.java @@ -1,5 +1,7 @@ package com.example.nto.entity; +import com.fasterxml.jackson.annotation.JsonBackReference; +import com.fasterxml.jackson.annotation.JsonIgnore; import jakarta.persistence.*; import lombok.AllArgsConstructor; import lombok.Builder; @@ -31,7 +33,8 @@ public class Booking { @JoinColumn(name = "place_id") private Place place; - @ManyToOne(targetEntity = Employee.class, fetch = FetchType.LAZY) + @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "employee_id") + @JsonBackReference 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 387b6b8..3ac4e3a 100644 --- a/src/main/java/com/example/nto/entity/Employee.java +++ b/src/main/java/com/example/nto/entity/Employee.java @@ -1,5 +1,7 @@ package com.example.nto.entity; +import com.fasterxml.jackson.annotation.JsonManagedReference; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; import jakarta.persistence.*; import lombok.AllArgsConstructor; import lombok.Builder; @@ -20,6 +22,7 @@ import java.util.List; @NoArgsConstructor @AllArgsConstructor @Entity +@JsonSerialize public class Employee { @Id @@ -32,7 +35,9 @@ public class Employee { private String photoUrl; - @OneToMany(mappedBy = "employee", cascade = CascadeType.ALL, fetch = FetchType.LAZY) + @OneToMany(mappedBy = "employee", cascade = CascadeType.ALL, fetch = FetchType.LAZY, + targetEntity = Booking.class) + @JsonManagedReference private List bookingList; public Employee(long id, String name, String code, String photoUrl) { diff --git a/src/main/java/com/example/nto/repository/BookingRepository.java b/src/main/java/com/example/nto/repository/BookingRepository.java index edf53ea..2a264b7 100644 --- a/src/main/java/com/example/nto/repository/BookingRepository.java +++ b/src/main/java/com/example/nto/repository/BookingRepository.java @@ -6,6 +6,7 @@ import org.springframework.stereotype.Repository; import org.springframework.stereotype.Service; import java.sql.Date; +import java.util.List; import java.util.Optional; /** @@ -18,4 +19,6 @@ import java.util.Optional; @Repository public interface BookingRepository extends JpaRepository { Optional findById(long id); + + List findByEmployee_Code(String code); } diff --git a/src/main/java/com/example/nto/repository/EmployeeRepository.java b/src/main/java/com/example/nto/repository/EmployeeRepository.java index e6ecbcb..725ab28 100644 --- a/src/main/java/com/example/nto/repository/EmployeeRepository.java +++ b/src/main/java/com/example/nto/repository/EmployeeRepository.java @@ -16,6 +16,6 @@ import java.util.Optional; @Service @Repository -public interface EmployeeRepository extends JpaRepository { +public interface EmployeeRepository extends JpaRepository { Optional findByCode(String code); } diff --git a/src/main/java/com/example/nto/service/EmployeeService.java b/src/main/java/com/example/nto/service/EmployeeService.java index a949b95..32e6980 100644 --- a/src/main/java/com/example/nto/service/EmployeeService.java +++ b/src/main/java/com/example/nto/service/EmployeeService.java @@ -3,6 +3,7 @@ package com.example.nto.service; import com.example.nto.entity.Employee; import java.util.List; +import java.util.Map; import java.util.Optional; /** @@ -18,4 +19,6 @@ public interface EmployeeService { boolean isCodeValid(String code); + + Map getEmployeeInfo(String code); } 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 c1c752b..4a4c7e3 100644 --- a/src/main/java/com/example/nto/service/impl/EmployeeServiceImpl.java +++ b/src/main/java/com/example/nto/service/impl/EmployeeServiceImpl.java @@ -1,14 +1,20 @@ package com.example.nto.service.impl; +import com.example.nto.entity.Booking; import com.example.nto.entity.Employee; import com.example.nto.repository.EmployeeRepository; import com.example.nto.service.EmployeeService; +import jakarta.transaction.Transactional; +import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; +import java.time.LocalDate; +import java.util.HashMap; import java.util.List; import java.util.Optional; +import java.util.stream.Collectors; /** * TODO: ДОРАБОТАТЬ в рамках задания @@ -36,9 +42,31 @@ public class EmployeeServiceImpl implements EmployeeService { return employeeRepository.findByCode(code); } + @Override public boolean isCodeValid(String code){ return employeeRepository.findByCode(code).isPresent(); } + public Map getEmployeeInfo(String code) { + Employee employee = getByCode(code) + .orElseThrow(() -> new RuntimeException("Employee not found")); + Map> bookingMap = employee.getBookingList().stream() + .collect(Collectors.toMap( + Booking::getDate, + booking -> { + Map info = new HashMap<>(); + info.put("id", booking.getId()); + info.put("place", booking.getPlace().getPlace()); + return info; + } + )); + + Map result = new HashMap<>(); + result.put("name", employee.getName()); + result.put("photoUrl", employee.getPhotoUrl()); + result.put("booking", bookingMap); + + return result; + } } -- 2.34.1 From 4a9e6633be6ccb35df87883e2cd89b6a45c84de7 Mon Sep 17 00:00:00 2001 From: lynxwq2 Date: Thu, 4 Dec 2025 14:51:56 +0300 Subject: [PATCH 15/25] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B0=20=D0=B0=D0=B2=D1=82=D0=BE=D1=80=D0=B8?= =?UTF-8?q?=D0=B7=D0=B0=D1=86=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/example/nto/controller/EmployeeController.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/example/nto/controller/EmployeeController.java b/src/main/java/com/example/nto/controller/EmployeeController.java index 7a0514c..4f157e7 100644 --- a/src/main/java/com/example/nto/controller/EmployeeController.java +++ b/src/main/java/com/example/nto/controller/EmployeeController.java @@ -33,14 +33,14 @@ public class EmployeeController { } @GetMapping("/{code}/auth") - public HttpStatus Isauth(@PathVariable String code) { + public ResponseEntity Isauth(@PathVariable String code) { if (employeeService.isCodeValid(code)) { - return HttpStatus.OK; + return ResponseEntity.ok().build(); } else if (!employeeService.isCodeValid(code)){ - return HttpStatus.UNAUTHORIZED; + return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build(); } else { - return HttpStatus.BAD_REQUEST; + return ResponseEntity.status(HttpStatus.BAD_REQUEST).build(); } } -- 2.34.1 From 639b1c6c24d760546e48c3bb77d168d96877572c Mon Sep 17 00:00:00 2001 From: lynxwq2 Date: Thu, 4 Dec 2025 16:17:26 +0300 Subject: [PATCH 16/25] =?UTF-8?q?+=D0=B1=D1=83=D0=BA=D0=B8=D0=BD=D0=B3=20(?= =?UTF-8?q?=D0=BF=D0=BE=D1=87=D1=82=D0=B8=20=D0=B1=D0=B5=D0=B7=20=D0=BE?= =?UTF-8?q?=D1=88=D0=B8=D0=B1=D0=BE=D0=BA)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nto/controller/BookingController.java | 29 +++++-- .../java/com/example/nto/entity/Booking.java | 8 ++ .../nto/repository/BookingRepository.java | 5 +- .../nto/repository/PlaceRepository.java | 1 - .../example/nto/service/BookingService.java | 4 +- .../nto/service/impl/BookingServiceImpl.java | 84 +++++++++++++++++-- 6 files changed, 113 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/example/nto/controller/BookingController.java b/src/main/java/com/example/nto/controller/BookingController.java index 106db06..6c90218 100644 --- a/src/main/java/com/example/nto/controller/BookingController.java +++ b/src/main/java/com/example/nto/controller/BookingController.java @@ -2,9 +2,13 @@ package com.example.nto.controller; import com.example.nto.entity.Booking; import com.example.nto.service.BookingService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.time.LocalDate; +import java.util.List; +import java.util.Map; /** * TODO: ДОРАБОТАТЬ в рамках задания @@ -12,9 +16,22 @@ import java.time.LocalDate; * МОЖНО: Добавлять методы, аннотации, зависимости * НЕЛЬЗЯ: Изменять название класса и пакета */ -//@RestController -//@RequestMapping("/api") -//public class BookingController { -// -// -//} +@RestController +@RequestMapping("/api") +public class BookingController { + + private final BookingService bookingService; + + @Autowired + public BookingController(BookingService bookingService) { + this.bookingService = bookingService; + } + + @GetMapping("/{code}/booking") + public ResponseEntity>> getAvailablePlaces( + @PathVariable String code + ) { + Map> response = bookingService.getAvailablePlaces(code); + return ResponseEntity.ok(response); + } +} diff --git a/src/main/java/com/example/nto/entity/Booking.java b/src/main/java/com/example/nto/entity/Booking.java index 045592b..5eb54d9 100644 --- a/src/main/java/com/example/nto/entity/Booking.java +++ b/src/main/java/com/example/nto/entity/Booking.java @@ -37,4 +37,12 @@ public class Booking { @JoinColumn(name = "employee_id") @JsonBackReference private Employee employee; + + + + @Data + public static class AvailablePlaceDto { + private long id; + private String place; + } } diff --git a/src/main/java/com/example/nto/repository/BookingRepository.java b/src/main/java/com/example/nto/repository/BookingRepository.java index 2a264b7..64e730e 100644 --- a/src/main/java/com/example/nto/repository/BookingRepository.java +++ b/src/main/java/com/example/nto/repository/BookingRepository.java @@ -6,6 +6,7 @@ import org.springframework.stereotype.Repository; import org.springframework.stereotype.Service; import java.sql.Date; +import java.time.LocalDate; import java.util.List; import java.util.Optional; @@ -18,7 +19,7 @@ import java.util.Optional; @Service @Repository public interface BookingRepository extends JpaRepository { - Optional findById(long id); + List findByDateAndEmployeeByCode(List dates, String code); - List findByEmployee_Code(String code); + Optional findById(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 616ae0d..638d706 100644 --- a/src/main/java/com/example/nto/repository/PlaceRepository.java +++ b/src/main/java/com/example/nto/repository/PlaceRepository.java @@ -13,5 +13,4 @@ import java.util.Optional; * НЕЛЬЗЯ: Изменять название класса и пакета */ public interface PlaceRepository extends JpaRepository { - Optional findPlaceById(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 c500134..206b655 100644 --- a/src/main/java/com/example/nto/service/BookingService.java +++ b/src/main/java/com/example/nto/service/BookingService.java @@ -4,6 +4,7 @@ import com.example.nto.entity.Booking; import com.example.nto.entity.Employee; import java.util.List; +import java.util.Map; import java.util.Optional; /** @@ -14,6 +15,7 @@ import java.util.Optional; */ public interface BookingService { List getAll(); - Optional getById(long id); + Optional getBookingById(String code); + Map> getAvailablePlaces(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 a25620f..3051a96 100644 --- a/src/main/java/com/example/nto/service/impl/BookingServiceImpl.java +++ b/src/main/java/com/example/nto/service/impl/BookingServiceImpl.java @@ -1,13 +1,19 @@ 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.repository.BookingRepository; +import com.example.nto.repository.EmployeeRepository; +import com.example.nto.repository.PlaceRepository; import com.example.nto.service.BookingService; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; -import java.util.List; -import java.util.Optional; +import java.time.LocalDate; +import java.util.*; +import java.util.stream.Collectors; /** * TODO: ДОРАБОТАТЬ в рамках задания @@ -20,10 +26,8 @@ import java.util.Optional; public class BookingServiceImpl implements BookingService { private final BookingRepository bookingRepository; - - public BookingServiceImpl(BookingRepository bookingRepository) { - this.bookingRepository = bookingRepository; - } + private final PlaceRepository placeRepository; + private final EmployeeRepository employeeRepository; @Override public List getAll() { @@ -31,8 +35,72 @@ public class BookingServiceImpl implements BookingService { } @Override - public Optional getById(long id) { - return bookingRepository.findById(id); + public Optional getBookingById(String code) { + return bookingRepository.findById(code); } + @Autowired + public BookingServiceImpl(BookingRepository bookingRepository, PlaceRepository placeRepository, + EmployeeRepository employeeRepository) { + this.bookingRepository = bookingRepository; + this.placeRepository = placeRepository; + this.employeeRepository = employeeRepository; + } + public Map> getAvailablePlaces(String employeeCode) { + // 1. Получаем сотрудника (для контекста) + Employee employee = employeeRepository.findByCode(employeeCode) + .orElseThrow(() -> new RuntimeException("Employee not found")); + + // 2. Определяем диапазон дат: сегодня + 3 дня + LocalDate today = LocalDate.now(); + List targetDates = List.of( + today, + today.plusDays(1), + today.plusDays(2), + today.plusDays(3) + ); + + // 3. Получаем все места из БД + List allPlaces = placeRepository.findAll(); + + // 4. Находим все бронирования на целевые даты + List bookings = bookingRepository.findByDateAndEmployeeByCode(targetDates, employeeCode); + + // 5. Группируем бронирования по датам + Map> bookingsByDate = bookings.stream() + .collect(Collectors.groupingBy(Booking::getDate)); + + // 6. Формируем итоговый ответ + Map> result = new HashMap<>(); + + for (LocalDate date : targetDates) { + String dateKey = date.toString(); // Формат: "yyyy-MM-dd" + + // Берём бронирования на эту дату + List dayBookings = bookingsByDate.getOrDefault(date, List.of()); + + // Собираем ID занятых мест + Set occupiedPlaceIds = dayBookings.stream() + .map(booking -> booking.getPlace().getId()) + .collect(Collectors.toSet()); + + // Фильтруем свободные места + List available = allPlaces.stream() + .filter(place -> !occupiedPlaceIds.contains(place.getId())) + .map(place -> { + Booking.AvailablePlaceDto dto = new Booking.AvailablePlaceDto(); + dto.setId(place.getId()); + dto.setPlace(place.getPlace()); + return dto; + }) + .collect(Collectors.toList()); + + result.put(dateKey, available); + } + + return result; + } } + + + -- 2.34.1 From 888284602028b8ec5733a80513b53d108fb832c4 Mon Sep 17 00:00:00 2001 From: lynxwq2 Date: Thu, 4 Dec 2025 16:40:11 +0300 Subject: [PATCH 17/25] =?UTF-8?q?+=D0=B1=D1=83=D0=BA=D0=B8=D0=BD=D0=B3=20(?= =?UTF-8?q?=D0=BF=D0=BE=D1=87=D1=82=D0=B8=20=D0=B1=D0=B5=D0=B7=20=D0=BE?= =?UTF-8?q?=D1=88=D0=B8=D0=B1=D0=BE=D0=BA)=20v2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/example/nto/repository/BookingRepository.java | 4 ++-- src/main/java/com/example/nto/service/BookingService.java | 2 +- .../com/example/nto/service/impl/BookingServiceImpl.java | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/example/nto/repository/BookingRepository.java b/src/main/java/com/example/nto/repository/BookingRepository.java index 64e730e..83d3958 100644 --- a/src/main/java/com/example/nto/repository/BookingRepository.java +++ b/src/main/java/com/example/nto/repository/BookingRepository.java @@ -19,7 +19,7 @@ import java.util.Optional; @Service @Repository public interface BookingRepository extends JpaRepository { - List findByDateAndEmployeeByCode(List dates, String code); + List findByDateInAndEmployee_Code(List dates, String code); - Optional findById(String code); + Optional findById(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 206b655..3488d55 100644 --- a/src/main/java/com/example/nto/service/BookingService.java +++ b/src/main/java/com/example/nto/service/BookingService.java @@ -15,7 +15,7 @@ import java.util.Optional; */ public interface BookingService { List getAll(); - Optional getBookingById(String code); + Optional getBookingById(Long id); Map> getAvailablePlaces(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 3051a96..8cd4d52 100644 --- a/src/main/java/com/example/nto/service/impl/BookingServiceImpl.java +++ b/src/main/java/com/example/nto/service/impl/BookingServiceImpl.java @@ -35,8 +35,8 @@ public class BookingServiceImpl implements BookingService { } @Override - public Optional getBookingById(String code) { - return bookingRepository.findById(code); + public Optional getBookingById(Long id) { + return bookingRepository.findById(id); } @Autowired @@ -64,7 +64,7 @@ public class BookingServiceImpl implements BookingService { List allPlaces = placeRepository.findAll(); // 4. Находим все бронирования на целевые даты - List bookings = bookingRepository.findByDateAndEmployeeByCode(targetDates, employeeCode); + List bookings = bookingRepository.findByDateInAndEmployee_Code(targetDates, employeeCode); // 5. Группируем бронирования по датам Map> bookingsByDate = bookings.stream() -- 2.34.1 From efbe78f118f64cf59212dd3acf58749af93da6f6 Mon Sep 17 00:00:00 2001 From: lynxwq2 Date: Thu, 4 Dec 2025 17:48:01 +0300 Subject: [PATCH 18/25] =?UTF-8?q?+=D0=B1=D1=83=D0=BA=D0=B8=D0=BD=D0=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/example/nto/repository/BookingRepository.java | 2 +- .../java/com/example/nto/service/impl/BookingServiceImpl.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/example/nto/repository/BookingRepository.java b/src/main/java/com/example/nto/repository/BookingRepository.java index 83d3958..9bb3aa0 100644 --- a/src/main/java/com/example/nto/repository/BookingRepository.java +++ b/src/main/java/com/example/nto/repository/BookingRepository.java @@ -19,7 +19,7 @@ import java.util.Optional; @Service @Repository public interface BookingRepository extends JpaRepository { - List findByDateInAndEmployee_Code(List dates, String code); + List findByDateIn(List dates); Optional findById(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 8cd4d52..450a603 100644 --- a/src/main/java/com/example/nto/service/impl/BookingServiceImpl.java +++ b/src/main/java/com/example/nto/service/impl/BookingServiceImpl.java @@ -64,7 +64,7 @@ public class BookingServiceImpl implements BookingService { List allPlaces = placeRepository.findAll(); // 4. Находим все бронирования на целевые даты - List bookings = bookingRepository.findByDateInAndEmployee_Code(targetDates, employeeCode); + List bookings = bookingRepository.findByDateIn(targetDates); // 5. Группируем бронирования по датам Map> bookingsByDate = bookings.stream() -- 2.34.1 From aee0974918a17788fe053eb358c2fcc65c1da29c Mon Sep 17 00:00:00 2001 From: lynxwq2 Date: Thu, 4 Dec 2025 18:20:13 +0300 Subject: [PATCH 19/25] =?UTF-8?q?+=D1=83=D1=81=D0=BB=D0=BE=D0=B2=D0=B8?= =?UTF-8?q?=D0=B5=20=D0=B4=D0=BB=D1=8F=20=D0=B1=D1=83=D0=BA=D0=B8=D0=BD?= =?UTF-8?q?=D0=B3=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../example/nto/controller/BookingController.java | 15 +++++++++++++-- .../com/example/nto/service/BookingService.java | 2 ++ .../nto/service/impl/BookingServiceImpl.java | 3 +++ 3 files changed, 18 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 6c90218..e0b2af4 100644 --- a/src/main/java/com/example/nto/controller/BookingController.java +++ b/src/main/java/com/example/nto/controller/BookingController.java @@ -3,6 +3,7 @@ package com.example.nto.controller; import com.example.nto.entity.Booking; import com.example.nto.service.BookingService; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @@ -31,7 +32,17 @@ public class BookingController { public ResponseEntity>> getAvailablePlaces( @PathVariable String code ) { - Map> response = bookingService.getAvailablePlaces(code); - return ResponseEntity.ok(response); + try { + if (!bookingService.isEmployeeExists(code)) { + return ResponseEntity + .status(401).build(); + } + Map> response = bookingService.getAvailablePlaces(code); + + return ResponseEntity.ok(response); + } catch (Exception e) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST).build(); + + } } } diff --git a/src/main/java/com/example/nto/service/BookingService.java b/src/main/java/com/example/nto/service/BookingService.java index 3488d55..0fc3edc 100644 --- a/src/main/java/com/example/nto/service/BookingService.java +++ b/src/main/java/com/example/nto/service/BookingService.java @@ -18,4 +18,6 @@ public interface BookingService { Optional getBookingById(Long id); Map> getAvailablePlaces(String code); + + boolean isEmployeeExists(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 450a603..09dbf12 100644 --- a/src/main/java/com/example/nto/service/impl/BookingServiceImpl.java +++ b/src/main/java/com/example/nto/service/impl/BookingServiceImpl.java @@ -46,6 +46,9 @@ public class BookingServiceImpl implements BookingService { this.placeRepository = placeRepository; this.employeeRepository = employeeRepository; } + public boolean isEmployeeExists(String code) { + return employeeRepository.findByCode(code).isPresent(); + } public Map> getAvailablePlaces(String employeeCode) { // 1. Получаем сотрудника (для контекста) Employee employee = employeeRepository.findByCode(employeeCode) -- 2.34.1 From 642eaeaaecb4fb5144c6b145980ddf3d4ab9fa8c Mon Sep 17 00:00:00 2001 From: Dell Date: Thu, 4 Dec 2025 20:40:42 +0300 Subject: [PATCH 20/25] +Employee not found with code --- .../nto/controller/BookingController.java | 20 +++++++++ .../nto/repository/BookingRepository.java | 6 +++ .../example/nto/service/BookingService.java | 3 ++ .../nto/service/impl/BookingServiceImpl.java | 44 +++++++++++++++++++ 4 files changed, 73 insertions(+) diff --git a/src/main/java/com/example/nto/controller/BookingController.java b/src/main/java/com/example/nto/controller/BookingController.java index e0b2af4..14a46c7 100644 --- a/src/main/java/com/example/nto/controller/BookingController.java +++ b/src/main/java/com/example/nto/controller/BookingController.java @@ -45,4 +45,24 @@ public class BookingController { } } + public static class CreateBookingRequest { + private LocalDate date; + private long placeId; + + public LocalDate getDate() { + return date; + } + + public void setDate(LocalDate date) { + this.date = date; + } + + public long getPlaceId() { + return placeId; + } + + public void setPlaceId(long placeId) { + this.placeId = placeId; + } + } } diff --git a/src/main/java/com/example/nto/repository/BookingRepository.java b/src/main/java/com/example/nto/repository/BookingRepository.java index 9bb3aa0..d88e7e3 100644 --- a/src/main/java/com/example/nto/repository/BookingRepository.java +++ b/src/main/java/com/example/nto/repository/BookingRepository.java @@ -22,4 +22,10 @@ public interface BookingRepository extends JpaRepository { List findByDateIn(List dates); Optional findById(Long id); + + Optional findByDateAndPlace_Id(LocalDate date, Long placeId); + + List findByDateAndEmployee_Code(LocalDate date, String employeeCode); + + List findByDateInAndEmployee_Code(List dates, String employeeCode); } diff --git a/src/main/java/com/example/nto/service/BookingService.java b/src/main/java/com/example/nto/service/BookingService.java index 0fc3edc..7125167 100644 --- a/src/main/java/com/example/nto/service/BookingService.java +++ b/src/main/java/com/example/nto/service/BookingService.java @@ -3,6 +3,7 @@ package com.example.nto.service; import com.example.nto.entity.Booking; import com.example.nto.entity.Employee; +import java.time.LocalDate; import java.util.List; import java.util.Map; import java.util.Optional; @@ -20,4 +21,6 @@ public interface BookingService { Map> getAvailablePlaces(String code); boolean isEmployeeExists(String code); + Booking createBooking(String employeeCode, LocalDate date, long placeId); + } 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 09dbf12..cbaef40 100644 --- a/src/main/java/com/example/nto/service/impl/BookingServiceImpl.java +++ b/src/main/java/com/example/nto/service/impl/BookingServiceImpl.java @@ -103,6 +103,50 @@ public class BookingServiceImpl implements BookingService { return result; } + @Override + public Booking createBooking(String employeeCode, LocalDate date, long placeId) { + // Проверяем сотрудника + Employee employee = employeeRepository.findByCode(employeeCode) + .orElseThrow(() -> new RuntimeException("Employee not found with code: " + employeeCode)); + + // Проверяем место + Place place = placeRepository.findById(placeId) + .orElseThrow(() -> new RuntimeException("Place not found with id: " + placeId)); + + // Проверяем, что дата в пределах 3 дней от сегодня + LocalDate today = LocalDate.now(); + LocalDate maxDate = today.plusDays(3); + + if (date.isBefore(today)) { + throw new RuntimeException("Cannot book in the past. Date: " + date); + } + + if (date.isAfter(maxDate)) { + throw new RuntimeException("Can only book up to 3 days in advance. Date: " + date); + } + + // Проверяем, не занято ли уже это место на эту дату + Optional existingPlaceBooking = bookingRepository.findByDateAndPlace_Id(date, placeId); + if (existingPlaceBooking.isPresent()) { + throw new RuntimeException("Place " + placeId + " is already booked for " + date); + } + + // Проверяем, нет ли у сотрудника уже брони на эту дату + List employeeBookings = bookingRepository.findByDateAndEmployee_Code(date, employeeCode); + if (!employeeBookings.isEmpty()) { + throw new RuntimeException("Employee already has a booking on " + date); + } + + // Создаем новое бронирование + Booking booking = Booking.builder() + .date(date) + .place(place) + .employee(employee) + .build(); + + // Сохраняем в базу + return bookingRepository.save(booking); + } } -- 2.34.1 From 7a0e8b70dbd5a1cbd696a56e3247db5e4588ef12 Mon Sep 17 00:00:00 2001 From: lynxwq2 Date: Fri, 5 Dec 2025 15:17:31 +0300 Subject: [PATCH 21/25] =?UTF-8?q?=D0=BF=D0=BE=D1=87=D1=82=D0=B8=20=D1=80?= =?UTF-8?q?=D0=BE=D0=B1=D0=B8=D1=82(400=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BA?= =?UTF-8?q?=D0=B0)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../nto/controller/BookingController.java | 56 +++++++++--- .../java/com/example/nto/entity/Booking.java | 28 ++++++ .../exception/BookingConflictException.java | 11 +++ .../exception/EmployeeNotFoundException.java | 12 +++ .../exception/InvalidBookingException.java | 11 +++ .../nto/repository/BookingRepository.java | 6 +- .../nto/service/impl/BookingServiceImpl.java | 85 ++++++++++++------- 7 files changed, 160 insertions(+), 49 deletions(-) create mode 100644 src/main/java/com/example/nto/exception/BookingConflictException.java create mode 100644 src/main/java/com/example/nto/exception/EmployeeNotFoundException.java create mode 100644 src/main/java/com/example/nto/exception/InvalidBookingException.java diff --git a/src/main/java/com/example/nto/controller/BookingController.java b/src/main/java/com/example/nto/controller/BookingController.java index 14a46c7..cc1f76f 100644 --- a/src/main/java/com/example/nto/controller/BookingController.java +++ b/src/main/java/com/example/nto/controller/BookingController.java @@ -1,10 +1,14 @@ package com.example.nto.controller; import com.example.nto.entity.Booking; +import com.example.nto.exception.BookingConflictException; +import com.example.nto.exception.EmployeeNotFoundException; +import com.example.nto.exception.InvalidBookingException; import com.example.nto.service.BookingService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.web.ErrorResponse; import org.springframework.web.bind.annotation.*; import java.time.LocalDate; @@ -45,24 +49,48 @@ public class BookingController { } } - public static class CreateBookingRequest { - private LocalDate date; - private long placeId; + @PostMapping("/{code}/book") + public ResponseEntity createBooking( + @PathVariable String code, + @RequestBody Booking.CreateBookingRequest request + ) { + try { + Booking booking = bookingService.createBooking( + code, + request.getDate(), + request.getPlaceId() + ); - public LocalDate getDate() { - return date; - } + // Возвращаем только статус 201 без тела + return ResponseEntity.status(HttpStatus.CREATED).build(); - public void setDate(LocalDate date) { - this.date = date; - } + } catch (EmployeeNotFoundException e) { + // 401 - кода не существует + return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build(); - public long getPlaceId() { - return placeId; - } + } catch (BookingConflictException e) { + // 409 - конфликт (уже забронировано) + return ResponseEntity.status(HttpStatus.CONFLICT).build(); - public void setPlaceId(long placeId) { - this.placeId = placeId; + } catch (InvalidBookingException e) { + // 400 - невалидные данные + return ResponseEntity.status(HttpStatus.BAD_REQUEST).build(); + + } catch (Exception e) { + // 500 - внутренняя ошибка сервера + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); } } + + private Booking.BookingResponse convertToResponse(Booking booking) { + Booking.BookingResponse response = new Booking.BookingResponse(); + response.setId(booking.getId()); + response.setDate(booking.getDate()); + response.setPlaceId(booking.getPlace().getId()); + response.setPlaceName(booking.getPlace().getPlace()); + response.setEmployeeId(booking.getEmployee().getId()); + response.setEmployeeName(booking.getEmployee().getName()); + return response; + } } + diff --git a/src/main/java/com/example/nto/entity/Booking.java b/src/main/java/com/example/nto/entity/Booking.java index 5eb54d9..0c15862 100644 --- a/src/main/java/com/example/nto/entity/Booking.java +++ b/src/main/java/com/example/nto/entity/Booking.java @@ -1,6 +1,7 @@ package com.example.nto.entity; import com.fasterxml.jackson.annotation.JsonBackReference; +import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonIgnore; import jakarta.persistence.*; import lombok.AllArgsConstructor; @@ -45,4 +46,31 @@ public class Booking { private long id; private String place; } + @Data + public class CreateBookingRequest { + @JsonFormat(pattern = "yyyy-MM-dd") + private LocalDate date; + private long placeId; + } + @Data + public class ErrorResponse { + private String error; + private String message; + + public ErrorResponse(String error, String message) { + this.error = error; + this.message = message; + } + } + + @Data + public static class BookingResponse { + private Long id; + @JsonFormat(pattern = "yyyy-MM-dd") + private LocalDate date; + private Long placeId; + private String placeName; + private Long employeeId; + private String employeeName; + } } diff --git a/src/main/java/com/example/nto/exception/BookingConflictException.java b/src/main/java/com/example/nto/exception/BookingConflictException.java new file mode 100644 index 0000000..f3e8c74 --- /dev/null +++ b/src/main/java/com/example/nto/exception/BookingConflictException.java @@ -0,0 +1,11 @@ +package com.example.nto.exception; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ResponseStatus(HttpStatus.CONFLICT) +public class BookingConflictException extends RuntimeException { + public BookingConflictException(String message) { + super(message); + } +} diff --git a/src/main/java/com/example/nto/exception/EmployeeNotFoundException.java b/src/main/java/com/example/nto/exception/EmployeeNotFoundException.java new file mode 100644 index 0000000..586473e --- /dev/null +++ b/src/main/java/com/example/nto/exception/EmployeeNotFoundException.java @@ -0,0 +1,12 @@ +package com.example.nto.exception; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ResponseStatus(HttpStatus.NOT_FOUND) +public class EmployeeNotFoundException extends RuntimeException { + public EmployeeNotFoundException(String message) { + super(message); + } +} + diff --git a/src/main/java/com/example/nto/exception/InvalidBookingException.java b/src/main/java/com/example/nto/exception/InvalidBookingException.java new file mode 100644 index 0000000..4fe16b2 --- /dev/null +++ b/src/main/java/com/example/nto/exception/InvalidBookingException.java @@ -0,0 +1,11 @@ +package com.example.nto.exception; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ResponseStatus(HttpStatus.BAD_REQUEST) +public class InvalidBookingException extends RuntimeException { + public InvalidBookingException(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 d88e7e3..4345e9c 100644 --- a/src/main/java/com/example/nto/repository/BookingRepository.java +++ b/src/main/java/com/example/nto/repository/BookingRepository.java @@ -23,9 +23,7 @@ public interface BookingRepository extends JpaRepository { Optional findById(Long id); - Optional findByDateAndPlace_Id(LocalDate date, Long placeId); - - List findByDateAndEmployee_Code(LocalDate date, String employeeCode); - List findByDateInAndEmployee_Code(List dates, String employeeCode); + Optional findByDateAndPlace_Id(LocalDate date, long placeId); + List findByDateAndEmployee_Code(LocalDate date, String employeeCode); } 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 cbaef40..00daa58 100644 --- a/src/main/java/com/example/nto/service/impl/BookingServiceImpl.java +++ b/src/main/java/com/example/nto/service/impl/BookingServiceImpl.java @@ -1,8 +1,12 @@ package com.example.nto.service.impl; import com.example.nto.entity.Booking; +import com.example.nto.entity.Booking.AvailablePlaceDto; import com.example.nto.entity.Employee; import com.example.nto.entity.Place; +import com.example.nto.exception.BookingConflictException; +import com.example.nto.exception.EmployeeNotFoundException; +import com.example.nto.exception.InvalidBookingException; import com.example.nto.repository.BookingRepository; import com.example.nto.repository.EmployeeRepository; import com.example.nto.repository.PlaceRepository; @@ -10,6 +14,7 @@ import com.example.nto.service.BookingService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import java.time.LocalDate; import java.util.*; @@ -49,7 +54,7 @@ public class BookingServiceImpl implements BookingService { public boolean isEmployeeExists(String code) { return employeeRepository.findByCode(code).isPresent(); } - public Map> getAvailablePlaces(String employeeCode) { + public Map> getAvailablePlaces(String employeeCode) { // 1. Получаем сотрудника (для контекста) Employee employee = employeeRepository.findByCode(employeeCode) .orElseThrow(() -> new RuntimeException("Employee not found")); @@ -74,7 +79,7 @@ public class BookingServiceImpl implements BookingService { .collect(Collectors.groupingBy(Booking::getDate)); // 6. Формируем итоговый ответ - Map> result = new HashMap<>(); + Map> result = new HashMap<>(); for (LocalDate date : targetDates) { String dateKey = date.toString(); // Формат: "yyyy-MM-dd" @@ -88,10 +93,10 @@ public class BookingServiceImpl implements BookingService { .collect(Collectors.toSet()); // Фильтруем свободные места - List available = allPlaces.stream() + List available = allPlaces.stream() .filter(place -> !occupiedPlaceIds.contains(place.getId())) .map(place -> { - Booking.AvailablePlaceDto dto = new Booking.AvailablePlaceDto(); + AvailablePlaceDto dto = new AvailablePlaceDto(); dto.setId(place.getId()); dto.setPlace(place.getPlace()); return dto; @@ -104,49 +109,67 @@ public class BookingServiceImpl implements BookingService { return result; } @Override + @Transactional public Booking createBooking(String employeeCode, LocalDate date, long placeId) { - // Проверяем сотрудника + // 1. Проверяем существование сотрудника (401 если нет) Employee employee = employeeRepository.findByCode(employeeCode) - .orElseThrow(() -> new RuntimeException("Employee not found with code: " + employeeCode)); + .orElseThrow(() -> new EmployeeNotFoundException("Employee with code '" + employeeCode + "' not found")); - // Проверяем место + // 2. Проверяем существование места (400 если нет) Place place = placeRepository.findById(placeId) - .orElseThrow(() -> new RuntimeException("Place not found with id: " + placeId)); + .orElseThrow(() -> new InvalidBookingException("Place with id " + placeId + " not found")); - // Проверяем, что дата в пределах 3 дней от сегодня - LocalDate today = LocalDate.now(); - LocalDate maxDate = today.plusDays(3); + // 3. Валидация даты (400 если невалидно) + validateBookingDate(date); - if (date.isBefore(today)) { - throw new RuntimeException("Cannot book in the past. Date: " + date); - } + // 4. Проверяем, не занято ли место (409 если занято) + checkPlaceAvailability(date, placeId); - if (date.isAfter(maxDate)) { - throw new RuntimeException("Can only book up to 3 days in advance. Date: " + date); - } + // 5. Проверяем, нет ли уже брони у сотрудника на эту дату (409 если есть) + checkEmployeeBookingConflict(employeeCode, date); - // Проверяем, не занято ли уже это место на эту дату - Optional existingPlaceBooking = bookingRepository.findByDateAndPlace_Id(date, placeId); - if (existingPlaceBooking.isPresent()) { - throw new RuntimeException("Place " + placeId + " is already booked for " + date); - } - - // Проверяем, нет ли у сотрудника уже брони на эту дату - List employeeBookings = bookingRepository.findByDateAndEmployee_Code(date, employeeCode); - if (!employeeBookings.isEmpty()) { - throw new RuntimeException("Employee already has a booking on " + date); - } - - // Создаем новое бронирование + // 6. Создаем и сохраняем бронирование Booking booking = Booking.builder() .date(date) .place(place) .employee(employee) .build(); - // Сохраняем в базу return bookingRepository.save(booking); } + + private void validateBookingDate(LocalDate date) { + LocalDate today = LocalDate.now(); + LocalDate maxDate = today.plusDays(3); + + if (date == null) { + throw new InvalidBookingException("Date is required"); + } + + if (date.isBefore(today)) { + throw new InvalidBookingException("Cannot book in the past"); + } + + if (date.isAfter(maxDate)) { + throw new InvalidBookingException("Can only book up to 3 days in advance"); + } + } + + private void checkPlaceAvailability(LocalDate date, long placeId) { + Optional existingBooking = bookingRepository.findByDateAndPlace_Id(date, placeId); + if (existingBooking.isPresent()) { + throw new BookingConflictException("Place " + placeId + " is already booked for " + date); + } + } + + private void checkEmployeeBookingConflict(String employeeCode, LocalDate date) { + List employeeBookingsOnDate = bookingRepository.findByDateAndEmployee_Code(date, employeeCode); + if (!employeeBookingsOnDate.isEmpty()) { + throw new BookingConflictException("Employee already has a booking on " + date); + } + } + + } -- 2.34.1 From 7155b7df069592fcdb2cb5ca2b9f0e65fc7611cb Mon Sep 17 00:00:00 2001 From: lynxwq2 Date: Fri, 5 Dec 2025 15:57:07 +0300 Subject: [PATCH 22/25] =?UTF-8?q?=D0=BD=D0=B5=D1=83=D0=B6=D0=B5=D0=BB?= =?UTF-8?q?=D0=B8=20=D0=B2=D1=81=D1=91=20>=5F Date: Fri, 5 Dec 2025 17:09:49 +0300 Subject: [PATCH 23/25] wrong answ code repaired --- .../nto/controller/BookingController.java | 5 +++ .../nto/service/impl/BookingServiceImpl.java | 35 +++++-------------- 2 files changed, 14 insertions(+), 26 deletions(-) diff --git a/src/main/java/com/example/nto/controller/BookingController.java b/src/main/java/com/example/nto/controller/BookingController.java index 45457a1..068fd05 100644 --- a/src/main/java/com/example/nto/controller/BookingController.java +++ b/src/main/java/com/example/nto/controller/BookingController.java @@ -58,15 +58,20 @@ public class BookingController { ) { try { Booking booking = bookingService.createBooking(code, request.getDate(), request.getPlaceId()); + System.out.println("OK"); return ResponseEntity.status(HttpStatus.CREATED).build(); } catch (EmployeeNotFoundException e) { + System.out.println("NOT"); return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build(); } catch (BookingConflictException e) { + System.out.println("NOT1"); return ResponseEntity.status(HttpStatus.CONFLICT).build(); } catch (InvalidBookingException e) { + System.out.println("NOT2"); return ResponseEntity.status(HttpStatus.BAD_REQUEST).build(); } catch (Exception e) { + System.out.println("NOT3"); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); } } 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 00daa58..f598fde 100644 --- a/src/main/java/com/example/nto/service/impl/BookingServiceImpl.java +++ b/src/main/java/com/example/nto/service/impl/BookingServiceImpl.java @@ -54,12 +54,11 @@ public class BookingServiceImpl implements BookingService { public boolean isEmployeeExists(String code) { return employeeRepository.findByCode(code).isPresent(); } - public Map> getAvailablePlaces(String employeeCode) { - // 1. Получаем сотрудника (для контекста) + @Override + public Map> getAvailablePlaces(String employeeCode) { Employee employee = employeeRepository.findByCode(employeeCode) - .orElseThrow(() -> new RuntimeException("Employee not found")); + .orElseThrow(() -> new EmployeeNotFoundException("Unauthorized")); - // 2. Определяем диапазон дат: сегодня + 3 дня LocalDate today = LocalDate.now(); List targetDates = List.of( today, @@ -68,41 +67,28 @@ public class BookingServiceImpl implements BookingService { today.plusDays(3) ); - // 3. Получаем все места из БД List allPlaces = placeRepository.findAll(); - - // 4. Находим все бронирования на целевые даты List bookings = bookingRepository.findByDateIn(targetDates); - - // 5. Группируем бронирования по датам Map> bookingsByDate = bookings.stream() .collect(Collectors.groupingBy(Booking::getDate)); - - // 6. Формируем итоговый ответ - Map> result = new HashMap<>(); + Map> result = new LinkedHashMap<>(); // Используем LinkedHashMap для сохранения порядка for (LocalDate date : targetDates) { - String dateKey = date.toString(); // Формат: "yyyy-MM-dd" - - // Берём бронирования на эту дату + String dateKey = date.toString(); List dayBookings = bookingsByDate.getOrDefault(date, List.of()); - // Собираем ID занятых мест Set occupiedPlaceIds = dayBookings.stream() .map(booking -> booking.getPlace().getId()) .collect(Collectors.toSet()); - - // Фильтруем свободные места - List available = allPlaces.stream() + List available = allPlaces.stream() .filter(place -> !occupiedPlaceIds.contains(place.getId())) .map(place -> { - AvailablePlaceDto dto = new AvailablePlaceDto(); + Booking.AvailablePlaceDto dto = new Booking.AvailablePlaceDto(); dto.setId(place.getId()); dto.setPlace(place.getPlace()); return dto; }) .collect(Collectors.toList()); - result.put(dateKey, available); } @@ -125,10 +111,7 @@ public class BookingServiceImpl implements BookingService { // 4. Проверяем, не занято ли место (409 если занято) checkPlaceAvailability(date, placeId); - // 5. Проверяем, нет ли уже брони у сотрудника на эту дату (409 если есть) - checkEmployeeBookingConflict(employeeCode, date); - // 6. Создаем и сохраняем бронирование Booking booking = Booking.builder() .date(date) .place(place) @@ -162,12 +145,12 @@ public class BookingServiceImpl implements BookingService { } } - private void checkEmployeeBookingConflict(String employeeCode, LocalDate date) { + /* private void checkEmployeeBookingConflict(String employeeCode, LocalDate date) { List employeeBookingsOnDate = bookingRepository.findByDateAndEmployee_Code(date, employeeCode); if (!employeeBookingsOnDate.isEmpty()) { throw new BookingConflictException("Employee already has a booking on " + date); } - } + } */ } -- 2.34.1 From 5e786c2701dc4661bd4b2b79b125302e7f687064 Mon Sep 17 00:00:00 2001 From: Dell Date: Fri, 5 Dec 2025 20:46:23 +0300 Subject: [PATCH 24/25] +Employee not found --- .../java/com/example/nto/service/impl/BookingServiceImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 f598fde..19435bf 100644 --- a/src/main/java/com/example/nto/service/impl/BookingServiceImpl.java +++ b/src/main/java/com/example/nto/service/impl/BookingServiceImpl.java @@ -70,7 +70,7 @@ public class BookingServiceImpl implements BookingService { List allPlaces = placeRepository.findAll(); List bookings = bookingRepository.findByDateIn(targetDates); Map> bookingsByDate = bookings.stream() - .collect(Collectors.groupingBy(Booking::getDate)); + .collect(Collectors.groupingBy(Booking::getDate, Collectors.toList())); Map> result = new LinkedHashMap<>(); // Используем LinkedHashMap для сохранения порядка for (LocalDate date : targetDates) { -- 2.34.1 From 09e13d89c977a7268a978e4c2fca50a665126e28 Mon Sep 17 00:00:00 2001 From: unknown Date: Fri, 5 Dec 2025 21:22:54 +0300 Subject: [PATCH 25/25] final --- .../nto/service/impl/BookingServiceImpl.java | 35 +++++++------------ 1 file changed, 12 insertions(+), 23 deletions(-) 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 19435bf..27b21cc 100644 --- a/src/main/java/com/example/nto/service/impl/BookingServiceImpl.java +++ b/src/main/java/com/example/nto/service/impl/BookingServiceImpl.java @@ -54,11 +54,9 @@ public class BookingServiceImpl implements BookingService { public boolean isEmployeeExists(String code) { return employeeRepository.findByCode(code).isPresent(); } - @Override - public Map> getAvailablePlaces(String employeeCode) { + public Map> getAvailablePlaces(String employeeCode) { Employee employee = employeeRepository.findByCode(employeeCode) - .orElseThrow(() -> new EmployeeNotFoundException("Unauthorized")); - + .orElseThrow(() -> new RuntimeException("Employee not found")); LocalDate today = LocalDate.now(); List targetDates = List.of( today, @@ -66,24 +64,21 @@ public class BookingServiceImpl implements BookingService { today.plusDays(2), today.plusDays(3) ); - List allPlaces = placeRepository.findAll(); List bookings = bookingRepository.findByDateIn(targetDates); Map> bookingsByDate = bookings.stream() - .collect(Collectors.groupingBy(Booking::getDate, Collectors.toList())); - Map> result = new LinkedHashMap<>(); // Используем LinkedHashMap для сохранения порядка - + .collect(Collectors.groupingBy(Booking::getDate)); + Map> result = new HashMap<>(); for (LocalDate date : targetDates) { - String dateKey = date.toString(); + String dateKey = date.toString(); // Формат: "yyyy-MM-dd" List dayBookings = bookingsByDate.getOrDefault(date, List.of()); - Set occupiedPlaceIds = dayBookings.stream() .map(booking -> booking.getPlace().getId()) .collect(Collectors.toSet()); - List available = allPlaces.stream() + List available = allPlaces.stream() .filter(place -> !occupiedPlaceIds.contains(place.getId())) .map(place -> { - Booking.AvailablePlaceDto dto = new Booking.AvailablePlaceDto(); + AvailablePlaceDto dto = new AvailablePlaceDto(); dto.setId(place.getId()); dto.setPlace(place.getPlace()); return dto; @@ -97,21 +92,13 @@ public class BookingServiceImpl implements BookingService { @Override @Transactional public Booking createBooking(String employeeCode, LocalDate date, long placeId) { - // 1. Проверяем существование сотрудника (401 если нет) Employee employee = employeeRepository.findByCode(employeeCode) .orElseThrow(() -> new EmployeeNotFoundException("Employee with code '" + employeeCode + "' not found")); - - // 2. Проверяем существование места (400 если нет) Place place = placeRepository.findById(placeId) .orElseThrow(() -> new InvalidBookingException("Place with id " + placeId + " not found")); - - // 3. Валидация даты (400 если невалидно) validateBookingDate(date); - - // 4. Проверяем, не занято ли место (409 если занято) checkPlaceAvailability(date, placeId); - - + checkEmployeeBookingConflict(employeeCode, date); Booking booking = Booking.builder() .date(date) .place(place) @@ -145,15 +132,17 @@ public class BookingServiceImpl implements BookingService { } } - /* private void checkEmployeeBookingConflict(String employeeCode, LocalDate date) { + private void checkEmployeeBookingConflict(String employeeCode, LocalDate date) { List employeeBookingsOnDate = bookingRepository.findByDateAndEmployee_Code(date, employeeCode); if (!employeeBookingsOnDate.isEmpty()) { throw new BookingConflictException("Employee already has a booking on " + date); } - } */ + } } + + -- 2.34.1