package com.example.nto.controller; 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 auth(@PathVariable("code") String code) { try { if (code == null) return ResponseEntity.badRequest().build(); Optional 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 opt = employeeService.findByCode(code); if (opt.isEmpty()) return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build(); Employee e = opt.get(); Map body = new LinkedHashMap<>(); body.put("name", e.getName()); body.put("photoUrl", e.getPhotoUrl()); // bookings grouped by date List bookings = bookingService.getBookingsForEmployee(e.getId()); Map> bookingMap = bookings.stream() .collect(Collectors.toMap( b -> b.getDate().toString(), b -> { Map 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(); } } }