feat: things are happening...
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import { StoccaTreDbConn, WithoutId } from "../../database.ts";
|
||||
import { Q, StoccaTreDbConn, WithoutId } from "../../database.ts";
|
||||
import { IngredientModel } from "./IngredientModel.ts";
|
||||
import { Maybe } from "../../Maybe.ts";
|
||||
import { Result, StoccaTreError } from "../../Result.ts";
|
||||
|
||||
const TABLE_NAME = "main.ingredients";
|
||||
|
||||
export default class IngredientCollection {
|
||||
private db: StoccaTreDbConn;
|
||||
@@ -10,36 +12,38 @@ export default class IngredientCollection {
|
||||
this.db = database;
|
||||
}
|
||||
|
||||
async addIngredient(ingredient: WithoutId<IngredientModel>): Promise<Maybe<IngredientModel[]>> {
|
||||
return await this.db.query(sql =>
|
||||
sql<IngredientModel[]>`INSERT INTO ingredients ${ sql(ingredient) }`
|
||||
);
|
||||
async addIngredient(ingredient: WithoutId<IngredientModel>): Promise<Result<IngredientModel>> {
|
||||
const [error, result] = <Q<IngredientModel>> await this.db.query(`INSERT INTO ${ TABLE_NAME } VALUES (DEFAULT, $name, $displayName, $displayNameDE) RETURNING *;`, ingredient);
|
||||
if (error) {
|
||||
return [error];
|
||||
}
|
||||
if (result.length > 0) {
|
||||
return [, result[0]];
|
||||
}
|
||||
return [new StoccaTreError("Ingredient wasn't inserted!")];
|
||||
}
|
||||
|
||||
async getById(id: number): Promise<Maybe<IngredientModel>> {
|
||||
async getById(id: number): Promise<Result<IngredientModel>> {
|
||||
const found = this.mapById.get(id);
|
||||
if (found) {
|
||||
return { just: found };
|
||||
return [, found];
|
||||
}
|
||||
const result = await this.db.query(sql => sql<IngredientModel[]>`SELECT * FROM ingredients WHERE id is ${ id }`);
|
||||
if (result.error) {
|
||||
return result;
|
||||
const [error, result] = <Q<IngredientModel>> await this.db.query(`SELECT * FROM ${ TABLE_NAME } WHERE id is $id`, { id });
|
||||
if (error) {
|
||||
return [error];
|
||||
}
|
||||
const ingredient = result.just[0];
|
||||
const ingredient = result[0];
|
||||
this.mapById.set(ingredient.id, ingredient);
|
||||
return { just: ingredient };
|
||||
return [, ingredient];
|
||||
}
|
||||
async getAllIngredients(): Promise<Maybe<IterableIterator<IngredientModel>>> {
|
||||
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));
|
||||
|
||||
async getAllIngredients(): Promise<Result<IterableIterator<IngredientModel>>> {
|
||||
const [error, result] = <Q<IngredientModel>> await this.db.query(`SELECT * FROM ${ TABLE_NAME }`);
|
||||
if (!error) {
|
||||
result.forEach((ingredient: IngredientModel) => this.mapById.set(ingredient.id, ingredient));
|
||||
} else {
|
||||
return result;
|
||||
return [error];
|
||||
}
|
||||
return {
|
||||
just: this.mapById.values(),
|
||||
};
|
||||
return [, this.mapById.values()];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user