Creating a database table for items for sale
This commit is contained in:
parent
90918f25ae
commit
ab4fda918a
@ -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
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ import (
|
||||
"html/template"
|
||||
"log"
|
||||
"myapp/internal/driver"
|
||||
"myapp/internal/models"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
@ -35,6 +36,7 @@ type application struct {
|
||||
errorLog *log.Logger
|
||||
templateCache map[string]*template.Template
|
||||
version string
|
||||
DB models.DBModel
|
||||
}
|
||||
|
||||
func (app *application) serve() error {
|
||||
@ -95,6 +97,7 @@ func main() {
|
||||
errorLog: errorLog,
|
||||
templateCache: tc,
|
||||
version: version,
|
||||
DB: models.DBModel{DB: conn},
|
||||
}
|
||||
|
||||
app.infoLog.Println("Connected to MariaDB")
|
||||
|
@ -1,6 +1,7 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"time"
|
||||
)
|
||||
@ -32,3 +33,19 @@ type Widget struct {
|
||||
CreatedAt time.Time `json:"-"`
|
||||
UpdatedAt time.Time `json:"-"`
|
||||
}
|
||||
|
||||
func (m *DBModel) GetWidget(id int) (Widget, error) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
var widget Widget
|
||||
|
||||
query := `SELECT id, name FROM widgets WHERE id = ?`
|
||||
|
||||
row := m.DB.QueryRowContext(ctx, query, id)
|
||||
err := row.Scan(&widget.ID, &widget.Name)
|
||||
if err != nil {
|
||||
return widget, err
|
||||
}
|
||||
return widget, nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user