Searching for availability by room

This commit is contained in:
vinchent 2024-07-09 23:08:31 +02:00
parent 74e9fee942
commit 42a88034bf
2 changed files with 28 additions and 1 deletions

View File

@ -64,3 +64,26 @@ func (m *postgresDBRepo) InsertRoomRestriction(r models.RoomRestriction) error {
return nil return nil
} }
// SearchAvailabilityByDates returns true if availability exists for roomID, and false if no availability
func (m *postgresDBRepo) SearchAvailabilityByDates(start, end time.Time, roomID int) (bool, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
var numRows int
stmt := `select count(id) from room_restrictions
where room_id = $1 and
$2 < end_date and $3> start_date`
row := m.DB.QueryRowContext(ctx, stmt, roomID, start, end)
err := row.Scan(&numRows)
if err != nil {
return false, err
}
if numRows == 0 {
return true, nil
}
return false, nil
}

View File

@ -1,10 +1,14 @@
package repository package repository
import "go-udemy-web-1/internal/models" import (
"go-udemy-web-1/internal/models"
"time"
)
type DatabaseRepo interface { type DatabaseRepo interface {
AllUsers() bool AllUsers() bool
InsertReservation(res models.Reservation) (int, error) InsertReservation(res models.Reservation) (int, error)
InsertRoomRestriction(res models.RoomRestriction) error InsertRoomRestriction(res models.RoomRestriction) error
SearchAvailabilityByDates(start, end time.Time, roomID int) (bool, error)
} }