Skip to content

Commit

Permalink
implement get ingredients from database b00tc4mp#407
Browse files Browse the repository at this point in the history
  • Loading branch information
berlem committed Apr 24, 2024
1 parent 81643ec commit d54f991
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 0 deletions.
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 })
}
}
40 changes: 40 additions & 0 deletions staff/belen-ivars/project/api/logic/getIngredientsList.js
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 staff/belen-ivars/project/api/logic/getIngredientsList.test.js
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 staff/belen-ivars/project/app/src/logic/getIngredientsList.js
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

0 comments on commit d54f991

Please sign in to comment.