Skip to content

Commit

Permalink
refactor edit logic b00tc4mp#407
Browse files Browse the repository at this point in the history
  • Loading branch information
berlem committed Apr 25, 2024
1 parent 5b5e68f commit df5e23d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ const editRecipeHandler = async (req, res) => {

const recipeId = req.params.recipeId

const { title, description, image } = req.body
const { title, description, image, ingredients, diet, complexity, method } = req.body

try {
await logic.editRecipe(userId, recipeId, title, description, image)
await logic.editRecipe(userId, recipeId, title, description, image, ingredients, diet, complexity, method)

res.status(204).send()
} catch (error) {
Expand Down
61 changes: 60 additions & 1 deletion staff/belen-ivars/project/api/logic/editRecipe.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { validate } from "com"
import { User, Recipe } from '../data/models.js'
import { NotFoundError, ContentError, SystemError } from "com/errors.js"
import checkIngredient from "./checkIngredient.js"


async function editRecipe(userId, recipeId, title, description, image) {
async function editRecipe(userId, recipeId, title, description, image, ingredients, diet, complexity, method) {
validate.id(userId, 'user id')
validate.id(recipeId, 'recipe id')

Expand All @@ -12,20 +13,24 @@ async function editRecipe(userId, recipeId, title, description, image) {
//if (image) validate.image pròximament

let user

try {
user = await User.findById(userId)
} catch (error) {
throw new SystemError(error.message)
}

if (!user)
throw new NotFoundError('user not found')

let recipe

try {
recipe = await Recipe.findById(recipeId)
} catch (error) {
throw new SystemError(error.message)
}

if (!recipe)
throw new NotFoundError('recipe not found')

Expand All @@ -38,13 +43,15 @@ async function editRecipe(userId, recipeId, title, description, image) {
} catch (error) {
throw new SystemError(error.message)
}
console.log('títol bé')

if (description)
try {
recipeUpdated = await Recipe.findOneAndUpdate({ _id: recipe.id }, { description: description })
} catch (error) {
throw new SystemError(error.message)
}
console.log('descripció')


if (image)
Expand All @@ -53,6 +60,58 @@ async function editRecipe(userId, recipeId, title, description, image) {
} catch (error) {
throw new SystemError(error.message)
}
console.log('image')


if (ingredients) {
let ingredientsOfThisRecipe = []
try {
const ingredientList = ingredients.split(', ')
for (let ingredient of ingredientList) {
const result = await checkIngredient(user.id, ingredient)
ingredientsOfThisRecipe.push(result._id)
}
} catch (error) {
throw new SystemError(error.message)
}

try {
console.log(recipe.id, 'hay recipe.id?')
recipeUpdated = await Recipe.findOneAndUpdate({ _id: recipe.id }, { ingredients: ingredientsOfThisRecipe })
} catch (error) {
throw new SystemError(error.message)
}
console.log('ingredients')
}


if (diet)
try {
recipeUpdated = await Recipe.findOneAndUpdate({ _id: recipe.id }, { diet: diet })
} catch (error) {
throw new SystemError(error.message)
}
console.log('dieta')


if (complexity)
try {
recipeUpdated = await Recipe.findOneAndUpdate({ _id: recipe.id }, { complexity: complexity })
} catch (error) {
throw new SystemError(error.message)
}
console.log('complexitat')


if (method)
try {
recipeUpdated = await Recipe.findOneAndUpdate({ _id: recipe.id }, { method: method })
} catch (error) {
throw new SystemError(error.message)
}
console.log('mètode')



} catch (error) {
throw new ContentError('recipe cannot be edited')
Expand Down
4 changes: 2 additions & 2 deletions staff/belen-ivars/project/app/src/logic/editRecipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import session from "./session"
const { SystemError } = errors


export default function editRecipe(recipeId, title, description, image) {
export default function editRecipe(recipeId, title, description, image, ingredients, diet, complexity, method) {
return (async () => {
const req = {
method: 'PATCH',
headers: {
Authorization: `Bearer ${session.token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({ title, description, image })
body: JSON.stringify({ title, description, image, ingredients, diet, complexity, method })
}

let res
Expand Down

0 comments on commit df5e23d

Please sign in to comment.