Creating a database table for items for sale
This commit is contained in:
parent
90918f25ae
commit
ab4fda918a
@ -5,6 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"myapp/internal/driver"
|
"myapp/internal/driver"
|
||||||
|
"myapp/internal/models"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
@ -31,6 +32,7 @@ type application struct {
|
|||||||
infoLog *log.Logger
|
infoLog *log.Logger
|
||||||
errorLog *log.Logger
|
errorLog *log.Logger
|
||||||
version string
|
version string
|
||||||
|
DB models.DBModel
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *application) serve() error {
|
func (app *application) serve() error {
|
||||||
@ -87,6 +89,7 @@ func main() {
|
|||||||
config: cfg,
|
config: cfg,
|
||||||
infoLog: infoLog,
|
infoLog: infoLog,
|
||||||
errorLog: errorLog,
|
errorLog: errorLog,
|
||||||
|
DB: models.DBModel{DB: conn},
|
||||||
}
|
}
|
||||||
|
|
||||||
app.infoLog.Println("Connected to MariaDB")
|
app.infoLog.Println("Connected to MariaDB")
|
||||||
|
@ -5,6 +5,8 @@ import (
|
|||||||
"myapp/internal/cards"
|
"myapp/internal/cards"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/go-chi/chi/v5"
|
||||||
)
|
)
|
||||||
|
|
||||||
type stripePayload struct {
|
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.Header().Set("Content-Type", "application/json")
|
||||||
w.Write(out)
|
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.Post("/api/payment-intent", app.GetPaymentIntent)
|
||||||
|
mux.Get("/api/widget/{id}", app.GetWidgetByID)
|
||||||
return mux
|
return mux
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
"html/template"
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
"myapp/internal/driver"
|
"myapp/internal/driver"
|
||||||
|
"myapp/internal/models"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
@ -35,6 +36,7 @@ type application struct {
|
|||||||
errorLog *log.Logger
|
errorLog *log.Logger
|
||||||
templateCache map[string]*template.Template
|
templateCache map[string]*template.Template
|
||||||
version string
|
version string
|
||||||
|
DB models.DBModel
|
||||||
}
|
}
|
||||||
|
|
||||||
func (app *application) serve() error {
|
func (app *application) serve() error {
|
||||||
@ -95,6 +97,7 @@ func main() {
|
|||||||
errorLog: errorLog,
|
errorLog: errorLog,
|
||||||
templateCache: tc,
|
templateCache: tc,
|
||||||
version: version,
|
version: version,
|
||||||
|
DB: models.DBModel{DB: conn},
|
||||||
}
|
}
|
||||||
|
|
||||||
app.infoLog.Println("Connected to MariaDB")
|
app.infoLog.Println("Connected to MariaDB")
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@ -32,3 +33,19 @@ type Widget struct {
|
|||||||
CreatedAt time.Time `json:"-"`
|
CreatedAt time.Time `json:"-"`
|
||||||
UpdatedAt 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