refactoring on back end

This commit is contained in:
Mikayla Dobson
2022-12-17 13:30:55 -06:00
parent be375aad8f
commit 9a43f58ee3
15 changed files with 117 additions and 90 deletions

View File

@@ -2,6 +2,7 @@ import { IRecipe } from "../schemas";
import pool from "../db";
import { CollectionCtl } from "../controllers";
import now from "../util/now";
import { CtlResponse } from "../util/types";
const CollectionInstance = new CollectionCtl();
export class Recipe {
@@ -76,13 +77,18 @@ export class Recipe {
if (!result.rows.length) return null;
// associate recipe with default collection once created
const collection = await CollectionInstance.getUserDefault(userid);
const collection: CtlResponse<IRecipe> = await CollectionInstance.getUserDefault(userid);
const associateToCollection = `
INSERT INTO recipin.cmp_recipecollection
(recipeid, collectionid)
VALUES ($1, $2) RETURNING *
`
const associateResult = await pool.query(associateToCollection, [result.rows[0].id, collection.id]);
if (typeof collection.data == 'string') {
return null;
}
const associateResult = await pool.query(associateToCollection, [result.rows[0].id, collection.data.id]);
if (!associateResult.rows.length) return null;
return { recipe: result.rows[0], collection: associateResult.rows[0] }