package com.valposystems.controller; import com.valposystems.dto.BookingDTO; import com.valposystems.service.BookingService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequestMapping("/api/bookings") @CrossOrigin public class BookingController { private static final Logger logger = LoggerFactory.getLogger(BookingController.class); private final BookingService bookingService; @Autowired public BookingController(BookingService bookingService) { this.bookingService = bookingService; } @GetMapping public ResponseEntity> getAllBookings() { try { List bookings = bookingService.getAllBookings(); return ResponseEntity.ok(bookings); } catch (Exception e) { logger.error("Error getting bookings", e); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body(null); } } @GetMapping("/user/{userId}") public ResponseEntity> getBookingsByUserId(@PathVariable Long userId) { try { List bookings = bookingService.getBookingsByUserId(userId); return ResponseEntity.ok(bookings); } catch (Exception e) { logger.error("Error getting bookings for user: {}", userId, e); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body(null); } } @PostMapping public ResponseEntity createBooking(@RequestBody BookingDTO dto) { try { BookingDTO created = bookingService.createBooking(dto); if (created == null) { return ResponseEntity.status(HttpStatus.CONFLICT) .body("{\"message\": \"No spots available or the user/class doesn't exist\"}"); } return ResponseEntity.status(HttpStatus.CREATED).body(created); } catch (Exception e) { logger.error("Error creating booking", e); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body("{\"error\": \"" + e.getMessage() + "\"}"); } } @PutMapping("/{id}/cancel") public ResponseEntity cancelBooking(@PathVariable Long id) { try { BookingDTO cancelled = bookingService.cancelBooking(id); if (cancelled == null) { return ResponseEntity.status(HttpStatus.NOT_FOUND) .body("{\"message\": \"The booking doesn't exist or isn't in confirmed state\"}"); } return ResponseEntity.ok(cancelled); } catch (Exception e) { logger.error("Error cancelling booking with ID: {}", id, e); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body("{\"error\": \"" + e.getMessage() + "\"}"); } } }