attempted to refactor db seed, still needs more updating

This commit is contained in:
Mikayla Dobson
2022-11-26 18:26:32 -06:00
parent 9bd1704da9
commit 9d0c8a8782
22 changed files with 340 additions and 68 deletions

View File

@@ -6,7 +6,7 @@ import pool from "../db";
import fs from 'fs';
const UserInstance = new User();
export class Collection {
async getOne(id: string) {
async getOne(id: number | string) {
try {
const statement = `SELECT * FROM recipin.collection WHERE id = $1`;
const values = [id];
@@ -18,6 +18,21 @@ export class Collection {
}
}
async getUserDefault(id: number | string) {
try {
const statement = `
SELECT * FROM recipin.collection
WHERE ownerid = $1
AND ismaincollection = true;
`
const result = await pool.query(statement, [id]);
if (result.rows.length) return result.rows[0];
return null;
} catch (e: any) {
throw new Error(e);
}
}
async getAll() {
// requires clearance
try {
@@ -31,6 +46,8 @@ export class Collection {
}
async post(data: ICollection) {
console.log('new default collection');
console.log(data);
const { name, active, ismaincollection, ownerid } = data;
try {
const statement = `
@@ -48,7 +65,7 @@ export class Collection {
}
}
async getSubscriptions(userid: string) {
async getSubscriptions(userid: number | string) {
try {
const sql = fs.readFileSync(appRoot + '/db/sql/get/getsubscriptions.sql').toString();
console.log(sql);
@@ -60,7 +77,7 @@ export class Collection {
}
}
async postSubscription(collectionid: string, userid: string): Promise<{ ok: boolean, code: number, data: string | any[] }> {
async postSubscription(collectionid: number | string, userid: number | string): Promise<{ ok: boolean, code: number, data: number | string | any[] }> {
try {
// ensure user exists
const user: IUser | null = await UserInstance.getOneByID(userid);
@@ -83,7 +100,8 @@ export class Collection {
}
// ensure a user cannot subscribe to their own collection
if (target.ownerid == parseInt(userid)) {
let typedUserID: number = (userid == typeof 'string') ? parseInt(userid) : userid as number;
if (target.ownerid == typedUserID) {
return {
ok: false,
code: 403,
@@ -99,7 +117,7 @@ export class Collection {
const subscriptionResult = await pool.query(allSubscriptions, [collectionid]);
if (subscriptionResult.rows?.length) {
for (let row of subscriptionResult.rows) {
if (row.usermemberid == parseInt(userid)) {
if (row.usermemberid == typedUserID) {
return {
ok: false,
code: 403,
@@ -112,8 +130,8 @@ export class Collection {
// finally, execute insertion
const statement = `
INSERT INTO recipin.cmp_usersubscriptions
(collectionid, usermemberid)
VALUES ($1, $2)
(collectionid, usermemberid, active)
VALUES ($1, $2, true)
RETURNING *;
`

View File

@@ -1,5 +1,6 @@
import { IIngredient } from "../schemas";
import pool from "../db";
import now from "../util/now";
export class Ingredient {
async getAll() {
@@ -28,9 +29,9 @@ export class Ingredient {
try {
const statement = `
INSERT INTO recipin.ingredient
(name, description)
VALUES ($1, $2) RETURNING *`;
const values = [data.name, data.description];
(name, description, datecreated)
VALUES ($1, $2, $3) RETURNING *`;
const values = [data.name, data.description, data.datecreated || now];
const result = await pool.query(statement, values);
if (result.rows.length) return result.rows[0];
return null;

View File

@@ -1,8 +1,8 @@
import { IRecipe } from "../schemas";
import pgPromise from "pg-promise";
import pool from "../db";
const pgp = pgPromise({ capSQL: true });
import { CollectionCtl } from "../controllers";
import now from "../util/now";
const CollectionInstance = new CollectionCtl();
export class Recipe {
async getOneByID(id: string) {
@@ -17,9 +17,24 @@ export class Recipe {
}
}
async getAllByUserID(id: string) {
async getAllAuthored(id: string) {
try {
// to do: use setupbrowser.sql to setup the recipe browser
const statement = `SELECT * FROM recipin.recipe WHERE authoruserid = $1`;
const result = await pool.query(statement, [id]);
if (result.rows.length) return result.rows;
return null;
} catch (e: any) {
throw new Error(e);
}
}
async getAllAccessible(id: string) {
}
async fetchRecipesByCollection(collectionid: string) {
try {
} catch (e: any) {
throw new Error(e);
}
@@ -45,15 +60,32 @@ export class Recipe {
}
}
async post(data: IRecipe) {
async post(userid: string, data: IRecipe) {
const { name, description, preptime } = data;
try {
const statement = `INSERT INTO recipin.recipe (name, description, preptime, authoruserid) VALUES ($1, $2, $3, (SELECT id FROM recipin.appusers WHERE id = 1)) RETURNING *;`
const values = [name, description, preptime];
// create recipe itself
const statement = `
INSERT INTO recipin.recipe
(name, description, preptime, authoruserid, datecreated, datemodified)
VALUES ($1, $2, $3, $4, $5, $6)
RETURNING *;
`
const values = [name, description, preptime, userid, now, now];
const result = await pool.query(statement, values);
if (result.rows) return result.rows[0];
return null;
if (!result.rows.length) return null;
// associate recipe with default collection once created
const collection = 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 (!associateResult.rows.length) return null;
return { recipe: result.rows[0], collection: associateResult.rows[0] }
} catch (error: any) {
throw new Error(error);
}

View File

@@ -17,7 +17,7 @@ export class User {
}
}
async getOneByID(id: string) {
async getOneByID(id: number | string) {
try {
const statement = `SELECT * FROM recipin.appusers WHERE id = $1`;
const values = [id];
@@ -29,7 +29,7 @@ export class User {
}
}
async getOneByEmail(email: string) {
async getOneByEmail(email: number | string) {
try {
const statement = `SELECT * FROM recipin.appusers WHERE email = $1`;
const result = await pool.query(statement, [email]);
@@ -41,7 +41,7 @@ export class User {
}
}
async updateOneByID(id: string, data: IUser) {
async updateOneByID(id: number | string, data: IUser) {
try {
const statement = `
UPDATE recipin.appusers
@@ -85,14 +85,14 @@ export class User {
`;
const params = [firstname, lastname, handle, email, password, active, isadmin, datecreated, datemodified];
const result = await pool.query(statement, params);
if (result.rows.length) return result.rows;
if (result.rows.length) return result.rows[0];
return null;
} catch (error: any) {
throw new Error(error);
}
}
async getFriends(id: string) {
async getFriends(id: number | string) {
try {
const sql = fs.readFileSync(appRoot + '/db/sql/derived/friendships.sql').toString();
const result = await pool.query(sql, [id]);
@@ -103,14 +103,14 @@ export class User {
}
}
async getFriendshipByID(id: string, userid: string) {
async getFriendshipByID(id: number | string, userid: number | string) {
try {
const statement = `SELECT * FROM recipin.cmp_userfriendships WHERE id = $1`;
const result = await pool.query(statement, [id]);
if (result.rows.length) {
const row = result.rows[0];
if (row.firstuserid == userid || row.seconduserid == userid) {
if (row.senderid == userid || row.targetid == userid) {
const sql = fs.readFileSync(appRoot + '/db/sql/get/friendshipbyid.sql').toString();
const formattedResult = await pool.query(sql, [id]);
if (formattedResult.rows.length) return { ok: true, code: 200, result: formattedResult.rows }
@@ -125,11 +125,11 @@ export class User {
}
}
async addFriendship(userid: string, targetid: string) {
async addFriendship(userid: number | string, targetid: number | string) {
try {
const statement = `
INSERT INTO recipin.cmp_userfriendships
(datecreated, active, pending, firstuserid, seconduserid)
(datecreated, active, pending, senderid, targetid)
VALUES ($1, false, true, $2, $3)
RETURNING *;
`
@@ -144,8 +144,15 @@ export class User {
}
}
async updateFriendship(id: string, data: { active: boolean, pending: boolean, dateterminated?: string }) {
async updateFriendship(id: number | string, userid: number | string, data: { active: boolean, pending: boolean, dateterminated?: string }) {
try {
const query = `SELECT * FROM recipin.cmp_userfriendships WHERE id = $1`;
const friendship = await pool.query(query, [id]);
if (!friendship.rows.length) return { ok: false, code: 404, result: "Friendship with this code not found" };
if (!(friendship.rows[0].active) && friendship.rows[0].senderid == userid) {
return { ok: false, code: 403, result: "Please wait for friend request to be accepted" }
}
const statement = `
UPDATE recipin.cmp_userfriendships
SET active = $1,
@@ -154,10 +161,10 @@ export class User {
WHERE id = $4
RETURNING *;
`
const values = [data.active, data.pending, data.dateterminated || null, id];
const values = [data.active, data.pending, (data.dateterminated || null), id];
const result = await pool.query(statement, values);
if (result.rows.length) return result.rows[0];
return null;
if (result.rows.length) return { ok: true, code: 200, result: result.rows[0] }
return { ok: false, code: 400, result: "Bad request" }
} catch (e: any) {
throw new Error(e);
}