Reservation Calendar 3
This commit is contained in:
		@ -309,11 +309,11 @@ func (m *Repository) PostAvailability(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type jsonResponse struct {
 | 
			
		||||
	OK        bool   `json:"ok"`
 | 
			
		||||
	Message   string `json:"message"`
 | 
			
		||||
	RoomID    string `json:"room_id"`
 | 
			
		||||
	StartDate string `json:"start_date"`
 | 
			
		||||
	EndDate   string `json:"end_date"`
 | 
			
		||||
	OK        bool   `json:"ok"`
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// AvailabilityJSON is the search for availability page handler
 | 
			
		||||
@ -661,6 +661,40 @@ func (m *Repository) AdminReservationsCalendar(w http.ResponseWriter, r *http.Re
 | 
			
		||||
 | 
			
		||||
	data["rooms"] = rooms
 | 
			
		||||
 | 
			
		||||
	for _, x := range rooms {
 | 
			
		||||
		// create maps
 | 
			
		||||
		reservationMap := make(map[string]int)
 | 
			
		||||
		blockMap := make(map[string]int)
 | 
			
		||||
 | 
			
		||||
		for d := firstOfMonth; !d.After(lastOfMonth); d = d.AddDate(0, 0, 1) {
 | 
			
		||||
			reservationMap[d.Format("2006-01-2")] = 0
 | 
			
		||||
			blockMap[d.Format("2006-01-2")] = 0
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// get all the restrictions for the current room
 | 
			
		||||
		restrictions, err := m.DB.GetRestrictionsForRoomByDate(x.ID, firstOfMonth, lastOfMonth)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			helpers.ServerError(w, err)
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		for _, y := range restrictions {
 | 
			
		||||
			if y.ReservationID > 0 {
 | 
			
		||||
				// it's a reservation
 | 
			
		||||
				for d := y.StartDate; !d.After(y.EndDate); d = d.AddDate(0, 0, 1) {
 | 
			
		||||
					reservationMap[d.Format("2006-01-2")] = y.ReservationID
 | 
			
		||||
				}
 | 
			
		||||
			} else {
 | 
			
		||||
				// it's a block
 | 
			
		||||
				blockMap[y.StartDate.Format("2006-01-2")] = y.ReservationID
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		data[fmt.Sprintf("reservation_map_%d", x.ID)] = reservationMap
 | 
			
		||||
		data[fmt.Sprintf("block_map_%d", x.ID)] = blockMap
 | 
			
		||||
 | 
			
		||||
		m.App.Session.Put(r.Context(), fmt.Sprintf("block_map_%d", x.ID), blockMap)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	render.Template(w, r, "admin-reservations-calendar.page.tmpl",
 | 
			
		||||
		&models.TemplateData{
 | 
			
		||||
			StringMap: stringMap,
 | 
			
		||||
 | 
			
		||||
@ -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