1st without errors
This commit is contained in:
@@ -12,15 +12,9 @@ import java.time.LocalDate;
|
||||
* МОЖНО: Добавлять методы, аннотации, зависимости
|
||||
* НЕЛЬЗЯ: Изменять название класса и пакета
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/api")
|
||||
public class BookingController {
|
||||
|
||||
private final BookingService bookingService;
|
||||
|
||||
@GetMapping("/{code}/info")
|
||||
public ResponseEntity<EmployeeInfoResponse> getEmployeeInfo(@PathVariable String code) {
|
||||
EmployeeInfoResponse response = employeeService.getEmployeeInfo(code);
|
||||
return ResponseEntity.ok(response);
|
||||
}
|
||||
}
|
||||
//@RestController
|
||||
//@RequestMapping("/api")
|
||||
//public class BookingController {
|
||||
//
|
||||
//
|
||||
//}
|
||||
|
||||
@@ -45,15 +45,8 @@ public class EmployeeController {
|
||||
}
|
||||
|
||||
@GetMapping("/{code}/info")
|
||||
public ResponseEntity<?> getInfo(@PathVariable String code){
|
||||
|
||||
Optional<Employee> employee = employeeService.getByCode(code);
|
||||
|
||||
if (employee.isPresent()) {
|
||||
return ResponseEntity.ok(employee.get());
|
||||
} else {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
public ResponseEntity<Map<String, Object>> getEmployeeInfo(@PathVariable String code) {
|
||||
Map<String, Object> response = employeeService.getEmployeeInfo(code);
|
||||
return ResponseEntity.ok(response);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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<Booking> bookingList;
|
||||
|
||||
public Employee(long id, String name, String code, String photoUrl) {
|
||||
|
||||
@@ -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<Booking, Long> {
|
||||
Optional<Booking> findById(long id);
|
||||
|
||||
List<Booking> findByEmployee_Code(String code);
|
||||
}
|
||||
|
||||
@@ -16,6 +16,6 @@ import java.util.Optional;
|
||||
|
||||
@Service
|
||||
@Repository
|
||||
public interface EmployeeRepository extends JpaRepository<Employee, String> {
|
||||
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
|
||||
Optional<Employee> findByCode(String code);
|
||||
}
|
||||
|
||||
@@ -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<String, Object> getEmployeeInfo(String code);
|
||||
}
|
||||
|
||||
@@ -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<String, Object> getEmployeeInfo(String code) {
|
||||
Employee employee = getByCode(code)
|
||||
.orElseThrow(() -> new RuntimeException("Employee not found"));
|
||||
|
||||
Map<LocalDate, Map<String, Object>> bookingMap = employee.getBookingList().stream()
|
||||
.collect(Collectors.toMap(
|
||||
Booking::getDate,
|
||||
booking -> {
|
||||
Map<String, Object> info = new HashMap<>();
|
||||
info.put("id", booking.getId());
|
||||
info.put("place", booking.getPlace().getPlace());
|
||||
return info;
|
||||
}
|
||||
));
|
||||
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result.put("name", employee.getName());
|
||||
result.put("photoUrl", employee.getPhotoUrl());
|
||||
result.put("booking", bookingMap);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user