This commit is contained in:
@@ -60,7 +60,7 @@ public class BookingController {
|
||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
|
||||
.body("Place ID and date must be provided");
|
||||
}
|
||||
Employee employee = employeeService.getEmployeeWithBookings(code);
|
||||
Employee employee = employeeService.getEmployeeByCode(code);
|
||||
if (employee == null) {
|
||||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
|
||||
.body("Employee not found");
|
||||
|
||||
@@ -7,6 +7,10 @@ import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* TODO: ДОРАБОТАТЬ в рамках задания
|
||||
* =================================
|
||||
@@ -18,31 +22,31 @@ import org.springframework.web.bind.annotation.*;
|
||||
public class EmployeeController {
|
||||
@Autowired
|
||||
private EmployeeService employeeService;
|
||||
@GetMapping("/{code}/auth")
|
||||
public ResponseEntity<Void> checkAuth(
|
||||
@PathVariable String code,
|
||||
@RequestHeader(value = "Authorization", required = false) String authHeader) {
|
||||
if (authHeader == null || !authHeader.equals("Bearer valid-token")) {
|
||||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
|
||||
}
|
||||
try {
|
||||
employeeService. getEmployeeWithBookings(code);
|
||||
return ResponseEntity.ok().build();
|
||||
} catch (IllegalArgumentException e) {
|
||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
|
||||
} catch (EmployeeNotFoundException e) {
|
||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
|
||||
}
|
||||
}
|
||||
@GetMapping("/{code}/info")
|
||||
public ResponseEntity<?> getEmployeeInfo(@PathVariable String code) {
|
||||
try {
|
||||
Employee response = employeeService.getEmployeeWithBookings(code);
|
||||
Employee employee = employeeService.getEmployeeByCode(code);
|
||||
Map<String, Object> response = new LinkedHashMap<>();
|
||||
response.put("name", employee.getName());
|
||||
response.put("photoUrl", employee.getPhotoUrl());
|
||||
Map<String, Object> bookingMap = employee.getBookings().stream()
|
||||
.collect(Collectors.toMap(
|
||||
b -> b.getDate().toString(),
|
||||
b -> Map.of(
|
||||
"id", b.getId(),
|
||||
"place", b.getPlace().getPlace()
|
||||
),
|
||||
(oldValue, newValue) -> newValue,
|
||||
LinkedHashMap::new
|
||||
));
|
||||
response.put("booking", bookingMap);
|
||||
return ResponseEntity.ok(response);
|
||||
} catch (IllegalArgumentException e) {
|
||||
return ResponseEntity.status(400).body(e.getMessage());
|
||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
|
||||
} catch (EmployeeNotFoundException e) {
|
||||
return ResponseEntity.status(404).body(e.getMessage());
|
||||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(e.getMessage());
|
||||
} catch (Exception e) {
|
||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Unexpected error");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
package com.example.nto.controller;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class EmployeeFullInfoResponseDto {
|
||||
private String name;
|
||||
private String photoUrl;
|
||||
private Map<String, BookingInfo> booking;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public static class BookingInfo {
|
||||
private Long id;
|
||||
private String place;
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,9 @@ import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@@ -31,28 +34,12 @@ public class Employee {
|
||||
private String code;
|
||||
|
||||
private String photoUrl;
|
||||
@OneToMany(mappedBy = "employee", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
|
||||
private List<Booking> bookings = new ArrayList<>();
|
||||
|
||||
@OneToMany(mappedBy = "employee", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
|
||||
@JsonIgnore
|
||||
private List<Booking> bookingList;
|
||||
|
||||
public List<Booking> getBookingList() {
|
||||
return bookingList;
|
||||
}
|
||||
|
||||
public Object getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public Object getPhotoUrl() {
|
||||
return photoUrl;
|
||||
}
|
||||
|
||||
public Object getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public Object getId() {
|
||||
return id;
|
||||
}
|
||||
public long getId() { return id; }
|
||||
public String getName() { return name; }
|
||||
public String getCode() { return code; }
|
||||
public String getPhotoUrl() { return photoUrl; }
|
||||
public List<Booking> getBookings() { return bookings; }
|
||||
}
|
||||
|
||||
@@ -16,6 +16,5 @@ import java.util.List;
|
||||
*/
|
||||
@Repository
|
||||
public interface BookingRepository extends JpaRepository<Booking, Long> {
|
||||
List<Booking> findByEmployee(Employee employee);
|
||||
List<Booking> findByDateBetween(LocalDate start, LocalDate end);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,8 @@ import com.example.nto.entity.Employee;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* TODO: ДОРАБОТАТЬ в рамках задания
|
||||
* =================================
|
||||
@@ -12,5 +14,5 @@ import org.springframework.stereotype.Repository;
|
||||
*/
|
||||
@Repository
|
||||
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
|
||||
Employee findByCode(String code);
|
||||
}
|
||||
Optional<Employee> findByCode(String code);
|
||||
}
|
||||
@@ -10,5 +10,5 @@ import org.springframework.stereotype.Service;
|
||||
*/
|
||||
@Service
|
||||
public interface EmployeeService {
|
||||
Employee getEmployeeWithBookings(String code);
|
||||
Employee getEmployeeByCode(String code);
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ public class BookingServiceImpl implements BookingService {
|
||||
if (employeeCode == null || employeeCode.isEmpty()) {
|
||||
throw new IllegalArgumentException("Employee code cannot be null or empty");
|
||||
}
|
||||
Employee employee = employeeService.getEmployeeWithBookings(employeeCode);
|
||||
Employee employee = employeeService.getEmployeeByCode(employeeCode);
|
||||
if (employee == null) {
|
||||
throw new EmployeeNotFoundException("Employee not found with code: " + employeeCode);
|
||||
}
|
||||
|
||||
@@ -4,7 +4,10 @@ import com.example.nto.excepation.EmployeeNotFoundException;
|
||||
import com.example.nto.repository.BookingRepository;
|
||||
import com.example.nto.repository.EmployeeRepository;
|
||||
import com.example.nto.service.EmployeeService;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
|
||||
@@ -15,19 +18,17 @@ import org.springframework.stereotype.Service;
|
||||
* НЕЛЬЗЯ: Изменять название класса и пакета
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class EmployeeServiceImpl implements EmployeeService {
|
||||
@Autowired
|
||||
private EmployeeRepository employeeRepository;
|
||||
@Override
|
||||
public Employee getEmployeeWithBookings(String code) {
|
||||
if (code == null || code.isEmpty()) {
|
||||
throw new IllegalArgumentException("Employee code cannot be null or empty");
|
||||
}
|
||||
Employee employee = employeeRepository.findByCode(code);
|
||||
if (employee == null) {
|
||||
throw new EmployeeNotFoundException("Employee not found with code: " + code);
|
||||
}
|
||||
return employee;
|
||||
}
|
||||
private EmployeeRepository employeeRepository;
|
||||
|
||||
@Override
|
||||
public Employee getEmployeeByCode(String code) {
|
||||
if (code == null || code.isEmpty()) {
|
||||
throw new IllegalArgumentException("Employee code is required");
|
||||
}
|
||||
return employeeRepository.findByCode(code)
|
||||
.orElseThrow(() -> new EmployeeNotFoundException("Employee not found"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user