From 563447b5cae3790caabc9d03c7b4bffac73ca98f Mon Sep 17 00:00:00 2001 From: Pomog Date: Mon, 8 Jan 2024 07:13:01 +0200 Subject: [PATCH] Fixing redirects --- cmd/web/routes.go | 6 +-- internal/handlers/handlers.go | 49 +++++++++++++++---- templates/admin-all-reservations.page.tmpl | 2 +- templates/admin-new-reservations.page.tmpl | 2 +- .../admin-reservations-calendar.page.tmpl | 2 +- templates/admin-reservations-show.page.tmpl | 12 ++++- 6 files changed, 55 insertions(+), 18 deletions(-) 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 {{$reservation.ID}} {{$reservation.FirstName}} - + {{$reservation.LastName}} diff --git a/templates/admin-new-reservations.page.tmpl b/templates/admin-new-reservations.page.tmpl index 89546f9..12ad3d2 100644 --- a/templates/admin-new-reservations.page.tmpl +++ b/templates/admin-new-reservations.page.tmpl @@ -39,7 +39,7 @@ All Reservations {{$reservation.Processed}} {{$reservation.FirstName}} - + {{$reservation.LastName}} diff --git a/templates/admin-reservations-calendar.page.tmpl b/templates/admin-reservations-calendar.page.tmpl index fcf2ead..d72ff76 100644 --- a/templates/admin-reservations-calendar.page.tmpl +++ b/templates/admin-reservations-calendar.page.tmpl @@ -57,7 +57,7 @@ {{range $index := iterate $dim}} {{if gt (index $reservations (printf "%s-%s-%d" $curYear $curMonth ($index))) 0}} - + R {{else}} diff --git a/templates/admin-reservations-show.page.tmpl b/templates/admin-reservations-show.page.tmpl index 9c2fb27..7ae962d 100644 --- a/templates/admin-reservations-show.page.tmpl +++ b/templates/admin-reservations-show.page.tmpl @@ -55,6 +55,8 @@ Reservations
+ +
@@ -101,7 +103,9 @@ Reservations {{else}} Cancel {{end}} + {{if eq $reservation.Processed 0}} Mark As Processed + {{end}}
Delete @@ -126,7 +130,9 @@ Reservations msg: 'Are you sure?', callback: function (result) { if (result !== false) { - window.location.href = "/admin/processes-reservation/{{$src}}/" + id; + window.location.href = "/admin/processes-reservation/{{$src}}/" + + id + + "/do?y={{index .StringMap "year"}}&m={{index .StringMap "month"}}"; } } }); @@ -140,7 +146,9 @@ Reservations msg: 'Are you sure?', callback: function (result) { if (result !== false) { - window.location.href = "/admin/delete-reservation/{{$src}}/" + id; + window.location.href = "/admin/delete-reservation/{{$src}}/" + + id + + "/do?y={{index .StringMap "year"}}&m={{index .StringMap "month"}}"; } } });