reservation calendar 2
This commit is contained in:
@ -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,
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -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")
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
Reference in New Issue
Block a user