Skip to content

Commit

Permalink
modify files to apply new logics b00tc4mp#407
Browse files Browse the repository at this point in the history
  • Loading branch information
berlem committed Mar 25, 2024
1 parent edabdbd commit a5787ac
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 51 deletions.
55 changes: 30 additions & 25 deletions staff/belen-ivars/project/api/data/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,34 +47,39 @@ const recipe = new Schema({
type: String,
required: true
},
ingredients: [{
author: {
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']
},
time: {
type: Number,
ref: 'User',
required: true
},
date: {
type: Date,
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']
// },
// time: {
// type: Number,
// required: true
// },
// date: {
// type: Date,
// required: true
// }
})

const review = new Schema({
Expand Down
4 changes: 3 additions & 1 deletion staff/belen-ivars/project/api/handlers/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import registerUserHandler from './registerUserHandler.js'
import authenticateUserHandler from './authenticateUserHandler.js'
import retrieveUserHandler from './retrieveUserHandler.js'
import createRecipeHandler from './createRecipeHandler.js'


export {
registerUserHandler,
authenticateUserHandler,
retrieveUserHandler
retrieveUserHandler,
createRecipeHandler
}
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 @@ -2,13 +2,14 @@ import dotenv from 'dotenv'
dotenv.config()

import mongoose from 'mongoose'
import express from 'express'
import express, { json } from 'express'
import cors from 'cors'

import {
registerUserHandler,
authenticateUserHandler,
retrieveUserHandler
retrieveUserHandler,
createRecipeHandler

} from './handlers/index.js'

Expand All @@ -28,6 +29,8 @@ mongoose.connect(process.env.MONGODB_URL)

server.get('/users/:id', retrieveUserHandler)

server.post('/recipes', jsonBodyParser, createRecipeHandler)

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
@@ -1,11 +1,13 @@
import registerUser from './registerUser.js'
import authenticateUser from './authenticateUser.js'
import retrieveUser from './retrieveUser.js'
import createRecipe from './createRecipe.js'

const logic = {
registerUser,
authenticateUser,
retrieveUser
retrieveUser,
createRecipe
}

export default logic
36 changes: 20 additions & 16 deletions staff/belen-ivars/project/app/src/components/NewRecipeForm.jsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
import { Button, Form, Field, Container } from "../library"
import logic from "../logic"
import { useContext } from '../hooks'
import { useContext } from 'react'
import session from "../logic/session"
import Context from "../Context"

export default function NewRecipe(props) {
console.log('NewRecipe')

const context = useContext()
const context = useContext(Context)

const handleSubmit = event => {
const handleSubmit = async event => {
event.preventDefault()

const image = event.target.image.value
const text = event.target.text.value
const titleInput = event.target.querySelector('#title-input')
const textInput = event.target.querySelector('#description-input')
const imageInput = event.target.querySelector('#image-input')

const title = titleInput.value
const text = textInput.value
const image = imageInput.value
const author = session.sessionUserId

try {
logic.publishRecipe(image, text, error => {
if (error) {
context.handleError(error)
await logic.createRecipe(author, title, text, image)

return
}
props.onPublish()

props.onPublish()
})
} catch (error) {
context.handleError(error)
}
Expand All @@ -34,12 +37,13 @@ export default function NewRecipe(props) {
props.onCancel()
}

return <Container className="new-post">
<h2>New post</h2>
return <Container className="new-recipe">
<h2>New recipe</h2>

<Form onSubmit={handleSubmit}>
<Field id="image" type="url" >Image</Field>
<Field id="text" type="text">Text</Field>
<Field id="title-input" type="text" >Title</Field>
<Field id="description-input" type="text">Text</Field>
<Field id="image-input" type="url" >Image</Field>

<Button type="submit">Post</Button>
<Button onClick={handleCancel}>Cancel</Button>
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 @@ -3,13 +3,15 @@ import loginUser from './loginUser'
import logoutUser from './logoutUser'
import retrieveUser from './retrieveUser'
import isUserLoggedIn from './isUserLoggedIn'
import createRecipe from './createRecipe'

const logic = {
registerUser,
loginUser,
logoutUser,
retrieveUser,
isUserLoggedIn
isUserLoggedIn,
createRecipe
}

export default logic
6 changes: 1 addition & 5 deletions staff/belen-ivars/project/app/src/logic/retrieveUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { API_URL } from '../utils/constants'
import { SystemError } from 'com/errors'

export default async function retrieveUser() {

// no faltaria passar el paràmetre de l'id?
return (async () => {
const req = {
method: 'GET',
Expand Down Expand Up @@ -40,8 +40,4 @@ export default async function retrieveUser() {
throw new SystemError(error.message)
}
})()
// return Promise.resolve({
// name: "Belén",
// email: "[email protected]"
// })
}
3 changes: 3 additions & 0 deletions staff/belen-ivars/project/app/src/pages/Home.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Profile from './Profile'
import FavsUser from "./FavsUser"
import FullHeader from "../components/FullHeader"
import Context from "../Context"
import NewRecipe from "../components/NewRecipeForm"


function Home(props) {
Expand Down Expand Up @@ -75,6 +76,8 @@ function Home(props) {
<Routes>
<Route path="/profile" element={<Profile />} />
<Route path='/favs' element={<FavsUser />} />
<Route path='/new-recipe' element={<NewRecipe />} />



</Routes>
Expand Down

0 comments on commit a5787ac

Please sign in to comment.