Reservation Calendar 3
This commit is contained in:
		@ -387,6 +387,7 @@ func (m *postgresDBRepo) UpdateProcessedForReservation(id, processed int) error
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// AllRooms gets all rooms
 | 
			
		||||
func (m *postgresDBRepo) AllRooms() ([]models.Room, error) {
 | 
			
		||||
	ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
 | 
			
		||||
	defer cancel()
 | 
			
		||||
@ -419,3 +420,42 @@ func (m *postgresDBRepo) AllRooms() ([]models.Room, error) {
 | 
			
		||||
	}
 | 
			
		||||
	return rooms, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// GetRestrictionsForRoomByDate returns restrictions for a room by date range
 | 
			
		||||
func (m *postgresDBRepo) GetRestrictionsForRoomByDate(roomId int, start, end time.Time) ([]models.RoomRestriction, error) {
 | 
			
		||||
	ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
 | 
			
		||||
	defer cancel()
 | 
			
		||||
 | 
			
		||||
	var restrictions []models.RoomRestriction
 | 
			
		||||
 | 
			
		||||
	// coalesce use 0 if null
 | 
			
		||||
	query := `select id, coalesce(reservation_id, 0), restriction_id, room_id, start_date, end_date
 | 
			
		||||
             from room_restrictions where $1 < end_date and $2 >= start_date
 | 
			
		||||
             and room_id = $3`
 | 
			
		||||
 | 
			
		||||
	rows, err := m.DB.QueryContext(ctx, query, start, end, roomId)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return restrictions, err
 | 
			
		||||
	}
 | 
			
		||||
	defer rows.Close() // To avoid memory leak
 | 
			
		||||
 | 
			
		||||
	for rows.Next() {
 | 
			
		||||
		var r models.RoomRestriction
 | 
			
		||||
		err := rows.Scan(
 | 
			
		||||
			&r.ID,
 | 
			
		||||
			&r.ReservationID,
 | 
			
		||||
			&r.RestrictionID,
 | 
			
		||||
			&r.RoomID,
 | 
			
		||||
			&r.StartDate,
 | 
			
		||||
			&r.EndDate,
 | 
			
		||||
		)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return restrictions, err
 | 
			
		||||
		}
 | 
			
		||||
		restrictions = append(restrictions, r)
 | 
			
		||||
	}
 | 
			
		||||
	if err = rows.Err(); err != nil {
 | 
			
		||||
		return restrictions, err
 | 
			
		||||
	}
 | 
			
		||||
	return restrictions, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -122,3 +122,9 @@ func (m *testDBRepo) AllRooms() ([]models.Room, error) {
 | 
			
		||||
 | 
			
		||||
	return rooms, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// GetRestrictionsForRoomByDate returns restrictions for a room by date range
 | 
			
		||||
func (m *testDBRepo) GetRestrictionsForRoomByDate(roomId int, start, end time.Time) ([]models.RoomRestriction, error) {
 | 
			
		||||
	var restrictions []models.RoomRestriction
 | 
			
		||||
	return restrictions, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -23,4 +23,5 @@ type DatabaseRepo interface {
 | 
			
		||||
	DeleteReservation(id int) error
 | 
			
		||||
	UpdateProcessedForReservation(id, processed int) error
 | 
			
		||||
	AllRooms() ([]models.Room, error)
 | 
			
		||||
	GetRestrictionsForRoomByDate(roomId int, start, end time.Time) ([]models.RoomRestriction, error)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user