Skip to content

Commit

Permalink
implement retrieve fav recipes on app and api b00tc4mp#407
Browse files Browse the repository at this point in the history
  • Loading branch information
berlem committed Apr 21, 2024
1 parent 63a1d49 commit 46aab79
Show file tree
Hide file tree
Showing 12 changed files with 137 additions and 16 deletions.
4 changes: 3 additions & 1 deletion staff/belen-ivars/project/api/handlers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import retrieveRecipesHandler from './retrieveRecipesHandler.js'
import deleteRecipeHandler from './deleteRecipeHandler.js'
import editRecipeHandler from './editeRecipeHandler.js'
import toggleFavRecipeHandler from './toggleFavRecipeHandler.js'
import retrieveFavRecipesHandler from './retrieveFavRecipesHandler.js'


export {
Expand All @@ -16,5 +17,6 @@ export {
retrieveRecipesHandler,
deleteRecipeHandler,
editRecipeHandler,
toggleFavRecipeHandler
toggleFavRecipeHandler,
retrieveFavRecipesHandler
}
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
7 changes: 5 additions & 2 deletions staff/belen-ivars/project/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import {
retrieveRecipesHandler,
deleteRecipeHandler,
editRecipeHandler,
toggleFavRecipeHandler
toggleFavRecipeHandler,
retrieveFavRecipesHandler

} from './handlers/index.js'

Expand All @@ -37,11 +38,13 @@ mongoose.connect(process.env.MONGODB_URL)

server.get('/recipes/', retrieveRecipesHandler)

server.get('/recipes/favs', retrieveFavRecipesHandler)

server.delete('/recipes/:recipeId', deleteRecipeHandler)

server.patch('/recipes/:recipeId', jsonBodyParser, editRecipeHandler)

server.patch('/users/:recipeId', toggleFavRecipeHandler)
server.patch('/recipes/:recipeId/favs', toggleFavRecipeHandler)

server.listen(process.env.PORT, () => console.log(`server running on port ${process.env.PORT}`))
})
Expand Down
4 changes: 3 additions & 1 deletion staff/belen-ivars/project/api/logic/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import retrieveRecipes from './retrieveRecipes.js'
import deleteRecipe from './deleteRecipe.js'
import editRecipe from './editRecipe.js'
import toggleFavRecipe from './toggleFavRecipe.js'
import retrieveFavRecipes from './retrieveFavRecipes.js'

const logic = {
registerUser,
Expand All @@ -15,7 +16,8 @@ const logic = {
retrieveRecipes,
deleteRecipe,
editRecipe,
toggleFavRecipe
toggleFavRecipe,
retrieveFavRecipes
}

export default logic
22 changes: 22 additions & 0 deletions staff/belen-ivars/project/api/logic/retrieveFavRecipes.js
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 staff/belen-ivars/project/api/logic/retrieveFavRecipes.test.js
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)
}
})()
4 changes: 4 additions & 0 deletions staff/belen-ivars/project/api/logic/retrieveRecipes.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ async function retrieveRecipes(id) {
console.log('user exists')
const recipes = await Recipe.find()

recipes.forEach(recipe => {
recipe.fav = user.favs.some(recipeObjectId => recipeObjectId.toString() === recipe._id)
})

return recipes
}

Expand Down
4 changes: 2 additions & 2 deletions staff/belen-ivars/project/app/src/components/Recipes.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ function Recipes(props) {
(async () => {

try {
const content = await logic.retrieveRecipes()

const content = await props.showRecipes()
//props.showRecipes
setRecipes(content)
} catch (error) {
context.handleError(error)
Expand Down
4 changes: 3 additions & 1 deletion staff/belen-ivars/project/app/src/logic/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import retrieveRecipes from './retrieveRecipes'
import editRecipe from './editRecipe'
import deleteRecipe from './deleteRecipe'
import toggleFavRecipe from './toggleFavRecipe'
import retrieveFavRecipes from './retrieveFavRecipes'

const logic = {
registerUser,
Expand All @@ -19,7 +20,8 @@ const logic = {
retrieveRecipes,
editRecipe,
deleteRecipe,
toggleFavRecipe
toggleFavRecipe,
retrieveFavRecipes
}

export default logic
42 changes: 42 additions & 0 deletions staff/belen-ivars/project/app/src/logic/retrieveFavRecipes.js
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)
}
})()
}
7 changes: 0 additions & 7 deletions staff/belen-ivars/project/app/src/pages/FavsUser.jsx

This file was deleted.

4 changes: 2 additions & 2 deletions staff/belen-ivars/project/app/src/pages/Home.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ function Home(props) {

<Routes>
<Route path="/profile" element={<Profile />} />
<Route path='/favs' element={<FavsUser />} />
<Route path='/favs' element={<Recipes showRecipes={logic.retrieveFavRecipes} />} />
<Route path='/new-recipe' element={<NewRecipe />} />
<Route path='/' element={<Recipes stamp={stamp} />} />
<Route path='/' element={<Recipes showRecipes={logic.retrieveRecipes} stamp={stamp} />} />
</Routes>


Expand Down

0 comments on commit 46aab79

Please sign in to comment.