Skip to content

Commit

Permalink
update reservation
Browse files Browse the repository at this point in the history
  • Loading branch information
Pomog committed Dec 29, 2023
1 parent cae714f commit c040ec4
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 4 deletions.
1 change: 1 addition & 0 deletions cmd/web/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 41 additions & 2 deletions internal/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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])
Expand Down Expand Up @@ -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)
}

59 changes: 59 additions & 0 deletions internal/repository/dbrepo/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
15 changes: 15 additions & 0 deletions internal/repository/dbrepo/testRepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
7 changes: 5 additions & 2 deletions internal/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit c040ec4

Please sign in to comment.