feat: improving infrastructure

This commit is contained in:
Daniel Ledda
2022-06-30 08:33:13 +02:00
parent bd177c7f09
commit 3e5c53f9f5
12 changed files with 76 additions and 61 deletions

View File

@@ -1,6 +1,9 @@
import {StoccaTreDbConn} from "/database.ts";
import IngredientCollection from "IngredientCollection.ts";
import StoccaTreRequest from "/StoccaTreRequest.ts";
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;
@@ -11,20 +14,40 @@ export default class IngredientResource {
this.collection = new IngredientCollection(dbConnection);
}
static isIngredient(json: JSONObject): json is IngredientModel {
if (json) {
return true;
}
return false;
}
async handleRequest(request: StoccaTreRequest): Promise<Maybe<JSONObject>> {
return await this.allIngredients(request);
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<Maybe<JSONObject>> {
const getAllIngredientResult = await this.collection.getAllIngredients();
if (!getAllIngredientResult.error) {
return getAllIngredientResult.just;
if (getAllIngredientResult.error) {
return getAllIngredientResult;
}
return {
just: {
ingredients: Array.from(getAllIngredientResult.just),
},
error: "",
};
}
}