feat: things are happening...
This commit is contained in:
@@ -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),
|
||||
}];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user