fix: removing collage feat: added ingredient type to server for testing and a config" chore: setup deno server package
34 lines
1.2 KiB
TypeScript
34 lines
1.2 KiB
TypeScript
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<number, Ingredient> = new Map<number, Ingredient>();
|
|
private allGotten = false;
|
|
|
|
constructor(database: StoccaTreDbConn) {
|
|
this.dbConnection = database;
|
|
}
|
|
|
|
async addIngredient(ingredient: WithoutId<Ingredient>): 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<IterableIterator<Ingredient>> {
|
|
if (!this.allGotten) {
|
|
const result = await this.dbConnection.query<Ingredient[]>("SELECT * FROM ingredients");
|
|
result.forEach((ingredient) => this.mapById.set(ingredient.id, ingredient));
|
|
this.allGotten = true;
|
|
}
|
|
return this.mapById.values();
|
|
}
|
|
} |