From 42a88034bf81a3f12d6c01150ad2c4514135d0d5 Mon Sep 17 00:00:00 2001 From: vinchent Date: Tue, 9 Jul 2024 23:08:31 +0200 Subject: [PATCH] Searching for availability by room --- internal/repository/dbrepo/postgres.go | 23 +++++++++++++++++++++++ internal/repository/repository.go | 6 +++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/internal/repository/dbrepo/postgres.go b/internal/repository/dbrepo/postgres.go index 1c960ed..29fdbbf 100644 --- a/internal/repository/dbrepo/postgres.go +++ b/internal/repository/dbrepo/postgres.go @@ -64,3 +64,26 @@ func (m *postgresDBRepo) InsertRoomRestriction(r models.RoomRestriction) error { 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 +} diff --git a/internal/repository/repository.go b/internal/repository/repository.go index 0076f0a..8b17375 100644 --- a/internal/repository/repository.go +++ b/internal/repository/repository.go @@ -1,10 +1,14 @@ package repository -import "go-udemy-web-1/internal/models" +import ( + "go-udemy-web-1/internal/models" + "time" +) type DatabaseRepo interface { AllUsers() bool InsertReservation(res models.Reservation) (int, error) InsertRoomRestriction(res models.RoomRestriction) error + SearchAvailabilityByDates(start, end time.Time, roomID int) (bool, error) }