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 get ingredients from database b00tc4mp#407
- Loading branch information
Showing
4 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
staff/belen-ivars/project/api/handlers/getIngredientsListHandler.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,28 @@ | ||
import logic from '../logic/index.js' | ||
import { errors } from 'com' | ||
const { DuplicityError, ContentError } = errors | ||
import jwt from 'jsonwebtoken' | ||
|
||
export default async function getIngredientsListHandler(req, res) { | ||
|
||
const token = req.headers.authorization.substring(7) | ||
|
||
const payload = jwt.verify(token, process.env.JWT_SECRET) | ||
const { sub: userId } = payload | ||
|
||
const recipeId = req.params.recipeId | ||
|
||
try { | ||
const ingredients = await logic.getIngredientsList(userId, recipeId) | ||
res.status(201).send(ingredients) | ||
|
||
} catch (error) { | ||
let status = 500 | ||
if (error instanceof ContentError || error instanceof TypeError) | ||
status = 500 | ||
if (error instanceof DuplicityError) | ||
status = 409 | ||
|
||
res.status(status).json({ error: error.constructor.name, message: error.message }) | ||
} | ||
} |
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,40 @@ | ||
import { SystemError } from 'com/errors.js' | ||
import { Ingredient, Recipe, User } from '../data/models.js' | ||
|
||
async function getIngredientsList(userId, recipeId) { | ||
|
||
const user = await User.findById(userId) | ||
|
||
if (!user) { | ||
throw new NotFoundError('No user found') | ||
} | ||
|
||
let recipe | ||
try { | ||
recipe = await Recipe.findById(recipeId) | ||
console.log(recipe) | ||
} catch (error) { | ||
throw new SystemError(error.message) | ||
} | ||
console.log(recipe.ingredients) | ||
if (!recipe || !recipe.ingredients) { | ||
return [] | ||
} | ||
|
||
const ingredients = [] | ||
|
||
for (let ingredientId of recipe.ingredients) { | ||
try { | ||
const ingredient = await Ingredient.findById(ingredientId) | ||
|
||
if (ingredient) | ||
ingredients.push(ingredient.name) | ||
console.log('estic en lògica backend') | ||
} catch (error) { | ||
throw new SystemError(error.message) | ||
} | ||
} | ||
return ingredients | ||
} | ||
|
||
export default getIngredientsList |
15 changes: 15 additions & 0 deletions
15
staff/belen-ivars/project/api/logic/getIngredientsList.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, { mongo } from 'mongoose' | ||
import createIngredientsList from './getIngredientsList.js' | ||
|
||
(async () => { | ||
await mongoose.connect(process.env.MONGODB_URL) | ||
try { | ||
const result = await createIngredientsList('65d655fac1dd88f9aee917d6', '6627c93094dfc90e1e112541') | ||
|
||
console.log(result, 'ingredients have been listed') | ||
} catch (error) { | ||
console.log(error) | ||
} | ||
})() |
45 changes: 45 additions & 0 deletions
45
staff/belen-ivars/project/app/src/logic/getIngredientsList.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,45 @@ | ||
import session from "./session" | ||
import { API_URL } from "../utils/constants" | ||
import { errors } from "com" | ||
const { SystemError } = errors | ||
|
||
async function getIngredientsList(recipeId) { | ||
return (async () => { | ||
const req = { | ||
method: 'GET', | ||
headers: { | ||
Authorization: `Bearer ${session.token}` | ||
} | ||
} | ||
|
||
let res | ||
try { | ||
res = await fetch(`${API_URL}/recipe/${recipeId}`, 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 ingredientsList = await res.json() | ||
console.log('estic en lògica front') | ||
return ingredientsList | ||
} catch (error) { | ||
throw new SystemError(error.message) | ||
} | ||
})() | ||
|
||
} | ||
|
||
export default getIngredientsList |