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

Arreglado el DELETE de reserva

parent 21b777d1
No related branches found
No related tags found
1 merge request!10Add ts types and json mocks, remove poblate scripts and fix the cascade...
......@@ -8,6 +8,10 @@ import com.uva.roomBooking.Repositories.BookingRepository;
import com.uva.roomBooking.Repositories.RoomRepository;
import com.uva.roomBooking.Repositories.UserRepository;
import jakarta.transaction.Transactional;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
......@@ -53,9 +57,32 @@ public class BookingController {
return bookingRepository.save(booking);
}
@DeleteMapping("/{id}")
public void deleteBooking(@PathVariable Integer id) {
bookingRepository.deleteById(id);
@GetMapping("/{id}")
public Booking getBookingById(@PathVariable Integer id) {
return bookingRepository.findById(id)
.orElseThrow(() -> new RuntimeException("Booking not found"));
}
@DeleteMapping("/{id}")
@Transactional
public ResponseEntity<String> deleteBooking(@PathVariable Integer id) {
try {
if (!bookingRepository.existsById(id)) {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body("Booking not found with id: " + id);
}
bookingRepository.deleteBookingById(id);
return ResponseEntity.ok("Booking deleted successfully");
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body("Error deleting booking: " + e.getMessage());
}
}
}
......@@ -7,6 +7,7 @@ import java.time.LocalDate;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
......@@ -15,4 +16,10 @@ public interface BookingRepository extends JpaRepository<Booking, Integer> {
@Query("SELECT b FROM Booking b WHERE b.roomId.id = ?1 AND b.startDate < ?2 AND b.endDate > ?3")
List<Booking> findByRoomIdAndDateRange(@Param("roomId") int roomId, @Param("startDate") LocalDate startDate,
@Param("endDate") LocalDate endDate);
@Modifying
@Query("DELETE FROM Booking b WHERE b.id = :id")
void deleteBookingById(@Param("id") Integer 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