refactor: some stuff
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -2,5 +2,6 @@ node_modules
|
||||
frontend/dist
|
||||
frontend/node_modules
|
||||
.idea
|
||||
.vim
|
||||
config.json
|
||||
pizza-collage
|
||||
@@ -1,4 +1,4 @@
|
||||
import {Capsule, h, Rung } from "@djledda/ladder";
|
||||
import { Capsule, h, Rung } from "@djledda/ladder";
|
||||
import StoccaTreLogo from "@/assets/stocca-tre-logo.svg";
|
||||
import "./stocca-tre-root.scss";
|
||||
import IngredientsPage from "../pages/IngredientsPage";
|
||||
|
||||
17
server/deno.json
Normal file
17
server/deno.json
Normal file
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"allowJs": true,
|
||||
"lib": ["deno.window"],
|
||||
"strict": true
|
||||
},
|
||||
"importMap": "./import_map.json",
|
||||
"fmt": {
|
||||
"options": {
|
||||
"useTabs": true,
|
||||
"lineWidth": 120,
|
||||
"indentWidth": 4,
|
||||
"singleQuote": false,
|
||||
"proseWrap": "preserve"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,21 @@
|
||||
import { Q, StoccaTreDbConn, WithoutId } from "../../database.ts";
|
||||
import { IngredientModel } from "./IngredientModel.ts";
|
||||
import { z } from "zod";
|
||||
import { JSONObject } from "../../JSON.ts";
|
||||
import { Q, StoccaTreDbConn } from "../../database.ts";
|
||||
import { Result, StoccaTreError } from "../../Result.ts";
|
||||
|
||||
const TABLE_NAME = "main.ingredients";
|
||||
export const IngredientSchema = z.object({
|
||||
id: z.number(),
|
||||
name: z.string().nonempty(),
|
||||
displayName: z.string().nonempty(),
|
||||
displayNameDE: z.string().nonempty(),
|
||||
});
|
||||
|
||||
export const IngredientSchemaWithoutId = IngredientSchema.omit({ id: true });
|
||||
|
||||
export type IngredientModel = z.infer<typeof IngredientSchema>;
|
||||
|
||||
export default class IngredientCollection {
|
||||
static readonly TABLE_NAME = "main.ingredients";
|
||||
private db: StoccaTreDbConn;
|
||||
private mapById: Map<number, IngredientModel> = new Map<number, IngredientModel>();
|
||||
|
||||
@@ -12,8 +23,15 @@ export default class IngredientCollection {
|
||||
this.db = database;
|
||||
}
|
||||
|
||||
async addIngredient(ingredient: WithoutId<IngredientModel>): Promise<Result<IngredientModel>> {
|
||||
const [error, result] = <Q<IngredientModel>> await this.db.query(`INSERT INTO ${ TABLE_NAME } VALUES (DEFAULT, $name, $displayName, $displayNameDE) RETURNING *;`, ingredient);
|
||||
async addIngredient(ingredient: JSONObject): Promise<Result<IngredientModel>> {
|
||||
const parsed = IngredientSchemaWithoutId.safeParse(ingredient);
|
||||
if (!ingredient.success) {
|
||||
return [new StoccaTreError("Ingredient definition was malformed.", 400)];
|
||||
}
|
||||
const [error, result] = <Q<IngredientModel>> await this.db.query(
|
||||
`INSERT INTO ${ IngredientCollection.TABLE_NAME } VALUES (DEFAULT, $name, $displayName, $displayNameDE) RETURNING *;`,
|
||||
parsed
|
||||
);
|
||||
if (error) {
|
||||
return [error];
|
||||
}
|
||||
@@ -28,7 +46,7 @@ export default class IngredientCollection {
|
||||
if (found) {
|
||||
return [, found];
|
||||
}
|
||||
const [error, result] = <Q<IngredientModel>> await this.db.query(`SELECT * FROM ${ TABLE_NAME } WHERE id is $id`, { id });
|
||||
const [error, result] = <Q<IngredientModel>> await this.db.query(`SELECT * FROM ${ IngredientCollection.TABLE_NAME } WHERE id is $id`, { id });
|
||||
if (error) {
|
||||
return [error];
|
||||
}
|
||||
@@ -37,13 +55,13 @@ export default class IngredientCollection {
|
||||
return [, ingredient];
|
||||
}
|
||||
|
||||
async getAllIngredients(): Promise<Result<IterableIterator<IngredientModel>>> {
|
||||
const [error, result] = <Q<IngredientModel>> await this.db.query(`SELECT * FROM ${ TABLE_NAME }`);
|
||||
async getAllIngredients(): Promise<Result<IngredientModel[]>> {
|
||||
const [error, result] = <Q<IngredientModel>> await this.db.query(`SELECT * FROM ${ IngredientCollection.TABLE_NAME }`);
|
||||
if (!error) {
|
||||
result.forEach((ingredient: IngredientModel) => this.mapById.set(ingredient.id, ingredient));
|
||||
} else {
|
||||
return [error];
|
||||
}
|
||||
return [, this.mapById.values()];
|
||||
return [, Array.from(this.mapById.values())];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export const IngredientSchema = z.object({
|
||||
id: z.number(),
|
||||
name: z.string().nonempty(),
|
||||
displayName: z.string().nonempty(),
|
||||
displayNameDE: z.string().nonempty(),
|
||||
});
|
||||
|
||||
export const IngredientSchemaWithoutId = IngredientSchema.omit({ id: true });
|
||||
|
||||
|
||||
export type IngredientModel = z.infer<typeof IngredientSchema>;
|
||||
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
import { StoccaTreDbConn } from "../../database.ts";
|
||||
import IngredientCollection from "./IngredientCollection.ts";
|
||||
import StoccaTreRequest, { RouteDefinition } from "../../StoccaTreRequest.ts";
|
||||
import {Result, StoccaTreError} from "../../Result.ts";
|
||||
import { Result, StoccaTreError } from "../../Result.ts";
|
||||
import { JSONObject } from "../../JSON.ts";
|
||||
import { IngredientModel, IngredientSchemaWithoutId } from "./IngredientModel.ts";
|
||||
|
||||
export default class IngredientResource {
|
||||
private dbConnection: StoccaTreDbConn;
|
||||
private collection: IngredientCollection;
|
||||
private routes: Readonly<Record<string, RouteDefinition>> = {
|
||||
Add: {
|
||||
@@ -20,44 +18,17 @@ export default class IngredientResource {
|
||||
} as const;
|
||||
|
||||
constructor(dbConnection: StoccaTreDbConn) {
|
||||
this.dbConnection = dbConnection;
|
||||
this.collection = new IngredientCollection(dbConnection);
|
||||
}
|
||||
|
||||
async handleRequest(request: StoccaTreRequest): Promise<Result<JSONObject> | null> {
|
||||
let result;
|
||||
if (request.match(this.routes.Add)) {
|
||||
result = await this.addIngredient(request);
|
||||
let result = null;
|
||||
if (request.match(this.routes.Add) && request.body) {
|
||||
result = await this.collection.addIngredient(request.body);
|
||||
}
|
||||
if (request.match(this.routes.GetAll)) {
|
||||
result = await this.allIngredients(request);
|
||||
result = await this.collection.getAllIngredients();
|
||||
}
|
||||
if (result) {
|
||||
const [error] = result;
|
||||
if (error) {
|
||||
return [error.qualified("Could not fulfill ingredient request: ")];
|
||||
} else {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private async addIngredient(request: StoccaTreRequest): Promise<Result<{ id: number }>> {
|
||||
const ingredient = IngredientSchemaWithoutId.safeParse(request.body);
|
||||
if (!ingredient.success) {
|
||||
return [new StoccaTreError("Ingredient definition was malformed.", 400)];
|
||||
}
|
||||
return await this.collection.addIngredient(ingredient.data);
|
||||
}
|
||||
|
||||
private async allIngredients(request: StoccaTreRequest): Promise<Result<JSONObject>> {
|
||||
const [error, ingredients] = await this.collection.getAllIngredients();
|
||||
if (error) {
|
||||
return [error];
|
||||
}
|
||||
return [, {
|
||||
ingredients: Array.from(ingredients),
|
||||
}];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,2 @@
|
||||
export { type IngredientModel } from "./IngredientModel.ts";
|
||||
export { default as IngredientCollection } from "./IngredientCollection.ts";
|
||||
export { default as IngredientCollection, type IngredientModel } from "./IngredientCollection.ts";
|
||||
export { default as IngredientResource } from "./IngredientResource.ts";
|
||||
|
||||
Reference in New Issue
Block a user