feat: implementing server framework
This commit is contained in:
35
server/resources/ingredient/IngredientCollection.ts
Normal file
35
server/resources/ingredient/IngredientCollection.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import {StoccaTreDbConn, WithoutId} from "../../database.ts";
|
||||
import {IngredientModel} from "./IngredientModel.ts";
|
||||
|
||||
export default class IngredientCollection {
|
||||
private dbConnection: StoccaTreDbConn;
|
||||
private mapById: Map<number, IngredientModel> = new Map<number, IngredientModel>();
|
||||
private allGotten = false;
|
||||
|
||||
constructor(database: StoccaTreDbConn) {
|
||||
this.dbConnection = database;
|
||||
}
|
||||
|
||||
async addIngredient(ingredient: WithoutId<IngredientModel>): Promise<any> {
|
||||
const result = await this.dbConnection.query<any>(
|
||||
`INSERT INTO ingredients (id, name, displayName, displayNameDE) VALUES (NULL, '${ingredient.name}', '${ingredient.displayName}', '${ingredient.displayNameDE}');`
|
||||
);
|
||||
return result;
|
||||
}
|
||||
|
||||
async getAllIngredients(): Promise<Maybe<IterableIterator<IngredientModel>>> {
|
||||
if (!this.allGotten) {
|
||||
const result = await this.dbConnection.query<IngredientModel[]>("SELECT * FROM ingredients");
|
||||
if (result.just) {
|
||||
result.just.forEach((ingredient) => this.mapById.set(ingredient.id, ingredient));
|
||||
} else {
|
||||
return result;
|
||||
}
|
||||
this.allGotten = true;
|
||||
}
|
||||
return {
|
||||
just: this.mapById.values(),
|
||||
error: null,
|
||||
};
|
||||
}
|
||||
}
|
||||
6
server/resources/ingredient/IngredientModel.ts
Normal file
6
server/resources/ingredient/IngredientModel.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
export type IngredientModel = {
|
||||
id: number,
|
||||
name: string,
|
||||
displayName: string,
|
||||
displayNameDE: string,
|
||||
};
|
||||
30
server/resources/ingredient/IngredientResource.ts
Normal file
30
server/resources/ingredient/IngredientResource.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import {StoccaTreDbConn} from "/database.ts";
|
||||
import IngredientCollection from "IngredientCollection.ts";
|
||||
import StoccaTreRequest from "/StoccaTreRequest.ts";
|
||||
|
||||
export default class IngredientResource {
|
||||
private dbConnection: StoccaTreDbConn;
|
||||
private collection: IngredientCollection;
|
||||
|
||||
constructor(dbConnection: StoccaTreDbConn) {
|
||||
this.dbConnection = dbConnection;
|
||||
this.collection = new IngredientCollection(dbConnection);
|
||||
}
|
||||
|
||||
async handleRequest(request: StoccaTreRequest): Promise<Maybe<JSONObject>> {
|
||||
return await this.allIngredients(request);
|
||||
}
|
||||
|
||||
async allIngredients(request: StoccaTreRequest): Promise<Maybe<JSONObject>> {
|
||||
const getAllIngredientResult = await this.collection.getAllIngredients();
|
||||
if (!getAllIngredientResult.error) {
|
||||
return getAllIngredientResult.just;
|
||||
}
|
||||
return {
|
||||
just: {
|
||||
ingredients: Array.from(getAllIngredientResult.just),
|
||||
},
|
||||
error: "",
|
||||
};
|
||||
}
|
||||
}
|
||||
3
server/resources/ingredient/main.ts
Normal file
3
server/resources/ingredient/main.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export { type IngredientModel } from "./IngredientModel.ts";
|
||||
export { default as IngredientCollection } from "./IngredientCollection.ts";
|
||||
export { default as IngredientResource } from "./IngredientResource.ts";
|
||||
1
server/resources/main.ts
Normal file
1
server/resources/main.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from "./ingredient/main.ts";
|
||||
Reference in New Issue
Block a user