mirror of
https://github.com/Fluffy-Bean/TastyBites.git
synced 2024-12-26 09:26:24 +00:00
Test out Item table
Add to real API Update air config Fix bug in MenuItem
This commit is contained in:
parent
899867e72d
commit
1fdb6ed2f4
|
@ -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
|
||||
|
|
21
api/api.go
21
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
|
||||
|
|
|
@ -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))
|
||||
|
|
|
@ -23,26 +23,28 @@
|
|||
<svelte:window on:resize={keepSquare}></svelte:window>
|
||||
|
||||
<div class="menu-item" bind:this={element}>
|
||||
{#if item.images}
|
||||
{#if item.images && item.images[0]}
|
||||
<img src={item.images[0]} alt="" class="menu-item-image">
|
||||
{:else}
|
||||
<img src={LoadingImage} alt="" class="menu-item-image">
|
||||
{/if}
|
||||
|
||||
<ul class="menu-item-labels">
|
||||
{#each item.labels as label}
|
||||
{#if label === Labels.vegan}
|
||||
<li class="vegan"><Leaf weight="fill" /></li>
|
||||
{:else if label === Labels.fish}
|
||||
<li class="fish"><Fish weight="fill" /></li>
|
||||
{:else if label === Labels.nut}
|
||||
<li class="nut"><Acorn weight="fill" /></li>
|
||||
{:else if label === Labels.gluten}
|
||||
<li class="gluten"><GrainsSlash weight="fill" /></li>
|
||||
{:else if label === Labels.spicy}
|
||||
<li class="spicy"><Pepper weight="fill" /></li>
|
||||
{/if}
|
||||
{/each}
|
||||
{#if item.labels}
|
||||
{#each item.labels as label}
|
||||
{#if label === Labels.vegan}
|
||||
<li class="vegan"><Leaf weight="fill" /></li>
|
||||
{:else if label === Labels.fish}
|
||||
<li class="fish"><Fish weight="fill" /></li>
|
||||
{:else if label === Labels.nut}
|
||||
<li class="nut"><Acorn weight="fill" /></li>
|
||||
{:else if label === Labels.gluten}
|
||||
<li class="gluten"><GrainsSlash weight="fill" /></li>
|
||||
{:else if label === Labels.spicy}
|
||||
<li class="spicy"><Pepper weight="fill" /></li>
|
||||
{/if}
|
||||
{/each}
|
||||
{/if}
|
||||
</ul>
|
||||
|
||||
<a class="menu-item-link" href="/item/{item.uuid}" use:link>
|
||||
|
|
|
@ -1,29 +0,0 @@
|
|||
import TestData from "%/lib/test-data.ts";
|
||||
|
||||
export async function getPopularToday() {
|
||||
const res = await fetch("/api/items");
|
||||
const data = res.json();
|
||||
|
||||
if (res.ok) {
|
||||
return data;
|
||||
} else {
|
||||
throw new Error("Failed to fetch popular today");
|
||||
}
|
||||
}
|
||||
|
||||
export function getMenuItems() {
|
||||
return [
|
||||
{
|
||||
name: "Main Menu",
|
||||
items: TestData,
|
||||
},
|
||||
{
|
||||
name: "Breakfast",
|
||||
items: [],
|
||||
},
|
||||
{
|
||||
name: "Seasonal",
|
||||
items: TestData,
|
||||
},
|
||||
];
|
||||
}
|
36
front/src/lib/api.ts
Normal file
36
front/src/lib/api.ts
Normal file
|
@ -0,0 +1,36 @@
|
|||
import { type Item, type JSONResponse } from "./types";
|
||||
import TestData from "./test-data";
|
||||
|
||||
const API_URL = "http://127.0.0.1:8080";
|
||||
|
||||
export async function getPopularToday(): Promise<Item[]> {
|
||||
const response = await fetch(API_URL + "/api/items");
|
||||
const {data, error}: JSONResponse = await response.json();
|
||||
|
||||
if (response.ok) {
|
||||
if (data?.item) {
|
||||
return data?.item;
|
||||
} else {
|
||||
return Promise.reject(new Error("Failed to fetch popular today"))
|
||||
}
|
||||
} else {
|
||||
return Promise.reject(error)
|
||||
}
|
||||
}
|
||||
|
||||
export function getMenuItems() {
|
||||
return [
|
||||
{
|
||||
name: "Main Menu",
|
||||
items: TestData,
|
||||
},
|
||||
{
|
||||
name: "Breakfast",
|
||||
items: [],
|
||||
},
|
||||
{
|
||||
name: "Seasonal",
|
||||
items: TestData,
|
||||
},
|
||||
];
|
||||
}
|
|
@ -6,17 +6,22 @@ export enum Labels {
|
|||
gluten = "GLUTEN",
|
||||
}
|
||||
|
||||
export interface Item {
|
||||
export type Item = {
|
||||
uuid: string;
|
||||
availability: boolean;
|
||||
availability?: boolean;
|
||||
name: string;
|
||||
price: number;
|
||||
labels: Labels[];
|
||||
description?: string;
|
||||
description: string;
|
||||
labels?: Labels[];
|
||||
images?: string[];
|
||||
}
|
||||
|
||||
export interface CartItem {
|
||||
export type CartItem = {
|
||||
amount: number;
|
||||
data: Item;
|
||||
}
|
||||
|
||||
export type JSONResponse = {
|
||||
data?: { item: Item[] }
|
||||
error?: string,
|
||||
}
|
Loading…
Reference in a new issue