refactor: some stuff
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -2,5 +2,6 @@ node_modules
|
|||||||
frontend/dist
|
frontend/dist
|
||||||
frontend/node_modules
|
frontend/node_modules
|
||||||
.idea
|
.idea
|
||||||
|
.vim
|
||||||
config.json
|
config.json
|
||||||
pizza-collage
|
pizza-collage
|
||||||
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 { z } from "zod";
|
||||||
import { IngredientModel } from "./IngredientModel.ts";
|
import { JSONObject } from "../../JSON.ts";
|
||||||
|
import { Q, StoccaTreDbConn } from "../../database.ts";
|
||||||
import { Result, StoccaTreError } from "../../Result.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 {
|
export default class IngredientCollection {
|
||||||
|
static readonly TABLE_NAME = "main.ingredients";
|
||||||
private db: StoccaTreDbConn;
|
private db: StoccaTreDbConn;
|
||||||
private mapById: Map<number, IngredientModel> = new Map<number, IngredientModel>();
|
private mapById: Map<number, IngredientModel> = new Map<number, IngredientModel>();
|
||||||
|
|
||||||
@@ -12,8 +23,15 @@ export default class IngredientCollection {
|
|||||||
this.db = database;
|
this.db = database;
|
||||||
}
|
}
|
||||||
|
|
||||||
async addIngredient(ingredient: WithoutId<IngredientModel>): Promise<Result<IngredientModel>> {
|
async addIngredient(ingredient: JSONObject): Promise<Result<IngredientModel>> {
|
||||||
const [error, result] = <Q<IngredientModel>> await this.db.query(`INSERT INTO ${ TABLE_NAME } VALUES (DEFAULT, $name, $displayName, $displayNameDE) RETURNING *;`, ingredient);
|
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) {
|
if (error) {
|
||||||
return [error];
|
return [error];
|
||||||
}
|
}
|
||||||
@@ -28,7 +46,7 @@ export default class IngredientCollection {
|
|||||||
if (found) {
|
if (found) {
|
||||||
return [, 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) {
|
if (error) {
|
||||||
return [error];
|
return [error];
|
||||||
}
|
}
|
||||||
@@ -37,13 +55,13 @@ export default class IngredientCollection {
|
|||||||
return [, ingredient];
|
return [, ingredient];
|
||||||
}
|
}
|
||||||
|
|
||||||
async getAllIngredients(): Promise<Result<IterableIterator<IngredientModel>>> {
|
async getAllIngredients(): Promise<Result<IngredientModel[]>> {
|
||||||
const [error, result] = <Q<IngredientModel>> await this.db.query(`SELECT * FROM ${ TABLE_NAME }`);
|
const [error, result] = <Q<IngredientModel>> await this.db.query(`SELECT * FROM ${ IngredientCollection.TABLE_NAME }`);
|
||||||
if (!error) {
|
if (!error) {
|
||||||
result.forEach((ingredient: IngredientModel) => this.mapById.set(ingredient.id, ingredient));
|
result.forEach((ingredient: IngredientModel) => this.mapById.set(ingredient.id, ingredient));
|
||||||
} else {
|
} else {
|
||||||
return [error];
|
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>;
|
|
||||||
|
|
||||||
|
|||||||
@@ -3,10 +3,8 @@ import IngredientCollection from "./IngredientCollection.ts";
|
|||||||
import StoccaTreRequest, { RouteDefinition } from "../../StoccaTreRequest.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 { JSONObject } from "../../JSON.ts";
|
||||||
import { IngredientModel, IngredientSchemaWithoutId } from "./IngredientModel.ts";
|
|
||||||
|
|
||||||
export default class IngredientResource {
|
export default class IngredientResource {
|
||||||
private dbConnection: StoccaTreDbConn;
|
|
||||||
private collection: IngredientCollection;
|
private collection: IngredientCollection;
|
||||||
private routes: Readonly<Record<string, RouteDefinition>> = {
|
private routes: Readonly<Record<string, RouteDefinition>> = {
|
||||||
Add: {
|
Add: {
|
||||||
@@ -20,44 +18,17 @@ export default class IngredientResource {
|
|||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
constructor(dbConnection: StoccaTreDbConn) {
|
constructor(dbConnection: StoccaTreDbConn) {
|
||||||
this.dbConnection = dbConnection;
|
|
||||||
this.collection = new IngredientCollection(dbConnection);
|
this.collection = new IngredientCollection(dbConnection);
|
||||||
}
|
}
|
||||||
|
|
||||||
async handleRequest(request: StoccaTreRequest): Promise<Result<JSONObject> | null> {
|
async handleRequest(request: StoccaTreRequest): Promise<Result<JSONObject> | null> {
|
||||||
let result;
|
let result = null;
|
||||||
if (request.match(this.routes.Add)) {
|
if (request.match(this.routes.Add) && request.body) {
|
||||||
result = await this.addIngredient(request);
|
result = await this.collection.addIngredient(request.body);
|
||||||
}
|
}
|
||||||
if (request.match(this.routes.GetAll)) {
|
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 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, type IngredientModel } from "./IngredientCollection.ts";
|
||||||
export { default as IngredientCollection } from "./IngredientCollection.ts";
|
|
||||||
export { default as IngredientResource } from "./IngredientResource.ts";
|
export { default as IngredientResource } from "./IngredientResource.ts";
|
||||||
|
|||||||
Reference in New Issue
Block a user