reservation calendar 2

This commit is contained in:
vinchent 2024-07-26 13:40:56 +02:00
parent a7cf9fe4f0
commit b97c6cba5c
6 changed files with 110 additions and 2 deletions

View File

@ -624,6 +624,9 @@ func (m *Repository) AdminReservationsCalendar(w http.ResponseWriter, r *http.Re
now = time.Date(year, time.Month(month), 1, 0, 0, 0, 0, time.UTC)
}
data := make(map[string]interface{})
data["now"] = now
next := now.AddDate(0, 1, 0)
last := now.AddDate(0, -1, 0)
@ -641,9 +644,28 @@ func (m *Repository) AdminReservationsCalendar(w http.ResponseWriter, r *http.Re
stringMap["this_month"] = now.Format("01")
stringMap["this_month_year"] = now.Format("2006")
// get the first and last days from the month
currentYear, currentMonth, _ := now.Date()
currentLocation := now.Location()
firstOfMonth := time.Date(currentYear, currentMonth, 1, 0, 0, 0, 0, currentLocation)
lastOfMonth := firstOfMonth.AddDate(0, 1, -1)
intMap := make(map[string]int)
intMap["days_in_month"] = lastOfMonth.Day()
rooms, err := m.DB.AllRooms()
if err != nil {
helpers.ServerError(w, err)
return
}
data["rooms"] = rooms
render.Template(w, r, "admin-reservations-calendar.page.tmpl",
&models.TemplateData{
StringMap: stringMap,
Data: data,
IntMap: intMap,
})
}

View File

@ -16,7 +16,10 @@ import (
)
var functions = template.FuncMap{
"humanDate": HumanDate,
"humanDate": HumanDate,
"formatDate": FormatDate,
"iterate": Iterate,
"add": Add,
}
var (
@ -34,6 +37,24 @@ func HumanDate(t time.Time) string {
return t.Format("2006-01-02")
}
func FormatDate(t time.Time, f string) string {
return t.Format(f)
}
// Iterate returns a slice of ints, starting at 1, going to count
func Iterate(count int) []int {
var i int
var items []int
for i = 0; i < count; i++ {
items = append(items, i)
}
return items
}
func Add(a, b int) int {
return a + b
}
// AddDefaultData adds default template data
func AddDefaultData(td *models.TemplateData, r *http.Request) *models.TemplateData {
td.Flash = app.Session.PopString(r.Context(), "flash")

View File

@ -386,3 +386,36 @@ func (m *postgresDBRepo) UpdateProcessedForReservation(id, processed int) error
}
return nil
}
func (m *postgresDBRepo) AllRooms() ([]models.Room, error) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
var rooms []models.Room
query := `select id, room_name, created_at, updated_at from rooms order by room_name`
rows, err := m.DB.QueryContext(ctx, query)
if err != nil {
return rooms, err
}
defer rows.Close() // To avoid memory leak
for rows.Next() {
var rm models.Room
err := rows.Scan(
&rm.ID,
&rm.RoomName,
&rm.CreatedAt,
&rm.UpdatedAt,
)
if err != nil {
return rooms, err
}
rooms = append(rooms, rm)
}
if err = rows.Err(); err != nil {
return rooms, err
}
return rooms, nil
}

View File

@ -116,3 +116,9 @@ func (m *testDBRepo) DeleteReservation(id int) error {
func (m *testDBRepo) UpdateProcessedForReservation(id, processed int) error {
return nil
}
func (m *testDBRepo) AllRooms() ([]models.Room, error) {
var rooms []models.Room
return rooms, nil
}

View File

@ -22,4 +22,5 @@ type DatabaseRepo interface {
UpdateReservation(r models.Reservation) error
DeleteReservation(id int) error
UpdateProcessedForReservation(id, processed int) error
AllRooms() ([]models.Room, error)
}

View File

@ -5,9 +5,12 @@ Reservations Calendar
{{end}}
{{define "content"}}
{{$now := index .Data "now"}}
{{$rooms := index .Data "rooms"}}
{{$dim := index .IntMap "days_in_month"}}
<div class="col-md-12">
<div class="text-center">
<h3>{{index .StringMap "this_month"}} {{index .StringMap "this_month_year"}}</h3>
<h3>{{formatDate $now "January"}} {{formatDate $now "2006"}}</h3>
</div>
<div class="float-start">
@ -21,5 +24,27 @@ Reservations Calendar
</div>
<div class="clearfix"></div>
{{range $rooms}}
{{$roomId := .ID}}
<h4 class="mt-4">{{.RoomName}}</h4>
<div class="table-responsive">
<table class="table table-bordered table-sm">
<tr class="table-dark">
{{range $index := iterate $dim}}
<td class="text-center">
{{add $index 1}}
</td>
{{end}}
</tr>
<tr class="table-light">
{{range $index := iterate $dim}}
<td class="text-center">
<input type="checkbox" name="checked" value="">
</td>
{{end}}
</tr>
</table>
</div>
{{end}}
</div>
{{end}}