Fixing redirects

This commit is contained in:
vinchent 2024-07-28 15:23:12 +02:00
parent d7f7a2d8d9
commit 623291541a
6 changed files with 43 additions and 11 deletions

View File

@ -45,10 +45,10 @@ func routes(app *config.AppConfig) http.Handler {
mux.Get("/reservations-all", handlers.Repo.AdminAllReservations) mux.Get("/reservations-all", handlers.Repo.AdminAllReservations)
mux.Get("/reservations-calendar", handlers.Repo.AdminReservationsCalendar) mux.Get("/reservations-calendar", handlers.Repo.AdminReservationsCalendar)
mux.Post("/reservations-calendar", handlers.Repo.AdminPostReservationsCalendar) mux.Post("/reservations-calendar", handlers.Repo.AdminPostReservationsCalendar)
mux.Get("/process-reservation/{src}/{id}", handlers.Repo.AdminProcessReservation) mux.Get("/process-reservation/{src}/{id}/do", handlers.Repo.AdminProcessReservation)
mux.Get("/delete-reservation/{src}/{id}", handlers.Repo.AdminDeleteReservation) mux.Get("/delete-reservation/{src}/{id}/do", handlers.Repo.AdminDeleteReservation)
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) mux.Post("/reservations/{src}/{id}", handlers.Repo.AdminPostShowReservation)
}) })

View File

@ -554,6 +554,12 @@ func (m *Repository) AdminShowReservation(w http.ResponseWriter, r *http.Request
stringMap := make(map[string]string) stringMap := make(map[string]string)
stringMap["src"] = src stringMap["src"] = src
year := r.URL.Query().Get("y")
month := r.URL.Query().Get("m")
stringMap["month"] = month
stringMap["year"] = year
// get reservation from the database // get reservation from the database
res, err := m.DB.GetReservationByID(id) res, err := m.DB.GetReservationByID(id)
if err != nil { if err != nil {
@ -610,8 +616,16 @@ func (m *Repository) AdminPostShowReservation(w http.ResponseWriter, r *http.Req
return return
} }
month := r.Form.Get("month")
year := r.Form.Get("year")
m.App.Session.Put(r.Context(), "flash", "Changes saved") m.App.Session.Put(r.Context(), "flash", "Changes saved")
http.Redirect(w, r, fmt.Sprintf("/admin/reservations-%s", src), http.StatusSeeOther) if year == "" {
log.Println(year, month)
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)
}
} }
// AdminReservationsCalendar displays the reservation calendar // AdminReservationsCalendar displays the reservation calendar
@ -711,9 +725,16 @@ func (m *Repository) AdminProcessReservation(w http.ResponseWriter, r *http.Requ
_ = m.DB.UpdateProcessedForReservation(id, 1) _ = m.DB.UpdateProcessedForReservation(id, 1)
year := r.URL.Query().Get("y")
month := r.URL.Query().Get("m")
m.App.Session.Put(r.Context(), "flash", "Reservation marked as processed") 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)
}
} }
// AdminDeleteReservation deletes a reservation // AdminDeleteReservation deletes a reservation
@ -723,9 +744,16 @@ func (m *Repository) AdminDeleteReservation(w http.ResponseWriter, r *http.Reque
_ = m.DB.DeleteReservation(id) _ = m.DB.DeleteReservation(id)
year := r.URL.Query().Get("y")
month := r.URL.Query().Get("m")
m.App.Session.Put(r.Context(), "flash", fmt.Sprintf("Reservation %d deleted", id)) m.App.Session.Put(r.Context(), "flash", fmt.Sprintf("Reservation %d deleted", id))
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)
}
} }
// AdminPostReservationsCalendar handles post of reservation calendar // AdminPostReservationsCalendar handles post of reservation calendar

View File

@ -26,7 +26,7 @@ All Reservations
<tr> <tr>
<td>{{ .ID}}</td> <td>{{ .ID}}</td>
<td> <td>
<a href="/admin/reservations/all/{{.ID}}"> <a href="/admin/reservations/all/{{.ID}}/show">
{{ .LastName}} {{ .LastName}}
</a> </a>
</td> </td>

View File

@ -26,7 +26,7 @@ New Reservations
<tr> <tr>
<td>{{ .ID}}</td> <td>{{ .ID}}</td>
<td> <td>
<a href="/admin/reservations/new/{{.ID}}"> <a href="/admin/reservations/new/{{.ID}}/show">
{{ .LastName}} {{ .LastName}}
</a> </a>
</td> </td>

View File

@ -49,7 +49,7 @@ Reservations Calendar
{{range $index := iterate $dim}} {{range $index := iterate $dim}}
<td class="text-center"> <td class="text-center">
{{if gt (index $reservations (printf "%s-%s-%d" $curYear $curMonth (add $index 1))) 0}} {{if gt (index $reservations (printf "%s-%s-%d" $curYear $curMonth (add $index 1))) 0}}
<a href="/admin/reservations/cal/{{index $reservations (printf "%s-%s-%d" $curYear $curMonth (add $index 1))}}"> <a href="/admin/reservations/cal/{{index $reservations (printf "%s-%s-%d" $curYear $curMonth (add $index 1))}}/show?y={{$curYear}}&m={{$curMonth}}">
<span class="text-danger">R</span> <span class="text-danger">R</span>
</a> </a>
{{else}} {{else}}

View File

@ -15,6 +15,8 @@ Reservation
</p> </p>
<form method="post" action="/admin/reservations/{{$src}}/{{$res.ID}}" class="" novalidate> <form method="post" action="/admin/reservations/{{$src}}/{{$res.ID}}" class="" novalidate>
<input type="hidden" name="csrf_token" value="{{.CSRFToken}}"> <input type="hidden" name="csrf_token" value="{{.CSRFToken}}">
<input type="hidden" name="year" value="{{index .StringMap "year"}}">
<input type="hidden" name="month" value="{{index .StringMap "month"}}">
<div class="form-group mt-5"> <div class="form-group mt-5">
<label for="first_name">First name:</label> <label for="first_name">First name:</label>
@ -55,7 +57,9 @@ Reservation
{{else}} {{else}}
<a href="/admin/reservations-{{$src}}" class="btn btn-warning">Cancel</a> <a href="/admin/reservations-{{$src}}" class="btn btn-warning">Cancel</a>
{{end}} {{end}}
{{ if eq $res.Processed 0}}
<a href="#" class="btn btn-info" onclick="processRes({{$res.ID}})">Mark as Processed</a> <a href="#" class="btn btn-info" onclick="processRes({{$res.ID}})">Mark as Processed</a>
{{end}}
</div> </div>
@ -78,7 +82,7 @@ function processRes(id) {
msg: 'Are you sure?', msg: 'Are you sure?',
callback: function(result) { callback: function(result) {
if (result != false) { if (result != false) {
window.location.href = "/admin/process-reservation/{{$src}}/" + id; window.location.href = "/admin/process-reservation/{{$src}}/" + id + "/do?y={{index .StringMap "year"}}&m={{index .StringMap "month"}}";
} }
} }
}) })
@ -89,7 +93,7 @@ function deleteRes(id) {
msg: 'Are you sure?', msg: 'Are you sure?',
callback: function(result) { callback: function(result) {
if (result != false) { 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"}}";
} }
} }
}) })