feat: things are happening...

This commit is contained in:
Daniel Ledda
2022-07-10 23:07:51 +02:00
parent ba68f953f0
commit fb9f78caf7
22 changed files with 448 additions and 207 deletions

View File

@@ -1,6 +1,8 @@
import { StoccaTreDbConn, WithoutId } from "../../database.ts";
import { Q, StoccaTreDbConn, WithoutId } from "../../database.ts";
import { IngredientModel } from "./IngredientModel.ts";
import { Maybe } from "../../Maybe.ts";
import { Result, StoccaTreError } from "../../Result.ts";
const TABLE_NAME = "main.ingredients";
export default class IngredientCollection {
private db: StoccaTreDbConn;
@@ -10,36 +12,38 @@ export default class IngredientCollection {
this.db = database;
}
async addIngredient(ingredient: WithoutId<IngredientModel>): Promise<Maybe<IngredientModel[]>> {
return await this.db.query(sql =>
sql<IngredientModel[]>`INSERT INTO ingredients ${ sql(ingredient) }`
);
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);
if (error) {
return [error];
}
if (result.length > 0) {
return [, result[0]];
}
return [new StoccaTreError("Ingredient wasn't inserted!")];
}
async getById(id: number): Promise<Maybe<IngredientModel>> {
async getById(id: number): Promise<Result<IngredientModel>> {
const found = this.mapById.get(id);
if (found) {
return { just: found };
return [, found];
}
const result = await this.db.query(sql => sql<IngredientModel[]>`SELECT * FROM ingredients WHERE id is ${ id }`);
if (result.error) {
return result;
const [error, result] = <Q<IngredientModel>> await this.db.query(`SELECT * FROM ${ TABLE_NAME } WHERE id is $id`, { id });
if (error) {
return [error];
}
const ingredient = result.just[0];
const ingredient = result[0];
this.mapById.set(ingredient.id, ingredient);
return { just: ingredient };
return [, ingredient];
}
async getAllIngredients(): Promise<Maybe<IterableIterator<IngredientModel>>> {
const result: Maybe<IngredientModel[]> = await this.db.query(sql =>
sql<IngredientModel[]>`SELECT * FROM ingredients`
);
if (!result.error) {
result.just.forEach(ingredient => this.mapById.set(ingredient.id, ingredient));
async getAllIngredients(): Promise<Result<IterableIterator<IngredientModel>>> {
const [error, result] = <Q<IngredientModel>> await this.db.query(`SELECT * FROM ${ TABLE_NAME }`);
if (!error) {
result.forEach((ingredient: IngredientModel) => this.mapById.set(ingredient.id, ingredient));
} else {
return result;
return [error];
}
return {
just: this.mapById.values(),
};
return [, this.mapById.values()];
}
}

View File

@@ -9,4 +9,6 @@ export const IngredientSchema = z.object({
export const IngredientSchemaWithoutId = IngredientSchema.omit({ id: true });
export type IngredientModel = z.infer<typeof IngredientSchema>;
export type IngredientModel = z.infer<typeof IngredientSchema>;

View File

@@ -1,9 +1,9 @@
import {StoccaTreDbConn} from "../../database.ts";
import { StoccaTreDbConn } from "../../database.ts";
import IngredientCollection from "./IngredientCollection.ts";
import StoccaTreRequest, {RouteDefinition} from "../../StoccaTreRequest.ts";
import {Maybe} from "../../Maybe.ts";
import {JSONObject} from "../../JSON.ts";
import {IngredientSchemaWithoutId} from "./IngredientModel.ts";
import StoccaTreRequest, { RouteDefinition } from "../../StoccaTreRequest.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;
@@ -11,11 +11,11 @@ export default class IngredientResource {
private routes: Readonly<Record<string, RouteDefinition>> = {
Add: {
pattern: /\/add/,
method: "POST"
method: "POST",
},
GetAll: {
pattern: /\/all/,
method: "GET"
method: "GET",
},
} as const;
@@ -24,33 +24,40 @@ export default class IngredientResource {
this.collection = new IngredientCollection(dbConnection);
}
async handleRequest(request: StoccaTreRequest): Promise<Maybe<JSONObject>> {
async handleRequest(request: StoccaTreRequest): Promise<Result<JSONObject> | null> {
let result;
if (request.match(this.routes.Add)) {
return await this.addIngredient(request);
result = await this.addIngredient(request);
}
if (request.match(this.routes.GetAll)) {
return await this.allIngredients(request);
result = await this.allIngredients(request);
}
return { error: { message: "Invalid route" }};
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<Maybe<{ insertedId: number }>> {
const ingredient = IngredientSchemaWithoutId.safeParse(JSON.parse(request.body ?? "{}"));
private async addIngredient(request: StoccaTreRequest): Promise<Result<{ id: number }>> {
const ingredient = IngredientSchemaWithoutId.safeParse(request.body);
if (!ingredient.success) {
return { error: new Error("Ingredient was malformed.") };
return [new StoccaTreError("Ingredient definition was malformed.", 400)];
}
return await this.collection.addIngredient(ingredient.data);
}
private async allIngredients(request: StoccaTreRequest): Promise<Maybe<JSONObject>> {
const getAllIngredientResult = await this.collection.getAllIngredients();
if (getAllIngredientResult.error) {
return getAllIngredientResult;
private async allIngredients(request: StoccaTreRequest): Promise<Result<JSONObject>> {
const [error, ingredients] = await this.collection.getAllIngredients();
if (error) {
return [error];
}
return {
just: {
ingredients: Array.from(getAllIngredientResult.just),
},
};
return [, {
ingredients: Array.from(ingredients),
}];
}
}
}