Reservation Calendar 3

This commit is contained in:
vinchent 2024-07-27 22:39:23 +02:00
parent b97c6cba5c
commit 7894a05daf
6 changed files with 85 additions and 1 deletions

View File

@ -53,6 +53,7 @@ func run() (*driver.DB, error) {
gob.Register(models.User{})
gob.Register(models.Room{})
gob.Register(models.Restriction{})
gob.Register(map[string]int{})
mailChan := make(chan models.MailData)
app.MailChan = mailChan

View File

@ -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,

View File

@ -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
}

View File

@ -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
}

View File

@ -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)
}

View File

@ -8,6 +8,8 @@ Reservations Calendar
{{$now := index .Data "now"}}
{{$rooms := index .Data "rooms"}}
{{$dim := index .IntMap "days_in_month"}}
{{$curMonth := index .StringMap "this_month"}}
{{$curYear := index .StringMap "this_month_year"}}
<div class="col-md-12">
<div class="text-center">
<h3>{{formatDate $now "January"}} {{formatDate $now "2006"}}</h3>