import { StoccaTreDbConn, WithoutId } from "./database.ts"; export type Ingredient = { id: number, name: string, displayName: string, displayNameDE: string, }; export default class IngredientService { private dbConnection: StoccaTreDbConn; private mapById: Map = new Map(); private allGotten = false; constructor(database: StoccaTreDbConn) { this.dbConnection = database; } async addIngredient(ingredient: WithoutId): Promise { const result = await this.dbConnection.query( `INSERT INTO ingredients (id, name, displayName, displayNameDE) VALUES (NULL, '${ingredient.name}', '${ingredient.displayName}', '${ingredient.displayNameDE}');` ); return result; } async getAllIngredients(): Promise> { if (!this.allGotten) { const result = await this.dbConnection.query("SELECT * FROM ingredients"); result.forEach((ingredient) => this.mapById.set(ingredient.id, ingredient)); this.allGotten = true; } return this.mapById.values(); } }