третья попытка((((

This commit is contained in:
2025-11-29 20:11:44 +03:00
parent 0fc0678392
commit c4afce7fa6
5 changed files with 19 additions and 47 deletions

View File

@@ -37,19 +37,17 @@ public class BookingController {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build(); return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
} }
} }
@GetMapping("/info") @GetMapping("/info")
public ResponseEntity<Map<String, Object>> info(@PathVariable String code) { public ResponseEntity<Map<String, Object>> info(@PathVariable String code) {
Optional<Employee> employeeOpt = employeeService.findByCode(code); Optional<Employee> employeeOpt = employeeService.findByCode(code);
if (employeeOpt.isEmpty()) return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build(); if (employeeOpt.isEmpty()) return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
Employee employee = employeeOpt.get(); Employee employee = employeeOpt.get();
Map<String, Object> response = new HashMap<>(); Map<String, Object> response = new HashMap<>();
response.put("name", employee.getName()); response.put("name", employee.getName());
response.put("photoUrl", employee.getPhotoUrl()); response.put("photoUrl", employee.getPhotoUrl());
Map<String, Map<String, Object>> bookingsMap = new HashMap<>(); Map<String, List<Map<String, Object>>> bookingsMap = new HashMap<>();
List<Booking> bookingList = bookingService.getBookingsByEmployee(employee); List<Booking> bookingList = bookingService.getBookingsByEmployee(employee);
for (Booking b : bookingList) { for (Booking b : bookingList) {
@@ -58,11 +56,10 @@ public class BookingController {
bookingData.put("id", b.getId()); bookingData.put("id", b.getId());
bookingData.put("place", b.getPlace().getPlace()); bookingData.put("place", b.getPlace().getPlace());
bookingsMap.put(dateKey, bookingData); bookingsMap.computeIfAbsent(dateKey, k -> new ArrayList<>()).add(bookingData);
} }
response.put("booking", bookingsMap); response.put("booking", bookingsMap);
return ResponseEntity.ok(response); return ResponseEntity.ok(response);
} }
@@ -107,8 +104,6 @@ public class BookingController {
try { try {
String dateStr = body.get("date"); String dateStr = body.get("date");
// поддерживаем все варианты ключей
String placeIdStr = body.getOrDefault("placeID", String placeIdStr = body.getOrDefault("placeID",
body.getOrDefault("placeId", body.getOrDefault("placeId",
body.getOrDefault("place", null))); body.getOrDefault("place", null)));

View File

@@ -21,7 +21,10 @@ public class EmployeeController {
@GetMapping("/{code}") @GetMapping("/{code}")
public ResponseEntity<Employee> getEmployeeByCode(@PathVariable String code) { public ResponseEntity<Employee> getEmployeeByCode(@PathVariable String code) {
Optional<Employee> employeeOpt = employeeService.findByCode(code); Optional<Employee> employeeOpt = employeeService.findByCode(code);
return employeeOpt.map(ResponseEntity::ok) if (employeeOpt.isPresent()) {
.orElseGet(() -> ResponseEntity.status(HttpStatus.NOT_FOUND).build()); return ResponseEntity.ok(employeeOpt.get());
} else {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
}
} }
} }

View File

@@ -1,10 +1,12 @@
package com.example.nto.entity; package com.example.nto.entity;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import jakarta.persistence.*; import jakarta.persistence.*;
import java.time.LocalDate; import java.time.LocalDate;
@Entity @Entity
@Table(name = "booking") @Table(name = "booking")
@JsonIgnoreProperties({"hibernateLazyInitializer"})
public class Booking { public class Booking {
@Id @Id
@@ -13,11 +15,11 @@ public class Booking {
private LocalDate date; private LocalDate date;
@ManyToOne(fetch = FetchType.LAZY) @ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "place_id") @JoinColumn(name = "place_id")
private Place place; private Place place;
@ManyToOne(fetch = FetchType.LAZY) @ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "employee_id") @JoinColumn(name = "employee_id")
private Employee employee; private Employee employee;
@@ -31,13 +33,10 @@ public class Booking {
public long getId() { return id; } public long getId() { return id; }
public void setId(long id) { this.id = id; } public void setId(long id) { this.id = id; }
public LocalDate getDate() { return date; } public LocalDate getDate() { return date; }
public void setDate(LocalDate date) { this.date = date; } public void setDate(LocalDate date) { this.date = date; }
public Place getPlace() { return place; } public Place getPlace() { return place; }
public void setPlace(Place place) { this.place = place; } public void setPlace(Place place) { this.place = place; }
public Employee getEmployee() { return employee; } public Employee getEmployee() { return employee; }
public void setEmployee(Employee employee) { this.employee = employee; } public void setEmployee(Employee employee) { this.employee = employee; }
} }

View File

@@ -1,5 +1,6 @@
package com.example.nto.entity; package com.example.nto.entity;
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*; import jakarta.persistence.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@@ -20,6 +21,7 @@ public class Employee {
private String photoUrl; private String photoUrl;
@OneToMany(mappedBy = "employee", cascade = CascadeType.ALL, fetch = FetchType.LAZY) @OneToMany(mappedBy = "employee", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JsonIgnore
private List<Booking> bookingList = new ArrayList<>(); private List<Booking> bookingList = new ArrayList<>();
public Employee() {} public Employee() {}
@@ -32,16 +34,12 @@ public class Employee {
public long getId() { return id; } public long getId() { return id; }
public void setId(long id) { this.id = id; } public void setId(long id) { this.id = id; }
public String getName() { return name; } public String getName() { return name; }
public void setName(String name) { this.name = name; } public void setName(String name) { this.name = name; }
public String getCode() { return code; } public String getCode() { return code; }
public void setCode(String code) { this.code = code; } public void setCode(String code) { this.code = code; }
public String getPhotoUrl() { return photoUrl; } public String getPhotoUrl() { return photoUrl; }
public void setPhotoUrl(String photoUrl) { this.photoUrl = photoUrl; } public void setPhotoUrl(String photoUrl) { this.photoUrl = photoUrl; }
public List<Booking> getBookingList() { return bookingList; } public List<Booking> getBookingList() { return bookingList; }
public void setBookingList(List<Booking> bookingList) { this.bookingList = bookingList; } public void setBookingList(List<Booking> bookingList) { this.bookingList = bookingList; }
} }

View File

@@ -13,34 +13,11 @@ public class Place {
@Column(name = "place_name", nullable = false, unique = true) @Column(name = "place_name", nullable = false, unique = true)
private String place; private String place;
public Place() { public Place() {}
} public Place(String place) { this.place = place; }
public Place(String place) { public Long getId() { return id; }
this.place = place; public void setId(Long id) { this.id = id; }
} public String getPlace() { return place; }
public void setPlace(String place) { this.place = place; }
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getPlace() {
return place;
}
public void setPlace(String place) {
this.place = place;
}
@Override
public String toString() {
return "Place{" +
"id=" + id +
", place='" + place + '\'' +
'}';
}
} }