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 package main
import ( import (
"myapp/internal/models"
"net/http" "net/http"
"strconv"
"github.com/go-chi/chi/v5"
) )
func (app *application) VirtualTerminal(w http.ResponseWriter, r *http.Request) { 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 // ChargeOnce displays the page to buy one widget
func (app *application) ChargeOnce(w http.ResponseWriter, r *http.Request) { func (app *application) ChargeOnce(w http.ResponseWriter, r *http.Request) {
widget := models.Widget{ id := chi.URLParam(r, "id")
ID: 1, widgetID, _ := strconv.Atoi(id)
Name: "Custom Widget",
Description: "Paris 2024", widget, err := app.DB.GetWidget(widgetID)
InventoryLevel: 10, if err != nil {
Price: 1000, app.errorLog.Println(err)
} }
data := make(map[string]interface{}) data := make(map[string]interface{})

View File

@ -11,7 +11,7 @@ func (app *application) routes() http.Handler {
mux.Get("/virtual-terminal", app.VirtualTerminal) mux.Get("/virtual-terminal", app.VirtualTerminal)
mux.Post("/payment-succeeded", app.PaymentSucceeded) mux.Post("/payment-succeeded", app.PaymentSucceeded)
mux.Get("/charge-once", app.ChargeOnce) mux.Get("/widget/{id}", app.ChargeOnce)
fileServer := http.FileServer(http.Dir("./static")) fileServer := http.FileServer(http.Dir("./static"))
mux.Handle("/static/*", http.StripPrefix("/static", fileServer)) mux.Handle("/static/*", http.StripPrefix("/static", fileServer))

View File

@ -43,7 +43,7 @@
aria-expanded="false">Products</a> aria-expanded="false">Products</a>
<ul class="dropdown-menu"> <ul class="dropdown-menu">
<li> <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>
<li> <li>
<a class="dropdown-item" href="#">Subscription</a> <a class="dropdown-item" href="#">Subscription</a>

View File

@ -69,8 +69,8 @@ type Transaction struct {
Amount int `json:"amount"` Amount int `json:"amount"`
Currency string `json:"currency"` Currency string `json:"currency"`
LastFour string `json:"last_four"` LastFour string `json:"last_four"`
BankReturnCode string `json:bank_return_code` BankReturnCode string `json:"bank_return_code"`
TransactionStatusID int `json:transaction_status_id` TransactionStatusID int `json:"transaction_status_id"`
CreatedAt time.Time `json:"-"` CreatedAt time.Time `json:"-"`
UpdatedAt time.Time `json:"-"` UpdatedAt time.Time `json:"-"`
} }
@ -92,10 +92,22 @@ func (m *DBModel) GetWidget(id int) (Widget, error) {
var widget Widget 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) 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 { if err != nil {
return widget, err return widget, err
} }