35 lines
1.1 KiB
TypeScript
35 lines
1.1 KiB
TypeScript
import { StoccaTreDbConn } from "../../database.ts";
|
|
import IngredientCollection from "./IngredientCollection.ts";
|
|
import StoccaTreRequest, { RouteDefinition } from "../../StoccaTreRequest.ts";
|
|
import { Result, StoccaTreError } from "../../Result.ts";
|
|
import { JSONObject } from "../../JSON.ts";
|
|
|
|
export default class IngredientResource {
|
|
private collection: IngredientCollection;
|
|
private routes: Readonly<Record<string, RouteDefinition>> = {
|
|
Add: {
|
|
pattern: /\/add/,
|
|
method: "POST",
|
|
},
|
|
GetAll: {
|
|
pattern: /\/all/,
|
|
method: "GET",
|
|
},
|
|
} as const;
|
|
|
|
constructor(dbConnection: StoccaTreDbConn) {
|
|
this.collection = new IngredientCollection(dbConnection);
|
|
}
|
|
|
|
async handleRequest(request: StoccaTreRequest): Promise<Result<JSONObject> | null> {
|
|
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.collection.getAllIngredients();
|
|
}
|
|
return result;
|
|
}
|
|
}
|