feat: improving infrastructure
This commit is contained in:
@@ -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: "",
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user