-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
I01 criacao conexao com mongo #4
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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,22 @@ | ||
{ | ||
"env": { | ||
"browser": true, | ||
"es2021": true | ||
}, | ||
"extends": [ | ||
"eslint:recommended", | ||
"plugin:react/recommended" | ||
], | ||
"parserOptions": { | ||
"ecmaFeatures": { | ||
"jsx": true | ||
}, | ||
"ecmaVersion": 12, | ||
"sourceType": "module" | ||
}, | ||
"plugins": [ | ||
"react" | ||
], | ||
"rules": { | ||
} | ||
} |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
yarn.lock | ||
node_modules | ||
node_modules | ||
package-lock.json |
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,16 @@ | ||
FROM node:alpine | ||
|
||
WORKDIR /usr/app | ||
|
||
COPY package.json ./ | ||
COPY index.js ./ | ||
|
||
RUN npm install | ||
|
||
RUN cat package.json | ||
|
||
RUN npm start | ||
|
||
EXPOSE 3000 | ||
|
||
# CMD npm start |
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 |
---|---|---|
@@ -1,9 +1,14 @@ | ||
const express = require('express'); | ||
const bodyParser = require('body-parser'); | ||
|
||
const mongoose = require('./src/database/index') | ||
|
||
const app = express(); | ||
|
||
app.use(bodyParser.json()); | ||
app.use(bodyParser.urlencoded({ extended: false })); | ||
|
||
app.listen(5000); | ||
require('./src/controllers/userController')(app); | ||
require('./src/controllers/recipeController')(app); | ||
|
||
app.listen(3001); |
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
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,79 @@ | ||
const express = require('express'); | ||
|
||
const Recipe = require('../models/recipeModel'); | ||
const User = require('../models/userModel'); | ||
|
||
const RecipeController = express.Router(); | ||
|
||
RecipeController.post('/register/:userId', async (req, res) => { | ||
try { | ||
const user = await User.findById(req.params.userId); //Pegando Id do usuario da url | ||
const recipe = await Recipe.create({ | ||
...req.body, | ||
user : user, | ||
}) | ||
await recipe.save(); //Salvando a receita criada | ||
user.recipes.push(recipe); //Adicionando a receita no array do usuario | ||
await user.save(); //Salvando o usuario antes de devolver a receita | ||
|
||
return res.status(201).send(recipe) | ||
} catch(err) { | ||
return res.status(400).send({ error: `${err}` }) | ||
} | ||
}); | ||
|
||
RecipeController.get('/findById/:recipeId', async (req, res) => { | ||
try { | ||
const recipe = await Recipe.findById(req.params.recipeId).populate([ | ||
{ path: 'user' }, | ||
]); //Pegando Id da receita da url | ||
|
||
return res.status(200).send(recipe) | ||
} catch(err) { | ||
return res.status(400).send({ error: `${err}` }) | ||
} | ||
}); | ||
|
||
RecipeController.get('/findAll', async (req, res) => { | ||
try { | ||
const recipes = await Recipe.find().populate([ | ||
{ path: 'user' }, | ||
]); //Pegando Id da receita da url | ||
|
||
return res.status(200).send(recipes) | ||
} catch(err) { | ||
return res.status(400).send({ error: `${err}` }) | ||
} | ||
}); | ||
|
||
RecipeController.delete('/delete/:recipeId', async (req, res) => { | ||
try { | ||
const recipe = await Recipe.findByIdAndDelete(req.params.recipeId); //Pegando Id da receita da url | ||
|
||
return res.status(200).send(recipe._id) | ||
} catch(err) { | ||
return res.status(400).send({ error: `${err}` }) | ||
} | ||
}); | ||
|
||
RecipeController.put('/update/:recipeId', async (req, res) => { | ||
try { | ||
const recipe = req.params.recipeId | ||
const update = req.body | ||
const updatedRecipe = await Recipe.findByIdAndUpdate( | ||
recipe, | ||
update, | ||
{ | ||
new: true, | ||
useFindAndModify: true, | ||
} | ||
); | ||
|
||
return res.status(200).send(updatedRecipe) | ||
} catch(err) { | ||
return res.status(400).send({ error: `${err}` }) | ||
} | ||
}); | ||
|
||
|
||
module.exports = app => app.use('/recipe', RecipeController); |
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,70 @@ | ||
const express = require('express'); | ||
|
||
const User = require('../models/userModel'); | ||
|
||
const AuthController = express.Router(); | ||
|
||
AuthController.post('/register', async (req, res) => { | ||
try { | ||
const user = await User.create(req.body) | ||
|
||
return res.status(201).send({ user }) | ||
} catch(err) { | ||
return res.status(400).send({ error: 'Registration failed' }) | ||
} | ||
}); | ||
|
||
AuthController.get('/findById/:userId', async (req, res) => { | ||
try { | ||
const user = await User.findById(req.params.userId).populate([ | ||
{ path: 'recipes' }, | ||
]); //Pegando Id da receita da url | ||
|
||
return res.status(200).send(user) | ||
} catch(err) { | ||
return res.status(400).send({ error: `${err}` }) | ||
} | ||
}); | ||
|
||
AuthController.get('/findAll', async (req, res) => { | ||
try { | ||
const users = await User.find(); //Pegando Id da receita da url | ||
|
||
return res.status(200).send(users) | ||
} catch(err) { | ||
return res.status(400).send({ error: `${err}` }) | ||
} | ||
}); | ||
|
||
AuthController.delete('/delete/:userId', async (req, res) => { | ||
try { | ||
const users = await User.findOneAndDelete(req.params.userId); //Pegando Id da receita da url | ||
|
||
return res.status(200).send({ message: `this user was deleted : ${users._id}`}) | ||
} catch(err) { | ||
return res.status(400).send({ error: `${err}` }) | ||
} | ||
}); | ||
|
||
|
||
AuthController.put('/update/:userId', async (req, res) => { | ||
try { | ||
const user = req.params.userId | ||
const update = req.body | ||
const updatedUser = await User.findByIdAndUpdate( | ||
user, | ||
update, | ||
{ | ||
new: true, | ||
useFindAndModify: true, | ||
} | ||
); | ||
|
||
return res.status(200).send(updatedUser) | ||
} catch(err) { | ||
return res.status(400).send({ error: `${err}` }) | ||
} | ||
}); | ||
|
||
|
||
module.exports = app => app.use('/user', AuthController); |
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,10 @@ | ||
const mongoose = require('mongoose'); | ||
|
||
mongoose.connect( | ||
'mongodb+srv://dev:dev12345@recipescluster.m9dnr.mongodb.net/recipes?retryWrites=true&w=majority', | ||
{ useNewUrlParser: true }, | ||
{ useUnifiedTopology: true }, | ||
); | ||
mongoose.Promise = global.Promise; | ||
|
||
module.exports = mongoose; |
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,30 @@ | ||
//User | ||
{ | ||
"name":"admin", | ||
"email": "admin@admin.com", | ||
"password": "admin" | ||
} | ||
//Recipe | ||
{ | ||
"title": "Batata recheada", | ||
"description": "Minha comida favorita", | ||
"difficulty": 3, | ||
"tags": [ | ||
"salgado", | ||
"batata", | ||
"assado" | ||
], | ||
"servings": 1, | ||
"estimatedTime": "1 hora", | ||
"ingredients": [ | ||
{ | ||
"name":"batata grande", | ||
"qty": "1" | ||
}, | ||
{ | ||
"name":"Strogonof", | ||
"qty": "10 colheres" | ||
} | ||
], | ||
"directions":"Assar a batata e rechear" | ||
} |
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,61 @@ | ||
const mongoose = require('mongoose'); | ||
|
||
const RecipeSchema = new mongoose.Schema({ | ||
title: { | ||
type: String, | ||
required: true, | ||
}, | ||
photo: { | ||
type: String, | ||
}, | ||
description: { | ||
type: String, | ||
required: true, | ||
}, | ||
difficulty: { | ||
type: Number, | ||
required: true, | ||
}, | ||
tags: [ | ||
{ | ||
type: String, | ||
required: true, | ||
}, | ||
], | ||
servings: { | ||
type: Number, | ||
required: true, | ||
}, | ||
estimatedTime: { | ||
type: String, | ||
required: true, | ||
}, | ||
createdAt: { | ||
type: Date, | ||
default: Date.now, | ||
}, | ||
user: { | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: 'User', | ||
}, | ||
ingredients: [ | ||
{ | ||
name: { | ||
type: String, | ||
require: true, | ||
}, | ||
qty: { | ||
type: String, | ||
require: true, | ||
} | ||
}, | ||
], | ||
directions: { | ||
type: String, | ||
required: true, | ||
}, | ||
}); | ||
|
||
const Recipe = mongoose.model('Recipe', RecipeSchema); | ||
|
||
module.exports = Recipe; |
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,43 @@ | ||
const mongoose = require('mongoose'); | ||
|
||
const UserSchema = new mongoose.Schema({ | ||
name: { | ||
type: String, | ||
required: true, | ||
}, | ||
email: { | ||
type: String, | ||
required: true, | ||
unique: true, | ||
}, | ||
password: { | ||
type: String, | ||
required: true, | ||
}, | ||
recipes: [ | ||
{ | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: 'Recipe', | ||
}, | ||
], | ||
favoriteRecipes: [ | ||
{ | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: 'Recipe', | ||
}, | ||
], | ||
recipesHistory: [ | ||
{ | ||
type: mongoose.Schema.Types.ObjectId, | ||
ref: 'Recipe', | ||
}, | ||
], | ||
createdAt: { | ||
type: Date, | ||
default: Date.now, | ||
}, | ||
}); | ||
|
||
const User = mongoose.model('User', UserSchema); | ||
|
||
module.exports = User; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nodemon é dependencia de desenvolvimento