diff --git a/cmd/web/routes.go b/cmd/web/routes.go index 049d1e2..a157b92 100644 --- a/cmd/web/routes.go +++ b/cmd/web/routes.go @@ -50,11 +50,11 @@ func routes(app *config.AppConfig) http.Handler { mux.Get("/reservations-all", handlers.Repo.AdminAllReservations) mux.Get("/reservations-calendar", handlers.Repo.AdminCalendarReservations) mux.Post("/reservations-calendar", handlers.Repo.AdminPostCalendarReservations) - mux.Get("/processes-reservation/{src}/{id}", handlers.Repo.AdminProcessReservations) - mux.Get("/delete-reservation/{src}/{id}", handlers.Repo.AdminDeleteReservations) + mux.Get("/processes-reservation/{src}/{id}/do", handlers.Repo.AdminProcessReservations) + mux.Get("/delete-reservation/{src}/{id}/do", handlers.Repo.AdminDeleteReservations) - mux.Get("/reservations/{src}/{id}",handlers.Repo.AdminShowReservation ) + mux.Get("/reservations/{src}/{id}/show",handlers.Repo.AdminShowReservation ) mux.Post("/reservations/{src}/{id}",handlers.Repo.AdminPostShowReservation ) }) diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go index c9d5a91..8af3a50 100644 --- a/internal/handlers/handlers.go +++ b/internal/handlers/handlers.go @@ -640,6 +640,11 @@ func (m *Repository) AdminShowReservation(w http.ResponseWriter, r *http.Request stringMap := make(map[string]string) stringMap["src"] = src + year := r.URL.Query().Get("y") + month := r.URL.Query().Get("m") + stringMap["year"] = year + stringMap["month"] = month + // get reservation from the DB res, err := m.DB.GetReservationByID(id) if err != nil { @@ -691,8 +696,17 @@ func (m *Repository) AdminPostShowReservation(w http.ResponseWriter, r *http.Req return } + month := r.Form.Get("month") + year := r.Form.Get("year") + m.App.Session.Put(r.Context(), "flash", "Changes saved") - http.Redirect(w, r, fmt.Sprintf("/admin/reservations-%s", src), http.StatusSeeOther) + + if year == "" { + http.Redirect(w, r, fmt.Sprintf("/admin/reservations-%s", src), http.StatusSeeOther) + } else { + http.Redirect(w, r, fmt.Sprintf("/admin/reservations-calendar?y=%s&m=%s", year, month), http.StatusSeeOther) + } + } // AdminProcessReservations marks a reservation processed @@ -701,10 +715,24 @@ func (m *Repository) AdminProcessReservations(w http.ResponseWriter, r *http.Req id, _ := strconv.Atoi(chi.URLParam(r, "id")) src := chi.URLParam(r, "src") - _ = m.DB.UpdateProcessedForReservation(id, 1) + err := m.DB.UpdateProcessedForReservation(id, 1) + if err != nil { + helpers.ServerError(w, err) + return + } + + year := r.URL.Query().Get("y") + month := r.URL.Query().Get("m") + m.App.Session.Put(r.Context(), "flash", "Reservation marked as processed") - http.Redirect(w, r, fmt.Sprintf("/admin/reservations-%s", src), http.StatusSeeOther) + if year == "" { + http.Redirect(w, r, fmt.Sprintf("/admin/reservations-%s", src), http.StatusSeeOther) + } else { + http.Redirect(w, r, fmt.Sprintf("/admin/reservations-calendar?y=%s&m=%s", year, month), http.StatusSeeOther) + } + + } // AdminDeleteReservations deletes a reservation @@ -716,7 +744,14 @@ func (m *Repository) AdminDeleteReservations(w http.ResponseWriter, r *http.Requ _ = m.DB.DeleteReservation(id) m.App.Session.Put(r.Context(), "flash", "Reservation deleted") - http.Redirect(w, r, fmt.Sprintf("/admin/reservations-%s", src), http.StatusSeeOther) + year := r.URL.Query().Get("y") + month := r.URL.Query().Get("m") + + if year == "" { + http.Redirect(w, r, fmt.Sprintf("/admin/reservations-%s", src), http.StatusSeeOther) + } else { + http.Redirect(w, r, fmt.Sprintf("/admin/reservations-calendar?y=%s&m=%s", year, month), http.StatusSeeOther) + } } // AdminPostCalendarReservations reservations calendar POST handling @@ -744,14 +779,10 @@ func (m *Repository) AdminPostCalendarReservations(w http.ResponseWriter, r *htt // that does not exist in the posted data and restrictionID > 1, then it is a blok need to remove curMap := m.App.Session.Get(r.Context(), fmt.Sprintf("blockMap_map_%d", x.ID)).(map[string]int) for name, value := range curMap { - // ok will be false if value is not th the map if val, ok := curMap[name]; ok { - // only pay attention to val > 0, and than are not in the form post if val > 0 { - fmt.Println("name, data: ", name, "reservationID value: ", value) - fmt.Println("val: ", val) if !form.Has(fmt.Sprintf("remove_block_%d_%s", x.ID, name)) { // delete restriction by id err := m.DB.DeleteBlockForRoom(value) @@ -759,7 +790,6 @@ func (m *Repository) AdminPostCalendarReservations(w http.ResponseWriter, r *htt helpers.ServerError(w, err) return } - log.Println("deleted restriction :", value, " name: ", name) } } } @@ -779,7 +809,6 @@ func (m *Repository) AdminPostCalendarReservations(w http.ResponseWriter, r *htt helpers.ServerError(w, err) return } - log.Println("inserted restriction for roomId:", roomID, " at: ", exploded[3]) } } diff --git a/templates/admin-all-reservations.page.tmpl b/templates/admin-all-reservations.page.tmpl index c8aef57..bbd555d 100644 --- a/templates/admin-all-reservations.page.tmpl +++ b/templates/admin-all-reservations.page.tmpl @@ -37,7 +37,7 @@ All Reservations