36 lines
1.3 KiB
TypeScript
36 lines
1.3 KiB
TypeScript
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,
|
|
};
|
|
}
|
|
}
|