main #8
@@ -60,7 +60,7 @@ public class BookingController {
|
|||||||
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
|
||||||
.body("Place ID and date must be provided");
|
.body("Place ID and date must be provided");
|
||||||
}
|
}
|
||||||
Employee employee = employeeService.getEmployeeWithBookings(code);
|
Employee employee = employeeService.getEmployeeByCode(code);
|
||||||
if (employee == null) {
|
if (employee == null) {
|
||||||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
|
return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
|
||||||
.body("Employee not found");
|
.body("Employee not found");
|
||||||
|
|||||||
@@ -7,6 +7,10 @@ import org.springframework.http.HttpStatus;
|
|||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO: ДОРАБОТАТЬ в рамках задания
|
* TODO: ДОРАБОТАТЬ в рамках задания
|
||||||
* =================================
|
* =================================
|
||||||
@@ -18,31 +22,31 @@ import org.springframework.web.bind.annotation.*;
|
|||||||
public class EmployeeController {
|
public class EmployeeController {
|
||||||
@Autowired
|
@Autowired
|
||||||
private EmployeeService employeeService;
|
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")
|
@GetMapping("/{code}/info")
|
||||||
public ResponseEntity<?> getEmployeeInfo(@PathVariable String code) {
|
public ResponseEntity<?> getEmployeeInfo(@PathVariable String code) {
|
||||||
try {
|
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);
|
return ResponseEntity.ok(response);
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
return ResponseEntity.status(400).body(e.getMessage());
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());
|
||||||
} catch (EmployeeNotFoundException e) {
|
} 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.Builder;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
@@ -31,28 +34,12 @@ public class Employee {
|
|||||||
private String code;
|
private String code;
|
||||||
|
|
||||||
private String photoUrl;
|
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)
|
public long getId() { return id; }
|
||||||
@JsonIgnore
|
public String getName() { return name; }
|
||||||
private List<Booking> bookingList;
|
public String getCode() { return code; }
|
||||||
|
public String getPhotoUrl() { return photoUrl; }
|
||||||
public List<Booking> getBookingList() {
|
public List<Booking> getBookings() { return bookings; }
|
||||||
return bookingList;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Object getCode() {
|
|
||||||
return code;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Object getPhotoUrl() {
|
|
||||||
return photoUrl;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Object getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Object getId() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,5 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
@Repository
|
@Repository
|
||||||
public interface BookingRepository extends JpaRepository<Booking, Long> {
|
public interface BookingRepository extends JpaRepository<Booking, Long> {
|
||||||
List<Booking> findByEmployee(Employee employee);
|
|
||||||
List<Booking> findByDateBetween(LocalDate start, LocalDate end);
|
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.data.jpa.repository.JpaRepository;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TODO: ДОРАБОТАТЬ в рамках задания
|
* TODO: ДОРАБОТАТЬ в рамках задания
|
||||||
* =================================
|
* =================================
|
||||||
@@ -12,5 +14,5 @@ import org.springframework.stereotype.Repository;
|
|||||||
*/
|
*/
|
||||||
@Repository
|
@Repository
|
||||||
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
|
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
|
@Service
|
||||||
public interface EmployeeService {
|
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()) {
|
if (employeeCode == null || employeeCode.isEmpty()) {
|
||||||
throw new IllegalArgumentException("Employee code cannot be null or empty");
|
throw new IllegalArgumentException("Employee code cannot be null or empty");
|
||||||
}
|
}
|
||||||
Employee employee = employeeService.getEmployeeWithBookings(employeeCode);
|
Employee employee = employeeService.getEmployeeByCode(employeeCode);
|
||||||
if (employee == null) {
|
if (employee == null) {
|
||||||
throw new EmployeeNotFoundException("Employee not found with code: " + employeeCode);
|
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.BookingRepository;
|
||||||
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 lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
|
||||||
@@ -15,19 +18,17 @@ import org.springframework.stereotype.Service;
|
|||||||
* НЕЛЬЗЯ: Изменять название класса и пакета
|
* НЕЛЬЗЯ: Изменять название класса и пакета
|
||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
public class EmployeeServiceImpl implements EmployeeService {
|
public class EmployeeServiceImpl implements EmployeeService {
|
||||||
@Autowired
|
@Autowired
|
||||||
private EmployeeRepository employeeRepository;
|
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@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