feat: new user resource integrating postgresql

This commit is contained in:
Daniel Ledda
2022-07-08 09:31:34 +02:00
parent 3e5c53f9f5
commit ba68f953f0
18 changed files with 281 additions and 121 deletions

View File

@@ -1,31 +1,42 @@
import {StoccaTreDbConn, WithoutId} from "../../database.ts";
import {IngredientModel} from "./IngredientModel.ts";
import {Maybe} from "../../Maybe.ts";
import { StoccaTreDbConn, WithoutId } from "../../database.ts";
import { IngredientModel } from "./IngredientModel.ts";
import { Maybe } from "../../Maybe.ts";
export default class IngredientCollection {
private dbConnection: StoccaTreDbConn;
private db: StoccaTreDbConn;
private mapById: Map<number, IngredientModel> = new Map<number, IngredientModel>();
private allGotten = false;
constructor(database: StoccaTreDbConn) {
this.dbConnection = database;
this.db = database;
}
async addIngredient(ingredient: WithoutId<IngredientModel>): Promise<any> {
return await this.dbConnection.query<any>(
`INSERT INTO ingredients (id, name, displayName, displayNameDE) VALUES (NULL, '${ingredient.name}', '${ingredient.displayName}', '${ingredient.displayNameDE}');`
async addIngredient(ingredient: WithoutId<IngredientModel>): Promise<Maybe<IngredientModel[]>> {
return await this.db.query(sql =>
sql<IngredientModel[]>`INSERT INTO ingredients ${ sql(ingredient) }`
);
}
async getById(id: number): Promise<Maybe<IngredientModel>> {
const found = this.mapById.get(id);
if (found) {
return { just: found };
}
const result = await this.db.query(sql => sql<IngredientModel[]>`SELECT * FROM ingredients WHERE id is ${ id }`);
if (result.error) {
return result;
}
const ingredient = result.just[0];
this.mapById.set(ingredient.id, ingredient);
return { just: ingredient };
}
async getAllIngredients(): Promise<Maybe<IterableIterator<IngredientModel>>> {
if (!this.allGotten) {
const result = await this.dbConnection.query<IngredientModel[]>("SELECT * FROM ingredients");
if (!result.error) {
result.just.forEach((ingredient: IngredientModel) => this.mapById.set(ingredient.id, ingredient));
} else {
return result;
}
this.allGotten = true;
const result: Maybe<IngredientModel[]> = await this.db.query(sql =>
sql<IngredientModel[]>`SELECT * FROM ingredients`
);
if (!result.error) {
result.just.forEach(ingredient => this.mapById.set(ingredient.id, ingredient));
} else {
return result;
}
return {
just: this.mapById.values(),