import {StoccaTreDbConn} from "../../database.ts"; import IngredientCollection from "./IngredientCollection.ts"; import StoccaTreRequest from "../../StoccaTreRequest.ts"; import {Maybe} from "../../Maybe.ts"; import {JSONObject} from "../../JSON.ts"; import {IngredientModel} from "./IngredientModel.ts"; export default class IngredientResource { private dbConnection: StoccaTreDbConn; private collection: IngredientCollection; constructor(dbConnection: StoccaTreDbConn) { this.dbConnection = dbConnection; this.collection = new IngredientCollection(dbConnection); } static isIngredient(json: JSONObject): json is IngredientModel { if (json) { return true; } return false; } async handleRequest(request: StoccaTreRequest): Promise> { switch (request.route) { case "/add": if (request.method === "POST") { const ingredient = JSON.parse(request.body ?? ""); return await this.collection.addIngredient(JSON.parse(request.body)); } break; case "/all": return await this.allIngredients(request); default: break; } return { error: {message: "Invalid route" }}; } async allIngredients(request: StoccaTreRequest): Promise> { const getAllIngredientResult = await this.collection.getAllIngredients(); if (getAllIngredientResult.error) { return getAllIngredientResult; } return { just: { ingredients: Array.from(getAllIngredientResult.just), }, }; } }