diff --git a/.air.toml b/.air.toml index 2981286..da27e7a 100644 --- a/.air.toml +++ b/.air.toml @@ -3,7 +3,7 @@ testdata_dir = "testdata" tmp_dir = "tmp" [build] - args_bin = ["run", "-host=127.0.0.1:3000"] + args_bin = ["run", "-host=127.0.0.1:3000", "-log"] bin = "./tmp/main" cmd = "go build -o ./tmp/main ." delay = 1000 diff --git a/api/api.go b/api/api.go index 52ea8f2..374b12f 100644 --- a/api/api.go +++ b/api/api.go @@ -15,6 +15,11 @@ type Config struct { Logging bool } +type JSONResponse struct { + Data interface{} `json:"data,omitempty"` + Error string `json:"error,omitempty"` +} + func Serve(c Config) { r := echo.New() @@ -28,6 +33,11 @@ func Serve(c Config) { apiGroup := r.Group("/api") apiGroup.GET("/items", func(e echo.Context) error { + var response JSONResponse + type ItemResponse struct { + Item []db.Item `json:"item"` + } + builder := db.ItemStruct.SelectFrom("Item").Select("*") query, args := builder.BuildWithFlavor(sb.SQLite) @@ -35,7 +45,8 @@ func Serve(c Config) { defer rows.Close() if err != nil { r.Logger.Fatal(err) - return e.String(http.StatusInternalServerError, "Could not query for data") + response.Error = "Could not query for data" + return e.JSON(http.StatusInternalServerError, response) } var items []db.Item @@ -44,12 +55,16 @@ func Serve(c Config) { err := rows.Scan(db.ItemStruct.Addr(&item)...) if err != nil { r.Logger.Fatal(err) - return e.String(http.StatusInternalServerError, "Could not scan row") + response.Error = "Failed to map response" + return e.JSON(http.StatusInternalServerError, response) } items = append(items, item) } + response.Data = ItemResponse{ + Item: items, + } - return e.JSON(http.StatusOK, items) + return e.JSON(http.StatusOK, response) }) r.HideBanner = true diff --git a/database/tables.go b/database/tables.go index 1ea0f17..7b5b4e5 100644 --- a/database/tables.go +++ b/database/tables.go @@ -5,12 +5,12 @@ import ( ) type Item struct { - UUID string `json:"uuid" db:"uuid"` - Name string `json:"name" db:"name"` - Price int64 `json:"price" db:"price"` - Description string `json:"description,omitempty" db:"description"` - //Labels []string `json:"labels,omitempty" db:"labels"` - //Images []string `json:"images,omitempty" db:"images"` + UUID string `json:"uuid" db:"uuid"` + Name string `json:"name" db:"name"` + Price int64 `json:"price" db:"price"` + Description string `json:"description" db:"description"` + Labels []string `json:"labels,omitempty" db:"-"` + Images []string `json:"images,omitempty" db:"-"` } var ItemStruct = sb.NewStruct(new(Item)) diff --git a/front/src/components/MenuItem.svelte b/front/src/components/MenuItem.svelte index b79d52d..7777203 100644 --- a/front/src/components/MenuItem.svelte +++ b/front/src/components/MenuItem.svelte @@ -23,26 +23,28 @@