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> = { 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 | 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; } }