From 85cd4dfc1f69cdf4681f9edafc9affef907bdd95 Mon Sep 17 00:00:00 2001 From: vinchent Date: Wed, 7 Aug 2024 11:56:53 +0200 Subject: [PATCH] Update db function and add to web-back --- cmd/web/handlers.go | 16 +++++++++------- cmd/web/routes.go | 2 +- cmd/web/templates/base.layout.gohtml | 2 +- internal/models/models.go | 20 ++++++++++++++++---- 4 files changed, 27 insertions(+), 13 deletions(-) diff --git a/cmd/web/handlers.go b/cmd/web/handlers.go index 0f7802b..7686bb5 100644 --- a/cmd/web/handlers.go +++ b/cmd/web/handlers.go @@ -1,8 +1,10 @@ package main import ( - "myapp/internal/models" "net/http" + "strconv" + + "github.com/go-chi/chi/v5" ) func (app *application) VirtualTerminal(w http.ResponseWriter, r *http.Request) { @@ -43,12 +45,12 @@ func (app *application) PaymentSucceeded(w http.ResponseWriter, r *http.Request) // ChargeOnce displays the page to buy one widget func (app *application) ChargeOnce(w http.ResponseWriter, r *http.Request) { - widget := models.Widget{ - ID: 1, - Name: "Custom Widget", - Description: "Paris 2024", - InventoryLevel: 10, - Price: 1000, + id := chi.URLParam(r, "id") + widgetID, _ := strconv.Atoi(id) + + widget, err := app.DB.GetWidget(widgetID) + if err != nil { + app.errorLog.Println(err) } data := make(map[string]interface{}) diff --git a/cmd/web/routes.go b/cmd/web/routes.go index e8fea37..d9c28f9 100644 --- a/cmd/web/routes.go +++ b/cmd/web/routes.go @@ -11,7 +11,7 @@ func (app *application) routes() http.Handler { mux.Get("/virtual-terminal", app.VirtualTerminal) mux.Post("/payment-succeeded", app.PaymentSucceeded) - mux.Get("/charge-once", app.ChargeOnce) + mux.Get("/widget/{id}", app.ChargeOnce) fileServer := http.FileServer(http.Dir("./static")) mux.Handle("/static/*", http.StripPrefix("/static", fileServer)) diff --git a/cmd/web/templates/base.layout.gohtml b/cmd/web/templates/base.layout.gohtml index 74f3e94..2e863a0 100644 --- a/cmd/web/templates/base.layout.gohtml +++ b/cmd/web/templates/base.layout.gohtml @@ -43,7 +43,7 @@ aria-expanded="false">Products