Clean up test API

This commit is contained in:
Michał Gdula 2024-05-14 13:31:25 +01:00
parent 0943c99749
commit 18b22e7928
3 changed files with 33 additions and 81 deletions

View file

@ -1,36 +1,16 @@
import { type Item, type JSONResponse } from "./types"; import { type Item, type JSONResponse } from "./types";
import TestData from "./test-data";
const API_URL = "http://127.0.0.1:8080"; const API_URL = "http://127.0.0.1:8080";
export async function getPopularToday(): Promise<Item[]> { export async function getPopularToday(): Promise<Item[]> {
const response = await fetch(`${API_URL}/api/items`); const response = await fetch(`${API_URL}/api/items`);
const { data, error }: JSONResponse = await response.json(); const {data, error}: JSONResponse = await response.json();
if (response.ok) { if (response.ok) {
if (data?.item) { if (data?.item) {
return data?.item; return data?.item;
} else {
return Promise.reject(new Error("Failed to fetch popular today"));
} }
} else { throw new Error("Failed to fetch popular today");
return Promise.reject(error);
} }
} throw new Error(error);
export function getMenuItems() {
return [
{
name: "Main Menu",
items: TestData,
},
{
name: "Breakfast",
items: [],
},
{
name: "Seasonal",
items: TestData,
},
];
} }

View file

@ -8,34 +8,24 @@ async function fakeDelay(timeout = 1000) {
} }
export async function getAnnouncements(): Promise<{ image: string }> { export async function getAnnouncements(): Promise<{ image: string }> {
if (cache["announcement_banner"] !== undefined) { if (cache["announcement_banner"] === undefined) {
return cache["announcement_banner"]; cache["announcement_banner"] = {
image: "/banner_images/BannerExampleImage.jpg",
};
await fakeDelay(200);
} }
await fakeDelay(200); return cache["announcement_banner"];
const data = {
image: "/banner_images/BannerExampleImage.jpg",
};
cache["announcement_banner"] = data;
return data;
} }
export async function getPopularToday(): Promise<Item[]> { export async function getPopularToday(): Promise<Item[]> {
if (cache["popular_today"] !== undefined) { if (cache["popular_today"] === undefined) {
return cache["popular_today"]; cache["popular_today"] = TestData;
await fakeDelay(200);
} }
await fakeDelay(200); return cache["popular_today"];
const data: Item[] = TestData;
cache["popular_today"] = data;
return data;
} }
export async function getMenuItems(): Promise< export async function getMenuItems(): Promise<{ name: string; items: Item[] }[]> {
{ name: string; items: Item[] }[]
> {
await fakeDelay(20); await fakeDelay(20);
return [ return [
{ {
@ -54,8 +44,6 @@ export async function getMenuItems(): Promise<
} }
export async function getItemsByUUID(items: string[]): Promise<Item[]> { export async function getItemsByUUID(items: string[]): Promise<Item[]> {
await fakeDelay(200);
const data: Item[] = []; const data: Item[] = [];
TestData.forEach((itemInDatabase: Item) => { TestData.forEach((itemInDatabase: Item) => {
@ -66,59 +54,41 @@ export async function getItemsByUUID(items: string[]): Promise<Item[]> {
}); });
}); });
if (data.length === 0) { if (data.length === 0) throw new Error("Resource could not be found");
throw new Error("Resource could not be found");
}
await fakeDelay(200);
return data; return data;
} }
export async function getItemByUUID(uuid: string): Promise<Item> { export async function getItemByUUID(uuid: string): Promise<Item> {
const data = await getItemsByUUID([uuid]); const data = await getItemsByUUID([uuid]);
if (data.length !== 1) { if (data.length !== 1) throw new Error("Resource could not be found");
throw new Error("Resource could not be found");
}
return data[0]; return data[0];
} }
export async function postContactEmail( export async function postContactEmail(name: string, email: string, message: string): Promise<string> {
name: string,
email: string,
message: string
): Promise<string> {
await fakeDelay(200); await fakeDelay(200);
if (!name) { if (!name) throw new Error("Name missing");
throw new Error("Name missing"); if (!email) throw new Error("Email missing");
} if (!message) throw new Error("Message missing");
if (!email) { if (message.length < 150) throw new Error("Message FUCKED");
throw new Error("Email missing");
}
if (!message) {
throw new Error("Message missing");
} else if (message.length < 150) {
throw new Error("Message FUCKED");
}
return "Check your email to confirm the message!"; return "Check your email to confirm the message!";
} }
export async function postVerifyCart( export async function postVerifyCart(cartData: Record<string, CartItem>): Promise<Record<string, CartItem>> {
currentCartData: Record<string, CartItem> let verifiedItems: Item[] = [];
): Promise<Record<string, CartItem>> {
const verifiedItems: Item[] = [];
await getItemsByUUID(Object.keys(currentCartData)) try {
.then((data) => { verifiedItems = await getItemsByUUID(Object.keys(cartData));
verifiedItems.push(...data); } catch (error) {
}) throw new Error(error);
.catch(() => { }
return new Error("Could not collect new cart information");
});
const newCartData: Record<string, CartItem> = {}; const newCartData: Record<string, CartItem> = {};
Object.entries(currentCartData).forEach(([uuid, currentData]) => { Object.entries(cartData).forEach(([uuid, currentData]) => {
verifiedItems.forEach((verifiedItem: Item) => { verifiedItems.forEach((verifiedItem: Item) => {
if (verifiedItem.uuid === uuid) { if (verifiedItem.uuid === uuid) {
newCartData[uuid] = { newCartData[uuid] = {

View file

@ -22,6 +22,8 @@ export type CartItem = {
}; };
export type JSONResponse = { export type JSONResponse = {
data?: any; // Todo make this _not_ an any data?: {
item: Item[], // Todo Make this not just item type
};
error?: string; error?: string;
}; };