forked from b00tc4mp/isdi-parttime-202309
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement retrieve fav recipes on app and api b00tc4mp#407
- Loading branch information
Showing
12 changed files
with
137 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
staff/belen-ivars/project/api/handlers/retrieveFavRecipesHandler.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { ContentError, NotFoundError } from 'com/errors.js' | ||
import logic from '../logic/index.js' | ||
import { validate } from "com" | ||
import jwt from 'jsonwebtoken' | ||
|
||
|
||
const retrieveFavRecipesHandler = async (req, res) => { | ||
const token = req.headers.authorization.substring(7) | ||
|
||
const payload = jwt.verify(token, process.env.JWT_SECRET) | ||
const { sub: userId } = payload | ||
console.log(userId) | ||
|
||
try { | ||
validate.id(userId, 'id') | ||
} catch (error) { | ||
|
||
return res.status(400).send() | ||
} | ||
|
||
try { | ||
const favRecipes = await logic.retrieveFavRecipes(userId) | ||
res.status(200).send(favRecipes) | ||
} catch (error) { | ||
if (error instanceof NotFoundError) { | ||
res.status(404).send(error) | ||
} | ||
else if (error instanceof ContentError) { | ||
res.status(400).send(error) | ||
} else { | ||
res.status(500).send(error) | ||
} | ||
} | ||
} | ||
|
||
export default retrieveFavRecipesHandler |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { NotFoundError } from "com/errors.js" | ||
import { Recipe, User } from "../data/models.js" | ||
|
||
async function retrieveFavRecipes(id) { | ||
|
||
const user = await User.findById(id) | ||
|
||
if (!user) { | ||
throw new NotFoundError('No user found') | ||
} | ||
|
||
console.log('user exists') | ||
const favRecipes = await Recipe.find({ _id: { $in: user.favs } }) | ||
|
||
favRecipes.forEach(recipe => { | ||
recipe.fav = user.favs.some(recipeObjectId => recipeObjectId.toString() === recipe.id) | ||
}) | ||
|
||
return favRecipes | ||
} | ||
|
||
export default retrieveFavRecipes |
15 changes: 15 additions & 0 deletions
15
staff/belen-ivars/project/api/logic/retrieveFavRecipes.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import dotenv from 'dotenv' | ||
dotenv.config() | ||
import mongoose from 'mongoose' | ||
|
||
import retrieveRecipes from './retrieveRecipes.js' | ||
|
||
(async () => { | ||
await mongoose.connect(process.env.MONGODB_URL) | ||
try { | ||
await retrieveRecipes('65f85ac7883fd3713c8bd3a3') | ||
console.log('fav recipes retrieved') | ||
} catch (error) { | ||
console.log(error) | ||
} | ||
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
staff/belen-ivars/project/app/src/logic/retrieveFavRecipes.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import session from "./session" | ||
import { API_URL } from "../utils/constants" | ||
import { errors } from "com" | ||
const { SystemError } = errors | ||
|
||
export default async function retrieveFavRecipes() { | ||
return (async () => { | ||
const req = { | ||
method: 'GET', | ||
headers: { | ||
Authorization: `Bearer ${session.token}` | ||
} | ||
} | ||
|
||
let res | ||
try { | ||
res = await fetch(`${API_URL}/recipes/favs`, req) | ||
} catch (error) { | ||
throw new SystemError(error.message) | ||
} | ||
|
||
if (!res.ok) { | ||
let body | ||
|
||
try { | ||
body = await res.json() | ||
} catch (error) { | ||
throw new SystemError(error.message) | ||
} | ||
|
||
throw new errors[body.error](body.message) | ||
} | ||
|
||
try { | ||
const favRecipes = await res.json() | ||
|
||
return favRecipes | ||
} catch (error) { | ||
throw new SystemError(error.message) | ||
} | ||
})() | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters