From c040ec4067f5fe48890df5ea4571ae64f88802a8 Mon Sep 17 00:00:00 2001 From: Pomog Date: Fri, 29 Dec 2023 18:29:08 +0200 Subject: [PATCH] update reservation --- cmd/web/routes.go | 1 + internal/handlers/handlers.go | 43 ++++++++++++++++++- internal/repository/dbrepo/postgres.go | 59 ++++++++++++++++++++++++++ internal/repository/dbrepo/testRepo.go | 15 +++++++ internal/repository/repository.go | 7 ++- 5 files changed, 121 insertions(+), 4 deletions(-) diff --git a/cmd/web/routes.go b/cmd/web/routes.go index 53459d9..b820bfe 100644 --- a/cmd/web/routes.go +++ b/cmd/web/routes.go @@ -51,6 +51,7 @@ func routes(app *config.AppConfig) http.Handler { mux.Get("/reservations-calendar", handlers.Repo.AdminCalendarReservations) mux.Get("/reservations/{src}/{id}",handlers.Repo.AdminShowReservation ) + mux.Post("/reservations/{src}/{id}",handlers.Repo.AdminPostShowReservation ) }) return mux diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go index be43e37..8ba1ce3 100644 --- a/internal/handlers/handlers.go +++ b/internal/handlers/handlers.go @@ -465,7 +465,8 @@ func (m *Repository) PostShowLogin(w http.ResponseWriter, r *http.Request) { err := r.ParseForm() if err != nil { - log.Println(err) + helpers.ServerError(w, err) + return } form := forms.New(r.PostForm) @@ -541,7 +542,6 @@ func (m *Repository) AdminCalendarReservations(w http.ResponseWriter, r *http.Re // AdminShowReservation shows the reservations in the admin tool func (m *Repository) AdminShowReservation(w http.ResponseWriter, r *http.Request) { - // get URL exploded := strings.Split(r.RequestURI, "/") id, err := strconv.Atoi(exploded[4]) @@ -570,3 +570,42 @@ func (m *Repository) AdminShowReservation(w http.ResponseWriter, r *http.Request Form: forms.New(nil), }) } + +// AdminPostShowReservation shows the reservations in the admin tool +func (m *Repository) AdminPostShowReservation(w http.ResponseWriter, r *http.Request) { + err := r.ParseForm() + if err != nil { + helpers.ServerError(w, err) + return + } + + // get URL + exploded := strings.Split(r.RequestURI, "/") + id, err := strconv.Atoi(exploded[4]) + if err != nil { + helpers.ServerError(w, err) + return + } + src := exploded[3] + + 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") + + 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) +} + diff --git a/internal/repository/dbrepo/postgres.go b/internal/repository/dbrepo/postgres.go index 8ed3dd9..5c93b62 100644 --- a/internal/repository/dbrepo/postgres.go +++ b/internal/repository/dbrepo/postgres.go @@ -377,3 +377,62 @@ func (m *postgresDBRepo) GetReservationByID(id int) (models.Reservation, error) return i, nil } + +// UpdateReservation updates the reservation by reservation model in the database +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 + where id = $6 + ` + _, err := m.DB.ExecContext(ctx, query, + r.FirstName, r.LastName, r.Email, r.Phone, time.Now(), r.ID, + ) + + if err != nil { + return err + } + return nil +} + + +// DeleteReservation delete the reservation by ID +func (m *postgresDBRepo) DeleteReservation (id int) error { + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + defer cancel() + + query := ` + delete from reservations where id = $1 + ` + + _, err := m.DB.ExecContext(ctx, query, id) + + if err != nil { + return err + } + return nil +} + +// UpdateProcessedForReservation updates processed for a reservation by ID +func (m *postgresDBRepo) UpdateProcessedForReservation (id, processed int) error { + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + defer cancel() + + query := ` + update reservations set processed = $1 where id = $2 + ` + + _, err := m.DB.ExecContext(ctx, query, processed, id) + + if err != nil { + return err + } + return nil +} diff --git a/internal/repository/dbrepo/testRepo.go b/internal/repository/dbrepo/testRepo.go index ca3dace..998073c 100644 --- a/internal/repository/dbrepo/testRepo.go +++ b/internal/repository/dbrepo/testRepo.go @@ -77,3 +77,18 @@ func (m *testDBRepo) GetReservationByID(id int) (models.Reservation, error) { return i, nil } + +// UpdateReservation updates the reservation by reservation model in the database +func (m *testDBRepo) UpdateReservation(r models.Reservation) error { + return nil +} + +// DeleteReservation delete the reservation by ID +func (m *testDBRepo) DeleteReservation (id int) error { + return nil +} + +// UpdateProcessedForReservation updates processed for a reservation by ID +func (m *testDBRepo) UpdateProcessedForReservation (id, processed int) error { + return nil +} diff --git a/internal/repository/repository.go b/internal/repository/repository.go index fd3cb37..cdb42b1 100644 --- a/internal/repository/repository.go +++ b/internal/repository/repository.go @@ -15,10 +15,13 @@ type DatabaseRepo interface { GetRoomById(id int) (models.Room, error) GetUserByID(id int) (models.User, error) - UpdateUser(u models.User) (error) + UpdateUser(u models.User) error Autenticate(email, testPassword string) (int, string, error) - + AllReservations() ([]models.Reservation, error) AllNewReservations() ([]models.Reservation, error) GetReservationByID(id int) (models.Reservation, error) + UpdateReservation(r models.Reservation) error + DeleteReservation(id int) error + UpdateProcessedForReservation(id, processed int) error }