Creating a database table for items for sale
This commit is contained in:
@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"myapp/internal/driver"
|
||||
"myapp/internal/models"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
@ -31,6 +32,7 @@ type application struct {
|
||||
infoLog *log.Logger
|
||||
errorLog *log.Logger
|
||||
version string
|
||||
DB models.DBModel
|
||||
}
|
||||
|
||||
func (app *application) serve() error {
|
||||
@ -87,6 +89,7 @@ func main() {
|
||||
config: cfg,
|
||||
infoLog: infoLog,
|
||||
errorLog: errorLog,
|
||||
DB: models.DBModel{DB: conn},
|
||||
}
|
||||
|
||||
app.infoLog.Println("Connected to MariaDB")
|
||||
|
@ -5,6 +5,8 @@ import (
|
||||
"myapp/internal/cards"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
type stripePayload struct {
|
||||
@ -65,3 +67,19 @@ func (app *application) GetPaymentIntent(w http.ResponseWriter, r *http.Request)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write(out)
|
||||
}
|
||||
|
||||
func (app *application) GetWidgetByID(w http.ResponseWriter, r *http.Request) {
|
||||
id := chi.URLParam(r, "id")
|
||||
widgetID, _ := strconv.Atoi(id)
|
||||
|
||||
widget, err := app.DB.GetWidget(widgetID)
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
}
|
||||
out, err := json.MarshalIndent(widget, "", " ")
|
||||
if err != nil {
|
||||
app.errorLog.Println(err)
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.Write(out)
|
||||
}
|
||||
|
@ -19,5 +19,6 @@ func (app *application) routes() http.Handler {
|
||||
}))
|
||||
|
||||
mux.Post("/api/payment-intent", app.GetPaymentIntent)
|
||||
mux.Get("/api/widget/{id}", app.GetWidgetByID)
|
||||
return mux
|
||||
}
|
||||
|
Reference in New Issue
Block a user