mirror of
https://github.com/Fluffy-Bean/TastyBites.git
synced 2025-06-21 17:36:16 +00:00
Start building out the backend with database
This commit is contained in:
parent
17476d3f2c
commit
677b4277a6
11 changed files with 241 additions and 63 deletions
29
api/api.go
29
api/api.go
|
@ -3,7 +3,9 @@ package api
|
|||
import (
|
||||
"net/http"
|
||||
|
||||
db "github.com/Fluffy-Bean/TastyBites/database"
|
||||
"github.com/Fluffy-Bean/TastyBites/front"
|
||||
sb "github.com/huandu/go-sqlbuilder"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v4/middleware"
|
||||
)
|
||||
|
@ -24,9 +26,30 @@ func Serve(c Config) {
|
|||
|
||||
r.StaticFS("/", front.DistDir)
|
||||
|
||||
api := r.Group("/api")
|
||||
api.GET("/items", func(e echo.Context) error {
|
||||
return e.JSON(http.StatusOK, sampleData)
|
||||
apiGroup := r.Group("/api")
|
||||
apiGroup.GET("/items", func(e echo.Context) error {
|
||||
builder := db.ItemStruct.SelectFrom("Item").Select("*")
|
||||
query, args := builder.BuildWithFlavor(sb.SQLite)
|
||||
|
||||
rows, err := db.Conn.Query(query, args...)
|
||||
defer rows.Close()
|
||||
if err != nil {
|
||||
r.Logger.Fatal(err)
|
||||
return e.String(http.StatusInternalServerError, "Could not query for data")
|
||||
}
|
||||
|
||||
var items []db.Item
|
||||
for rows.Next() {
|
||||
var item db.Item
|
||||
err := rows.Scan(db.ItemStruct.Addr(&item)...)
|
||||
if err != nil {
|
||||
r.Logger.Fatal(err)
|
||||
return e.String(http.StatusInternalServerError, "Could not scan row")
|
||||
}
|
||||
items = append(items, item)
|
||||
}
|
||||
|
||||
return e.JSON(http.StatusOK, items)
|
||||
})
|
||||
|
||||
r.HideBanner = true
|
||||
|
|
|
@ -1,59 +0,0 @@
|
|||
package api
|
||||
|
||||
type menuItem struct {
|
||||
Name string `json:"name"`
|
||||
Price float32 `json:"price"`
|
||||
Labels []string `json:"labels"`
|
||||
Image string `json:"image"`
|
||||
}
|
||||
|
||||
var sampleData = []menuItem{
|
||||
{
|
||||
Name: "Bar of Soap",
|
||||
Price: 69.99,
|
||||
Labels: []string{"vegan", "spicy"},
|
||||
},
|
||||
{
|
||||
Name: "Sock",
|
||||
Price: 21,
|
||||
Labels: []string{"vegan", "fish", "nut", "spicy"},
|
||||
},
|
||||
{
|
||||
Name: "Brick",
|
||||
Price: 0,
|
||||
Labels: []string{"spicy"},
|
||||
},
|
||||
{
|
||||
Name: "Toast",
|
||||
Price: 4382749832743,
|
||||
Labels: []string{"gluten"},
|
||||
},
|
||||
{
|
||||
Name: "water",
|
||||
Price: 1,
|
||||
Labels: []string{"fish"},
|
||||
},
|
||||
{
|
||||
Name: "half eaten mouldy bread",
|
||||
Price: -9999,
|
||||
Labels: []string{"nut"},
|
||||
},
|
||||
{
|
||||
Name: "GwaGwa",
|
||||
Price: 69,
|
||||
Labels: []string{"nut"},
|
||||
Image: "/dab.jpg",
|
||||
},
|
||||
{
|
||||
Name: "Hogermellon",
|
||||
Price: 1111,
|
||||
Labels: []string{"fish"},
|
||||
Image: "/wathog.jpg",
|
||||
},
|
||||
{
|
||||
Name: "Blue HOGGGGG",
|
||||
Price: 0,
|
||||
Labels: []string{"nut", "gluten", "spicy"},
|
||||
Image: "/sonichog.jpg",
|
||||
},
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue