Skip to content
Snippets Groups Projects
Commit a23ffb3e authored by hugcubi's avatar hugcubi
Browse files

Separado booking en controlador y servicio

parent 39cc475c
No related branches found
No related tags found
2 merge requests!26Revert "Funciona register",!22Separado booking en controlador y servicio
// BookingController.java
package com.uva.monolith.services.bookings.controllers;
import jakarta.transaction.Transactional;
import com.uva.monolith.services.bookings.models.Booking;
import com.uva.monolith.services.bookings.services.BookingService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.uva.monolith.services.bookings.models.Booking;
import com.uva.monolith.services.bookings.repositories.BookingRepository;
import com.uva.monolith.services.hotels.models.Room;
import com.uva.monolith.services.hotels.repositories.RoomRepository;
import com.uva.monolith.services.users.models.User;
import com.uva.monolith.services.users.repositories.UserRepository;
import java.time.LocalDate;
import java.util.List;
......@@ -22,89 +14,38 @@ import java.util.List;
@CrossOrigin(origins = "*")
public class BookingController {
private final BookingRepository bookingRepository;
private final UserRepository userRepository;
private final RoomRepository roomRepository;
private final BookingService bookingService;
public BookingController(BookingRepository bookingRepository, UserRepository userRepository,
RoomRepository roomRepository) {
this.bookingRepository = bookingRepository;
this.userRepository = userRepository;
this.roomRepository = roomRepository;
public BookingController(BookingService bookingService) {
this.bookingService = bookingService;
}
@GetMapping()
@GetMapping
public List<Booking> getAllBookings(
@RequestParam(required = false) LocalDate start,
@RequestParam(required = false) LocalDate end,
@RequestParam(required = false) Integer roomId,
@RequestParam(required = false) Integer userId) {
List<Booking> bookings = null;
if (start != null && end != null) {
bookings = bookingRepository.findByDateRange(start, end);
}
if (roomId != null) {
if (bookings == null) {
bookings = bookingRepository.findByRoomId(roomId);
} else {
bookings = bookings.stream()
.filter(booking -> booking.getRoomId().getId() == roomId)
.toList();
}
}
if (userId != null) {
if (bookings == null) {
bookings = bookingRepository.findByUserId(userId);
} else {
bookings = bookings.stream()
.filter(booking -> booking.getUserId().getId() == userId)
.toList();
}
}
if (start == null & end == null && roomId == null && userId == null) {
bookings = bookingRepository.findAll();
}
return bookings;
return bookingService.getBookings(start, end, roomId, userId);
}
@PostMapping
public Booking createBooking(@RequestBody Booking booking) {
User user = userRepository.findById(booking.getUserId().getId())
.orElseThrow(() -> new RuntimeException("User not found"));
Room room = roomRepository.findById(booking.getRoomId().getId())
.orElseThrow(() -> new RuntimeException("Room not found"));
// Verificar disponibilidad
List<Booking> existingBookings = bookingRepository.findByRoomIdAndDateRange(
room.getId(), booking.getStartDate(), booking.getEndDate());
if (!existingBookings.isEmpty()) {
throw new RuntimeException("Room is not available for the selected dates");
}
booking.setUserId(user);
booking.setRoomId(room);
return bookingRepository.save(booking);
return bookingService.createBooking(booking);
}
@GetMapping("/{id}")
public Booking getBookingById(@PathVariable Integer id) {
return bookingRepository.findById(id)
.orElseThrow(() -> new RuntimeException("Booking not found"));
return bookingService.getBookingById(id);
}
@DeleteMapping("/{id}")
@Transactional
public ResponseEntity<Void> deleteBooking(@PathVariable Integer id) {
try {
if (!bookingRepository.existsById(id))
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
bookingRepository.deleteBookingById(id);
bookingService.deleteBooking(id);
return new ResponseEntity<>(HttpStatus.ACCEPTED);
} catch (Exception e) {
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
} catch (RuntimeException e) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
}
package com.uva.monolith.services.bookings.services;
import com.uva.monolith.services.bookings.models.Booking;
import com.uva.monolith.services.bookings.repositories.BookingRepository;
import com.uva.monolith.services.hotels.models.Room;
import com.uva.monolith.services.hotels.repositories.RoomRepository;
import com.uva.monolith.services.users.models.User;
import com.uva.monolith.services.users.repositories.UserRepository;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.util.List;
@Service
public class BookingService {
private final BookingRepository bookingRepository;
private final UserRepository userRepository;
private final RoomRepository roomRepository;
public BookingService(BookingRepository bookingRepository, UserRepository userRepository, RoomRepository roomRepository) {
this.bookingRepository = bookingRepository;
this.userRepository = userRepository;
this.roomRepository = roomRepository;
}
public List<Booking> getBookings(LocalDate start, LocalDate end, Integer roomId, Integer userId) {
List<Booking> bookings = null;
if (start != null && end != null) {
bookings = bookingRepository.findByDateRange(start, end);
}
if (roomId != null) {
if (bookings == null) {
bookings = bookingRepository.findByRoomId(roomId);
} else {
bookings = bookings.stream()
.filter(booking -> booking.getRoomId().getId() == roomId)
.toList();
}
}
if (userId != null) {
if (bookings == null) {
bookings = bookingRepository.findByUserId(userId);
} else {
bookings = bookings.stream()
.filter(booking -> booking.getUserId().getId() == userId)
.toList();
}
}
if (start == null && end == null && roomId == null && userId == null) {
bookings = bookingRepository.findAll();
}
return bookings;
}
public Booking createBooking(Booking booking) {
User user = userRepository.findById(booking.getUserId().getId())
.orElseThrow(() -> new RuntimeException("User not found"));
Room room = roomRepository.findById(booking.getRoomId().getId())
.orElseThrow(() -> new RuntimeException("Room not found"));
// Check availability
List<Booking> existingBookings = bookingRepository.findByRoomIdAndDateRange(
room.getId(), booking.getStartDate(), booking.getEndDate());
if (!existingBookings.isEmpty()) {
throw new RuntimeException("Room is not available for the selected dates");
}
booking.setUserId(user);
booking.setRoomId(room);
return bookingRepository.save(booking);
}
public Booking getBookingById(Integer id) {
return bookingRepository.findById(id)
.orElseThrow(() -> new RuntimeException("Booking not found"));
}
public void deleteBooking(Integer id) {
if (!bookingRepository.existsById(id)) {
throw new RuntimeException("Booking not found");
}
bookingRepository.deleteBookingById(id);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment