Compare commits

..

No commits in common. "b9f974cd1f3ec3beff2aebf98e5ee442ac313d24" and "74e9fee9420c126b0c88086bf7b9e83627d8d888" have entirely different histories.

5 changed files with 2 additions and 72 deletions

View File

@ -64,65 +64,3 @@ func (m *postgresDBRepo) InsertRoomRestriction(r models.RoomRestriction) error {
return nil return nil
} }
// SearchAvailabilityByDatesByRoomID returns true if availability exists for roomID, and false if no availability
func (m *postgresDBRepo) SearchAvailabilityByDatesByRoomID(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
}
// SearchAvailabilityForAllRooms returns a slice of rooms, if any, for given date range
func (m *postgresDBRepo) SearchAvailabilityForAllRooms(start, end time.Time) ([]models.Room, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
var rooms []models.Room
stmt := `select
r.id, r.room_name
from
rooms r
where r.id not in
(select
roomId
from
room_restrictions rr
where
$1 < rr.end_date and $2> rr.start_date)`
rows, err := m.DB.QueryContext(ctx, stmt, start, end)
if err != nil {
return nil, err
}
for rows.Next() {
var room models.Room
err := rows.Scan(&room.ID, &room.RoomName)
if err != nil {
return rooms, err
}
rooms = append(rooms, room)
}
if err = rows.Err(); err != nil {
return rooms, err
}
return rooms, nil
}

View File

@ -1,15 +1,10 @@
package repository package repository
import ( import "go-udemy-web-1/internal/models"
"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
SearchAvailabilityByDatesByRoomID(start, end time.Time, roomID int) (bool, error)
SearchAvailabilityForAllRooms(start, end time.Time) ([]models.Room, error)
} }

View File

@ -1,2 +0,0 @@
change_column("room_restrictions", "reservation_id", "integer", {"null": false})

View File

@ -1 +0,0 @@
change_column("room_restrictions", "reservation_id", "integer", {"null": true})

View File

@ -107,7 +107,7 @@ CREATE TABLE public.room_restrictions (
start_date date NOT NULL, start_date date NOT NULL,
end_date date NOT NULL, end_date date NOT NULL,
room_id integer NOT NULL, room_id integer NOT NULL,
reservation_id integer, reservation_id integer NOT NULL,
restriction_id integer NOT NULL, restriction_id integer NOT NULL,
created_at timestamp without time zone NOT NULL, created_at timestamp without time zone NOT NULL,
updated_at timestamp without time zone NOT NULL updated_at timestamp without time zone NOT NULL