diff --git a/data/crud.go b/data/crud.go index f0a5b3c..d8eb762 100644 --- a/data/crud.go +++ b/data/crud.go @@ -28,6 +28,7 @@ func Shutdown() { } func SelectJson[T any](c *fiber.Ctx, err error, sql string, args ...interface{}) error { + // BUG: add a precondition that aborts if array of interfaces to avoid "unsupported $1 array of interfaces" var result []T if err != nil { goto fail } diff --git a/data/models.go b/data/models.go index 2c59352..88c491d 100644 --- a/data/models.go +++ b/data/models.go @@ -25,9 +25,30 @@ type NullExample struct { HasMaybe null.Int `db:"replying_to" validate:"omitempty,numeric"` } +type Product struct { + Id int `db:"id" validate:"numeric"` + Title string `db:"title" validate:"required"` + Description string `db:"description" validate:"required"` + Price float64 `db:"price" validate:"required"` + Slug string `db:"slug" validate:"required"` +} + +type CartItem struct { + Id int `db:"id" validate:"numeric"` + UserId int `db:"user_id" validate:"numeric,required"` + ProductId int `db:"product_id" validate:"required"` + Quantity int `db:"quantity" validate:"numeric"` +} + +type Cart struct { + Items []CartItem +} + func Models() map[string]reflect.Type { return map[string]reflect.Type{ "user": reflect.TypeFor[User](), + "product": reflect.TypeFor[Product](), + "cart_item": reflect.TypeFor[CartItem](), "null_example": reflect.TypeFor[NullExample](), } } diff --git a/features/init.go b/features/init.go index 16e7c88..08322d3 100644 --- a/features/init.go +++ b/features/init.go @@ -2,11 +2,13 @@ package features import ( "github.com/gofiber/fiber/v2" - email "MY/webapp/features/email" - paypal "MY/webapp/features/paypal" + "MY/webapp/features/email" + "MY/webapp/features/paypal" + "MY/webapp/features/shopping" ) func Setup(app *fiber.App) { - email.Setup(app) - paypal.Setup(app) + features_email.Setup(app) + features_paypal.Setup(app) + features_shopping.Setup(app) } diff --git a/migrations/20260112190943_shopping_init.sql b/migrations/20260112190943_shopping_init.sql new file mode 100644 index 0000000..07fa190 --- /dev/null +++ b/migrations/20260112190943_shopping_init.sql @@ -0,0 +1,13 @@ +-- +goose Up +-- +goose StatementBegin +CREATE TABLE Product (id INTEGER PRIMARY KEY, + title TEXT, + description TEXT, + price float, + slug TEXT UNIQUE); +-- +goose StatementEnd + +-- +goose Down +-- +goose StatementBegin +DROP TABLE Product; +-- +goose StatementEnd diff --git a/migrations/20260112195518_shopping_cart.sql b/migrations/20260112195518_shopping_cart.sql new file mode 100644 index 0000000..9bd2802 --- /dev/null +++ b/migrations/20260112195518_shopping_cart.sql @@ -0,0 +1,13 @@ +-- +goose Up +-- +goose StatementBegin +CREATE TABLE Cart ( + id INTEGER PRIMARY KEY, + user_id INTEGER NOT NULL, + product_id INTEGER NOT NULL, + quantity INTEGER); +-- +goose StatementEnd + +-- +goose Down +-- +goose StatementBegin +DROP TABLE Cart; +-- +goose StatementEnd