Skip to content

Commit

Permalink
Fixing redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
Pomog committed Jan 8, 2024
1 parent 7f1379e commit 563447b
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 18 deletions.
6 changes: 3 additions & 3 deletions cmd/web/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
})

Expand Down
49 changes: 39 additions & 10 deletions internal/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -744,22 +779,17 @@ 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)
if err != nil {
helpers.ServerError(w, err)
return
}
log.Println("deleted restriction :", value, " name: ", name)
}
}
}
Expand All @@ -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])
}
}

Expand Down
2 changes: 1 addition & 1 deletion templates/admin-all-reservations.page.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ All Reservations
<td>{{$reservation.ID}}</td>
<td>{{$reservation.FirstName}}</td>
<td>
<a href="/admin/reservations/all/{{$reservation.ID}}">
<a href="/admin/reservations/all/{{$reservation.ID}}/show">
{{$reservation.LastName}}
</a>
</td>
Expand Down
2 changes: 1 addition & 1 deletion templates/admin-new-reservations.page.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ All Reservations
<td>{{$reservation.Processed}}</td>
<td>{{$reservation.FirstName}}</td>
<td>
<a href="/admin/reservations/new/{{$reservation.ID}}">
<a href="/admin/reservations/new/{{$reservation.ID}}/show">
{{$reservation.LastName}}
</a>
</td>
Expand Down
2 changes: 1 addition & 1 deletion templates/admin-reservations-calendar.page.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
{{range $index := iterate $dim}}
<td class="text-center">
{{if gt (index $reservations (printf "%s-%s-%d" $curYear $curMonth ($index))) 0}}
<a href="/admin/reservations/cal/{{index $reservations (printf "%s-%s-%d" $curYear $curMonth ($index))}}">
<a href="/admin/reservations/cal/{{index $reservations (printf "%s-%s-%d" $curYear $curMonth ($index))}}/show?y={{$curYear}}&m={{$curMonth}}">
<span class="text-danger">R</span>

{{else}}
Expand Down
12 changes: 10 additions & 2 deletions templates/admin-reservations-show.page.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ Reservations

<form method="post" action="/admin/reservations/{{$src}}/{{$reservation.ID}}" 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="first_name">First Name:</label>
Expand Down Expand Up @@ -101,7 +103,9 @@ Reservations
{{else}}
<a href="/admin/reservations-{{$src}}" class="btn btn-warning">Cancel</a>
{{end}}
{{if eq $reservation.Processed 0}}
<a href="#!" class="btn btn-info" onclick="processRes({{$reservation.ID}})">Mark As Processed</a>
{{end}}
</div>
<div class="float-right">
<a href="#!" class="btn btn-danger" onclick="deleteRes({{$reservation.ID}})">Delete</a>
Expand All @@ -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"}}";
}
}
});
Expand All @@ -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"}}";
}
}
});
Expand Down

0 comments on commit 563447b

Please sign in to comment.