main #10
@@ -12,15 +12,9 @@ import java.time.LocalDate;
|
|||||||
* МОЖНО: Добавлять методы, аннотации, зависимости
|
* МОЖНО: Добавлять методы, аннотации, зависимости
|
||||||
* НЕЛЬЗЯ: Изменять название класса и пакета
|
* НЕЛЬЗЯ: Изменять название класса и пакета
|
||||||
*/
|
*/
|
||||||
@RestController
|
//@RestController
|
||||||
@RequestMapping("/api")
|
//@RequestMapping("/api")
|
||||||
public class BookingController {
|
//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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -45,15 +45,8 @@ public class EmployeeController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/{code}/info")
|
@GetMapping("/{code}/info")
|
||||||
public ResponseEntity<?> getInfo(@PathVariable String code){
|
public ResponseEntity<Map<String, Object>> getEmployeeInfo(@PathVariable String code) {
|
||||||
|
Map<String, Object> response = employeeService.getEmployeeInfo(code);
|
||||||
Optional<Employee> employee = employeeService.getByCode(code);
|
return ResponseEntity.ok(response);
|
||||||
|
|
||||||
if (employee.isPresent()) {
|
|
||||||
return ResponseEntity.ok(employee.get());
|
|
||||||
} else {
|
|
||||||
return ResponseEntity.notFound().build();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package com.example.nto.entity;
|
package com.example.nto.entity;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonBackReference;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
@@ -31,7 +33,8 @@ public class Booking {
|
|||||||
@JoinColumn(name = "place_id")
|
@JoinColumn(name = "place_id")
|
||||||
private Place place;
|
private Place place;
|
||||||
|
|
||||||
@ManyToOne(targetEntity = Employee.class, fetch = FetchType.LAZY)
|
@ManyToOne(fetch = FetchType.LAZY)
|
||||||
@JoinColumn(name = "employee_id")
|
@JoinColumn(name = "employee_id")
|
||||||
|
@JsonBackReference
|
||||||
private Employee employee;
|
private Employee employee;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package com.example.nto.entity;
|
package com.example.nto.entity;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonManagedReference;
|
||||||
|
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
@@ -20,6 +22,7 @@ import java.util.List;
|
|||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@Entity
|
@Entity
|
||||||
|
@JsonSerialize
|
||||||
public class Employee {
|
public class Employee {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
@@ -32,7 +35,9 @@ 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,
|
||||||
|
targetEntity = Booking.class)
|
||||||
|
@JsonManagedReference
|
||||||
private List<Booking> bookingList;
|
private List<Booking> bookingList;
|
||||||
|
|
||||||
public Employee(long id, String name, String code, String photoUrl) {
|
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 org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.sql.Date;
|
import java.sql.Date;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -18,4 +19,6 @@ import java.util.Optional;
|
|||||||
@Repository
|
@Repository
|
||||||
public interface BookingRepository extends JpaRepository<Booking, Long> {
|
public interface BookingRepository extends JpaRepository<Booking, Long> {
|
||||||
Optional<Booking> findById(long id);
|
Optional<Booking> findById(long id);
|
||||||
|
|
||||||
|
List<Booking> findByEmployee_Code(String code);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,6 @@ import java.util.Optional;
|
|||||||
|
|
||||||
@Service
|
@Service
|
||||||
@Repository
|
@Repository
|
||||||
public interface EmployeeRepository extends JpaRepository<Employee, String> {
|
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
|
||||||
Optional<Employee> findByCode(String code);
|
Optional<Employee> findByCode(String code);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package com.example.nto.service;
|
|||||||
import com.example.nto.entity.Employee;
|
import com.example.nto.entity.Employee;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -18,4 +19,6 @@ public interface EmployeeService {
|
|||||||
|
|
||||||
|
|
||||||
boolean isCodeValid(String code);
|
boolean isCodeValid(String code);
|
||||||
|
|
||||||
|
Map<String, Object> getEmployeeInfo(String code);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,20 @@
|
|||||||
package com.example.nto.service.impl;
|
package com.example.nto.service.impl;
|
||||||
|
|
||||||
|
import com.example.nto.entity.Booking;
|
||||||
import com.example.nto.entity.Employee;
|
import com.example.nto.entity.Employee;
|
||||||
import com.example.nto.repository.EmployeeRepository;
|
import com.example.nto.repository.EmployeeRepository;
|
||||||
import com.example.nto.service.EmployeeService;
|
import com.example.nto.service.EmployeeService;
|
||||||
|
import jakarta.transaction.Transactional;
|
||||||
|
import java.util.Map;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO: ДОРАБОТАТЬ в рамках задания
|
* TODO: ДОРАБОТАТЬ в рамках задания
|
||||||
@@ -36,9 +42,31 @@ public class EmployeeServiceImpl implements EmployeeService {
|
|||||||
return employeeRepository.findByCode(code);
|
return employeeRepository.findByCode(code);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public boolean isCodeValid(String code){
|
public boolean isCodeValid(String code){
|
||||||
return employeeRepository.findByCode(code).isPresent();
|
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