This commit is contained in:
@@ -1,10 +1,68 @@
|
||||
package com.example.nto.controller;
|
||||
|
||||
/**
|
||||
* TODO: ДОРАБОТАТЬ в рамках задания
|
||||
* =================================
|
||||
* МОЖНО: Добавлять методы, аннотации, зависимости
|
||||
* НЕЛЬЗЯ: Изменять название класса и пакета
|
||||
*/
|
||||
import com.example.nto.entity.Booking;
|
||||
import com.example.nto.entity.Employee;
|
||||
import com.example.nto.service.BookingService;
|
||||
import com.example.nto.service.EmployeeService;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@RestController
|
||||
public class EmployeeController {
|
||||
|
||||
private final EmployeeService employeeService;
|
||||
private final BookingService bookingService;
|
||||
|
||||
public EmployeeController(EmployeeService employeeService, BookingService bookingService) {
|
||||
this.employeeService = employeeService;
|
||||
this.bookingService = bookingService;
|
||||
}
|
||||
|
||||
// GET /api/{code}/auth
|
||||
@GetMapping("/api/{code}/auth")
|
||||
public ResponseEntity<Void> auth(@PathVariable("code") String code) {
|
||||
try {
|
||||
if (code == null) return ResponseEntity.badRequest().build();
|
||||
Optional<Employee> e = employeeService.findByCode(code);
|
||||
return e.isPresent() ? ResponseEntity.ok().build() : ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
|
||||
} catch (Exception ex) {
|
||||
return ResponseEntity.badRequest().build();
|
||||
}
|
||||
}
|
||||
|
||||
// GET /api/{code}/info
|
||||
@GetMapping("/api/{code}/info")
|
||||
public ResponseEntity<?> info(@PathVariable("code") String code) {
|
||||
try {
|
||||
if (code == null) return ResponseEntity.badRequest().build();
|
||||
Optional<Employee> opt = employeeService.findByCode(code);
|
||||
if (opt.isEmpty()) return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
|
||||
Employee e = opt.get();
|
||||
Map<String, Object> body = new LinkedHashMap<>();
|
||||
body.put("name", e.getName());
|
||||
body.put("photoUrl", e.getPhotoUrl());
|
||||
// bookings grouped by date
|
||||
List<Booking> bookings = bookingService.getBookingsForEmployee(e.getId());
|
||||
Map<String, Map<String, Object>> bookingMap = bookings.stream()
|
||||
.collect(Collectors.toMap(
|
||||
b -> b.getDate().toString(),
|
||||
b -> {
|
||||
Map<String,Object> m = new LinkedHashMap<>();
|
||||
m.put("id", b.getId());
|
||||
m.put("place", b.getPlace().getPlace());
|
||||
return m;
|
||||
},
|
||||
(a,b)->a,
|
||||
LinkedHashMap::new
|
||||
));
|
||||
body.put("booking", bookingMap);
|
||||
return ResponseEntity.ok(body);
|
||||
} catch (Exception ex) {
|
||||
return ResponseEntity.badRequest().build();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user