searching for availability for all rooms
This commit is contained in:
		@ -65,8 +65,8 @@ 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) {
 | 
			
		||||
// 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()
 | 
			
		||||
 | 
			
		||||
@ -87,3 +87,42 @@ func (m *postgresDBRepo) SearchAvailabilityByDates(start, end time.Time, roomID
 | 
			
		||||
 | 
			
		||||
	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
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -10,5 +10,6 @@ type DatabaseRepo interface {
 | 
			
		||||
 | 
			
		||||
	InsertReservation(res models.Reservation) (int, error)
 | 
			
		||||
	InsertRoomRestriction(res models.RoomRestriction) error
 | 
			
		||||
	SearchAvailabilityByDates(start, end time.Time, roomID int) (bool, error)
 | 
			
		||||
	SearchAvailabilityByDatesByRoomID(start, end time.Time, roomID int) (bool, error)
 | 
			
		||||
	SearchAvailabilityForAllRooms(start, end time.Time) ([]models.Room, error)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -0,0 +1,2 @@
 | 
			
		||||
change_column("room_restrictions", "reservation_id", "integer", {"null": false})
 | 
			
		||||
 | 
			
		||||
@ -0,0 +1 @@
 | 
			
		||||
change_column("room_restrictions", "reservation_id", "integer", {"null": true})
 | 
			
		||||
@ -107,7 +107,7 @@ CREATE TABLE public.room_restrictions (
 | 
			
		||||
    start_date date NOT NULL,
 | 
			
		||||
    end_date date NOT NULL,
 | 
			
		||||
    room_id integer NOT NULL,
 | 
			
		||||
    reservation_id integer NOT NULL,
 | 
			
		||||
    reservation_id integer,
 | 
			
		||||
    restriction_id integer NOT NULL,
 | 
			
		||||
    created_at timestamp without time zone NOT NULL,
 | 
			
		||||
    updated_at timestamp without time zone NOT NULL
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user