mirror of
https://github.com/Fluffy-Bean/TastyBites.git
synced 2025-05-21 19:04:54 +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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue