Update db function and add to web-back

This commit is contained in:
vinchent 2024-08-07 11:56:53 +02:00
parent d32667acd1
commit 85cd4dfc1f
4 changed files with 27 additions and 13 deletions

View File

@ -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{})

View File

@ -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))

View File

@ -43,7 +43,7 @@
aria-expanded="false">Products</a>
<ul class="dropdown-menu">
<li>
<a class="dropdown-item" href="/charge-once">Buy one widget</a>
<a class="dropdown-item" href="/widget/1">Buy one widget</a>
</li>
<li>
<a class="dropdown-item" href="#">Subscription</a>

View File

@ -69,8 +69,8 @@ type Transaction struct {
Amount int `json:"amount"`
Currency string `json:"currency"`
LastFour string `json:"last_four"`
BankReturnCode string `json:bank_return_code`
TransactionStatusID int `json:transaction_status_id`
BankReturnCode string `json:"bank_return_code"`
TransactionStatusID int `json:"transaction_status_id"`
CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"`
}
@ -92,10 +92,22 @@ func (m *DBModel) GetWidget(id int) (Widget, error) {
var widget Widget
query := `SELECT id, name FROM widgets WHERE id = ?`
query := `SELECT id, name, description, inventory_level, price,
coalesce(image, ''),
created_at, updated_at
FROM widgets WHERE id = ?;`
row := m.DB.QueryRowContext(ctx, query, id)
err := row.Scan(&widget.ID, &widget.Name)
err := row.Scan(
&widget.ID,
&widget.Name,
&widget.Description,
&widget.InventoryLevel,
&widget.Price,
&widget.Image,
&widget.CreatedAt,
&widget.UpdatedAt,
)
if err != nil {
return widget, err
}