get all accessible recipes

This commit is contained in:
Mikayla Dobson
2022-12-17 14:22:24 -06:00
parent 9a43f58ee3
commit 6c3f835b25
10 changed files with 54 additions and 18 deletions

View File

@@ -29,7 +29,9 @@ export default class RecipeCtl {
async getAllAccessible(id: string) {
try {
const result = await RecipeInstance.getAllAccessible(id);
const code = result !== null ? StatusCode.OK : StatusCode.NotFound;
return new ControllerResponse(code, (result || "No recipes currently accessible"));
} catch (e: any) {
throw new Error(e);
}

View File

@@ -79,9 +79,9 @@ export default class UserCtl {
}
}
async getPendingFriendRequests(senderid: string | number) {
async getPendingFriendRequests(recipient: string | number) {
try {
const { ok, code, result } = await UserInstance.getPendingFriendRequests(senderid);
const { ok, code, result } = await UserInstance.getPendingFriendRequests(recipient);
return new ControllerResponse(code, result);
} catch (e: any) {
throw new Error(e);
@@ -102,7 +102,7 @@ export default class UserCtl {
async updateFriendship(id: number | string, userid: number | string, data: { active: boolean, pending: boolean, dateterminated?: string }) {
try {
const { ok, code, result } = await UserInstance.updateFriendship(id, userid, data);
const { code, result } = await UserInstance.updateFriendship(id, userid, data);
return new ControllerResponse(code, result);
} catch (e: any) {
throw new Error(e);

View File

@@ -8,7 +8,7 @@ export default async function populate() {
INSERT INTO recipin.appusers
(firstname, lastname, handle, email, password, active, isadmin, datecreated, datemodified)
VALUES
('Mikayla', 'Dobson', 'innocuoussymmetry', 'mikaylaherself@gmail.com', 'password1', true, true, $1, $1),
('Mikayla', 'Dobson', 'innocuoussymmetry', 'mikaylaherself@gmail.com', '$2a$10$T9..JhNxfha86mQZNrwFo.CW7sR.d7w.9.T1M32aXL6r3vE2B.GhS', true, true, $1, $1),
('Emily', 'Dobson', 'emjdobson', 'emily@email.com', 'password2', true, false, $1, $1),
('Montanna', 'Dobson', 'delayedlemon', 'montanna@email.com', 'password3', true, false, $1, $1),
('Christine', 'Riley', 'christine', 'christine@email.com', 'password4', true, false, $1, $1),

View File

@@ -0,0 +1,9 @@
SELECT * FROM recipin.recipe
WHERE authoruserid = $1
OR authoruserid IN (
SELECT targetid FROM recipin.cmp_userfriendships
WHERE senderid = $1
UNION
SELECT senderid FROM recipin.cmp_userfriendships
WHERE targetid =$ 1
);

View File

@@ -1,4 +1,5 @@
import { IRecipe } from "../schemas";
import fs from 'fs';
import pool from "../db";
import { CollectionCtl } from "../controllers";
import now from "../util/now";
@@ -30,7 +31,24 @@ export class Recipe {
}
async getAllAccessible(id: string) {
try {
const statement = `
SELECT * FROM recipin.recipe
WHERE authoruserid = $1
OR authoruserid IN (
SELECT targetid FROM recipin.cmp_userfriendships
WHERE senderid = $1
UNION
SELECT senderid FROM recipin.cmp_userfriendships
WHERE targetid = $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 fetchRecipesByCollection(collectionid: string) {

View File

@@ -128,10 +128,10 @@ export class User {
}
}
async getPendingFriendRequests(senderid: number | string) {
async getPendingFriendRequests(recipient: number | string) {
try {
const statement = `SELECT * FROM recipin.cmp_userfriendships WHERE pending = true AND senderid = $1`
const result = await pool.query(statement, [senderid]);
const statement = `SELECT * FROM recipin.cmp_userfriendships WHERE pending = true AND targetid = $1`
const result = await pool.query(statement, [recipient]);
if (result.rows.length) return { ok: true, code: StatusCode.OK, result: result.rows }
return { ok: true, code: StatusCode.NotFound, result: "No pending friend requests found" }

View File

@@ -72,7 +72,7 @@ export const authRoute = (app: Express, passport: PassportStatic) => {
if (err) throw err;
})
res.clearCookie('userid');
res.status(204).redirect('/');
res.status(204).send({ ok: true });
} catch(e) {
next(e);
}

View File

@@ -51,7 +51,16 @@ export const friendRouter = (app: Express) => {
}
})
// update a friendship by its id
/**
* Update friendship by friendship ID
* allows user who received a friend request to confirm it
* expects body schema of:
* active: boolean
* pending: boolean
* dateterminated: string | null
* receives friendship ID from req.params and checks
* against current user ID from session
*/
router.put('/:id', async (req, res, next) => {
const data = req.body;
const { id } = req.params;

View File

@@ -12,15 +12,13 @@ import { cuisineRouter } from "./cuisine";
import { courseRouter } from "./course";
export const routes = async (app: Express, passport: PassportStatic) => {
console.log('routes called');
authRoute(app, passport);
userRoute(app);
friendRouter(app);
recipeRoute(app);
ingredientRoute(app);
// to do: refactor for ctlresponse
authRoute(app, passport);
collectionRoute(app);
subscriptionRoute(app);
groceryListRoute(app);

View File

@@ -28,12 +28,12 @@ export const recipeRoute = (app: Express) => {
try {
let result: CtlResponse<IRecipe[] | string>;
switch (filterby) {
case "allaccessible":
// result = await recipectl.getAllAccessible(user.id);
// break;
default:
case "myrecipes":
result = await recipectl.getAllAuthored(user.id);
break;
default:
result = await recipectl.getAllAccessible(user.id);
break;
}
res.status(result.code).send(result.data);