Skip to content

Commit

Permalink
create recipe with more parameters b00tc4mp#407
Browse files Browse the repository at this point in the history
  • Loading branch information
berlem committed Apr 22, 2024
1 parent c7eb045 commit cdd5ce8
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 37 deletions.
40 changes: 20 additions & 20 deletions staff/belen-ivars/project/api/data/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,26 +52,26 @@ const recipe = new Schema({
ref: 'User',
required: true
},
// ingredients: [{
// type: ObjectId,
// required: true,
// ref: 'Ingredient'
// }],
// diet: {
// type: String,
// required: true,
// enum: ['glutenfree', 'vegetarian', 'vegan', 'omnivorous']
// },
// complexity: {
// type: Number,
// required: true,
// enum: [1, 2, 3, 4, 5]
// },
// method: {
// type: String,
// required: true,
// enum: ['steamed', 'oven', 'microwave', 'grill', 'fresh']
// },
ingredients: [{
type: ObjectId,
required: true,
ref: 'Ingredient'
}],
diet: {
type: String,
required: true,
enum: ['glutenfree', 'vegetarian', 'vegan']
},
complexity: {
type: String,
required: true,
enum: ['easy', 'regular', 'complex']
},
method: {
type: String,
required: true,
enum: ['steamed', 'oven', 'microwave', 'grill', 'fresh']
}
// time: {
// type: Number,
// required: true
Expand Down
17 changes: 13 additions & 4 deletions staff/belen-ivars/project/api/handlers/createRecipeHandler.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import { ContentError, NotFoundError } from "com/errors.js";
import logic from "../logic/index.js";
import { errors } from 'com'
import jwt from 'jsonwebtoken'

const createRecipeHandler = async (req, res) => {

const token = req.headers.authorization.substring(7)

const { sub: author } = jwt.verify(token, process.env.JWT_SECRET)

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

export default async (req, res) => {
const { author, title, description, image } = req.body

try {
await logic.createRecipe(author, title, description, image)
await logic.createRecipe(author, title, description, image, ingredients, diet, complexity, method)

res.status(201).send()
} catch (error) {
Expand All @@ -18,4 +25,6 @@ export default async (req, res) => {

res.status(status).json({ error: error.constructor.name, message: error.message })
}
}
}

export default createRecipeHandler
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Ingredient } from '../data/models.js'
(async () => {
await mongoose.connect(process.env.MONGODB_URL)
try {
const result = await checkIngredient('65d655fac1dd88f9aee917d6', 'sal')
await checkIngredient('65d655fac1dd88f9aee917d6', 'sal')

} catch (error) {
console.log(error)
Expand Down
21 changes: 17 additions & 4 deletions staff/belen-ivars/project/api/logic/createRecipe.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { validate, errors } from 'com'
import { Recipe, User } from '../data/models.js'
const { ContentError, NotFoundError } = errors
import checkIngredient from './checkIngredient.js'
const { SystemError, NotFoundError } = errors

async function createRecipe(userId, title, description, image) {
async function createRecipe(userId, title, description, image, ingredients, diet, complexity, method) {
validate.text(title, 'title')
validate.text(description, 'description')
validate.text(image, 'image')
Expand All @@ -14,13 +15,25 @@ async function createRecipe(userId, title, description, image) {
throw new NotFoundError('user not found')
console.log('user founded')

let ingredientsOfThisRecipe = []

try {
for (let ingredient of ingredients) {
const result = await checkIngredient(user.id, ingredient)
ingredientsOfThisRecipe.push(result._id)
}
} catch (error) {
throw new SystemError('ingredients cannnot be pushed')
}

let recipe

try {
recipe = await Recipe.create({ author: userId, title, description, image })
recipe = await Recipe.create({ author: user.id, title, description, image, ingredients: ingredientsOfThisRecipe, diet, complexity, method })
} catch (error) {
throw new ContentError('recipe cannot be published')
throw new SystemError(error.message)
}
return recipe
}

export default createRecipe
7 changes: 3 additions & 4 deletions staff/belen-ivars/project/api/logic/createRecipe.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import dotenv from 'dotenv'
dotenv.config()
import mongoose from 'mongoose'

import mongoose, { mongo } from 'mongoose'
import createRecipe from './createRecipe.js'

(async () => {
await mongoose.connect(process.env.MONGODB_URL)
try {
await createRecipe('65d655fac1dd88f9aee917d6', 'Colors', 'Persona con pintura corporal', 'https://www.pexels.com/es-es/foto/persona-con-pintura-corporal-1209843/')
await createRecipe('65d655fac1dd88f9aee917d6', 'Aguacate relleno', 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi ut neque ut ante mollis ultricies sed sit amet lacus. Nam dignissim urna vel porttitor placerat.', 'https://content.elmueble.com/medio/2023/03/31/aguacate-relleno-de-huevo-y-beicon_00000000_230821131859_1200x1798.jpg', ['aguacate', 'huevo', 'sal'], 'glutenfree', 'easy', 'grill')

console.log('recipe has been published')
console.log('recipe has been created')
} catch (error) {
console.log(error)
}
Expand Down
18 changes: 16 additions & 2 deletions staff/belen-ivars/project/app/src/components/NewRecipeForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,23 @@ export default function NewRecipe(props) {
const titleInput = event.target.querySelector('#title-input')
const textInput = event.target.querySelector('#description-input')
const imageInput = event.target.querySelector('#image-input')
const ingredientsInput = event.target.querySelector('#ingredients-input')
const dietInput = event.target.querySelector('#diet-input')
const complexityInput = event.target.querySelector('#complexity-input')
const methodInput = event.target.querySelector('#method-input')


const title = titleInput.value
const text = textInput.value
const image = imageInput.value
const author = session.sessionUserId
const ingredients = ingredientsInput.value.split(',' || '-' || '.')
const diet = dietInput.value
const complexity = complexityInput.value
const method = methodInput.value


try {
await logic.createRecipe(author, title, text, image)
await logic.createRecipe(title, text, image, ingredients, diet, complexity, method)

props.onPublish()

Expand All @@ -44,6 +53,11 @@ export default function NewRecipe(props) {
<Field id="title-input" type="text" >Title</Field>
<Field id="description-input" type="text">Description</Field>
<Field id="image-input" type="url" >Image</Field>
<Field id="ingredients-input" type="text" placeholder='example1, example2,...'>Ingredients</Field>
<Field id="diet-input" type="text" >Diet</Field>
<Field id="complexity-input" type="text" >Complexity</Field>
<Field id="method-input" type="text" >Method</Field>


<Button type="submit">Post</Button>
<Button onClick={handleCancel}>Cancel</Button>
Expand Down
4 changes: 2 additions & 2 deletions staff/belen-ivars/project/app/src/logic/createRecipe.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 createRecipe(author, title, description, image) {
export default function createRecipe(title, description, image, ingredients, diet, complexity, method) {
return (async () => {
const req = {
method: 'POST',
headers: {
Authorization: `Bearer ${session.token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({ author, title, description, image })
body: JSON.stringify({ title, description, image, ingredients, diet, complexity, method })
}

let res
Expand Down

0 comments on commit cdd5ce8

Please sign in to comment.