Skip to content

Commit

Permalink
corrected redirections for editing reservation
Browse files Browse the repository at this point in the history
  • Loading branch information
jagottsicher committed Sep 22, 2023
1 parent b9539e7 commit c5637a9
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 12 deletions.
6 changes: 3 additions & 3 deletions cmd/web/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ func routes(app *config.AppConfig) http.Handler {
mux.Get("/reservations-all", handlers.Repo.AdminAllReservations)
mux.Get("/reservations-calendar", handlers.Repo.AdminReservationsCalendar)
mux.Post("/reservations-calendar", handlers.Repo.AdminPostReservationsCalendar)
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.Get("/process-reservation/{src}/{id}", handlers.Repo.AdminProcessReservation)
mux.Get("/delete-reservation/{src}/{id}", handlers.Repo.AdminDeleteReservation)
mux.Get("/process-reservation/{src}/{id}/do", handlers.Repo.AdminProcessReservation)
mux.Get("/delete-reservation/{src}/{id}/do", handlers.Repo.AdminDeleteReservation)
})

fileServer := http.FileServer(http.Dir("./static/"))
Expand Down
35 changes: 31 additions & 4 deletions internal/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,12 @@ 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["month"] = month
stringMap["year"] = year

render.Template(w, r, "admin-reservations-show-page.tpml", &models.TemplateData{
StringMap: stringMap,
Data: data,
Expand Down Expand Up @@ -729,9 +735,16 @@ func (m *Repository) AdminPostShowReservation(w http.ResponseWriter, r *http.Req
return
}

m.App.Session.Put(r.Context(), "success", "Changes saved")
month := r.Form.Get("month")
year := r.Form.Get("year")

http.Redirect(w, r, fmt.Sprintf("/admin/reservations-%s", src), http.StatusSeeOther)
m.App.Session.Put(r.Context(), "success", "Changes successfully saved")

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)
}
}

// AdminProcessReservation changes the status of a reservation to processed
Expand All @@ -745,9 +758,16 @@ func (m *Repository) AdminProcessReservation(w http.ResponseWriter, r *http.Requ
log.Println(err)
}

year := r.URL.Query().Get("y")
month := r.URL.Query().Get("m")

m.App.Session.Put(r.Context(), "success", "Reservation successfully 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 from the database
Expand All @@ -758,9 +778,16 @@ func (m *Repository) AdminDeleteReservation(w http.ResponseWriter, r *http.Reque

_ = m.DB.DeleteReservation(id)

year := r.URL.Query().Get("y")
month := r.URL.Query().Get("m")

m.App.Session.Put(r.Context(), "success", "Reservation successfully deleted")

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 is the handler for post requests to the reservation calendar
Expand Down
2 changes: 1 addition & 1 deletion templates/admin-all-reservations-page.tpml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
{{range $res}}
<tr>
<td>{{.ID}}</td>
<td><a href="/admin/reservations/all/{{.ID}}">{{.FullName}}</a></td>
<td><a href="/admin/reservations/all/{{.ID}}/show">{{.FullName}}</a></td>
<td>{{.Bungalow.BungalowName}}</td>
<td>{{humanReadableDate .StartDate}}</td>
<td>{{humanReadableDate .EndDate}}</td>
Expand Down
2 changes: 1 addition & 1 deletion templates/admin-new-reservations-page.tpml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
{{range $res}}
<tr>
<td>{{.ID}}</td>
<td><a href="/admin/reservations/new/{{.ID}}">{{.FullName}}</a></td>
<td><a href="/admin/reservations/new/{{.ID}}/show">{{.FullName}}</a></td>
<td>{{.Bungalow.BungalowName}}</td>
<td>{{humanReadableDate .StartDate}}</td>
<td>{{humanReadableDate .EndDate}}</td>
Expand Down
2 changes: 1 addition & 1 deletion templates/admin-reservations-calendar-page.tpml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
{{range $index := iterate $dim}}
<td class="text-center">
{{if gt (index $reservations (printf "%s-%s-%d" $curYear $curMonth (add $index 1))) 0}}
<a href="/admin/reservations/calendar/{{index $reservations (printf "%s-%s-%d" $curYear $curMonth (add $index 1))}}"><span class="text-danger">R</span></a>
<a href="/admin/reservations/calendar/{{index $reservations (printf "%s-%s-%d" $curYear $curMonth (add $index 1))}}/show?y={{$curYear}}&m={{$curMonth}}"><span class="text-danger">R</span></a>
{{else}}
<input
{{if gt (index $blocks (printf "%s-%s-%d" $curYear $curMonth (add $index 1))) 0 }}
Expand Down
10 changes: 8 additions & 2 deletions templates/admin-reservations-show-page.tpml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

<form action="/admin/reservations/{{$src}}/{{$res.ID}}" method="POST" class="" novalidate>
<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-3">
<label for="full_name">Full Name:</label>
Expand Down Expand Up @@ -76,7 +78,9 @@
msg: 'Are you sure?',
callback: function (result) {
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"}}";
}
}
})
Expand All @@ -88,7 +92,9 @@
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"}}";
}
}
})
Expand Down

0 comments on commit c5637a9

Please sign in to comment.