Editing a reservation

This commit is contained in:
vinchent 2024-07-25 13:29:57 +02:00
parent d4cf44bb22
commit ca1e72c676
3 changed files with 49 additions and 2 deletions

View File

@ -46,6 +46,7 @@ func routes(app *config.AppConfig) http.Handler {
mux.Get("/reservations-calendar", handlers.Repo.AdminReservationsCalendar)
mux.Get("/reservations/{src}/{id}", handlers.Repo.AdminShowReservation)
mux.Post("/reservations/{src}/{id}", handlers.Repo.AdminPostShowReservation)
})
return mux

View File

@ -545,6 +545,7 @@ func (m *Repository) AdminShowReservation(w http.ResponseWriter, r *http.Request
id, err := strconv.Atoi(exploded[4])
if err != nil {
helpers.ServerError(w, err)
return
}
src := exploded[3]
@ -555,6 +556,7 @@ func (m *Repository) AdminShowReservation(w http.ResponseWriter, r *http.Request
res, err := m.DB.GetReservationByID(id)
if err != nil {
helpers.ServerError(w, err)
return
}
data := make(map[string]interface{})
@ -568,6 +570,48 @@ func (m *Repository) AdminShowReservation(w http.ResponseWriter, r *http.Request
})
}
// AdminShowReservation shows the detail of a reservation
func (m *Repository) AdminPostShowReservation(w http.ResponseWriter, r *http.Request) {
if err := r.ParseForm(); err != nil {
helpers.ServerError(w, err)
return
}
exploded := strings.Split(r.RequestURI, "/")
id, err := strconv.Atoi(exploded[4])
if err != nil {
helpers.ServerError(w, err)
return
}
src := exploded[3]
stringMap := make(map[string]string)
stringMap["src"] = src
// get reservation from the database
res, err := m.DB.GetReservationByID(id)
if err != nil {
helpers.ServerError(w, err)
return
}
res.FirstName = r.Form.Get("first_name")
res.LastName = r.Form.Get("last_name")
res.Email = r.Form.Get("email")
res.Phone = r.Form.Get("phone")
// TODO error checking
err = m.DB.UpdateReservation(res)
if err != nil {
helpers.ServerError(w, err)
return
}
m.App.Session.Put(r.Context(), "flash", "Changes saved")
http.Redirect(w, r, fmt.Sprintf("/admin/reservations-%s", src), http.StatusSeeOther)
}
// AdminReservationsCalendar displays the reservation calendar
func (m *Repository) AdminReservationsCalendar(w http.ResponseWriter, r *http.Request) {
render.Template(w, r, "admin-reservations-calendar.page.tmpl", &models.TemplateData{})

View File

@ -348,9 +348,11 @@ func (m *postgresDBRepo) UpdateReservation(r models.Reservation) error {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
query := `update reservations set first_name = $1, last_name = $2, email = $3, phone = $4, updated_at = $5`
query := `update reservations set first_name = $1, last_name = $2, email = $3, phone = $4, updated_at = $5
where id = $6`
_, err := m.DB.ExecContext(ctx, query, r.FirstName, r.LastName, r.Email, r.Phone, time.Now())
_, err := m.DB.ExecContext(ctx, query, r.FirstName, r.LastName, r.Email,
r.Phone, time.Now(), r.ID)
if err != nil {
return err
}