Skip to content

Commit

Permalink
Merge pull request #19 from fga-eps-mds/i11_Visualizar_informacoes_so…
Browse files Browse the repository at this point in the history
…bre_a_planta

I11 visualizar informacoes sobre a planta
  • Loading branch information
rafaelmakaha authored Sep 28, 2020
2 parents 26d9dbe + d15a6f8 commit b8f854b
Show file tree
Hide file tree
Showing 12 changed files with 1,673 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/node_modules
.env
package-lock.json
yarn.lock
yarn.lock
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@
},
"homepage": "https://github.com/fga-eps-mds/2020.1-Grupo2-backend#readme",
"dependencies": {
"body-parser": "^1.19.0",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"joi": "^17.2.1",
"mongodb": "^3.6.2",
"mongoose": "^5.10.5",
"morgan": "^1.10.0"
},
Expand Down
5 changes: 5 additions & 0 deletions src/config/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
app : {
port: process.env.PORT || 3000,
},
};
14 changes: 11 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
require('dotenv').config();
const config = require('./config')
const express = require('express');
const app = express();
const morgan = require('morgan');
const mongoose = require('mongoose');

const itemRoutes = require('./routes/itemRoutes');
const plantRoutes = require('./routes/plantRoutes');
const topicRoutes = require('./routes/topicRoutes');
const commentRoutes = require('./routes/commentRoutes');
const authRoutes = require('./routes/authRoutes');

// MongoDB connection

//mongodb://localhost:27017/noderest => meu banco de dados local polupado
//mongodb://mongo:27017/backend => banco de dados da develop
mongoose
.connect(
'mongodb://mongo:27017/backend',
Expand All @@ -15,9 +23,6 @@ mongoose
.then(() => console.log('MongoDB Connected'))
.catch(err => console.log(err));

// settings
app.set('port', process.env.PORT || 3000);
app.set('json spaces', 2);

// middlewares
app.use(morgan('dev'));
Expand All @@ -26,6 +31,9 @@ app.use(express.json());

// routes
app.use('/item',itemRoutes);
app.use('/plant',plantRoutes);
app.use('/topic',topicRoutes);
app.use('/comment',commentRoutes);
app.use('/auth',authRoutes);

// starting the server
Expand Down
36 changes: 36 additions & 0 deletions src/models/Comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const mongoose = require('mongoose');

const CommentSchema = new mongoose.Schema({
username: {
type: String,
require : true
},
message: {
type: String,
require: true
},
vote_count:{
type: Number,
require:true
}

});


const Comment = mongoose.model('comment',CommentSchema);

module.exports = Comment;
//Exemplo de teste insominia
//{
// "scientificName":"scientificName",
// "family_name":"family_name",
// "gender_name":"gender_name",
// "specie_name":"specie_name",
// "usage":"usage",
// "first_User":"first_User",
// "collection_count":"17",
// "extinction":"1",
// "profile_picture":"7",
// "gbifID":"17",
// "stateProvince":"brasilia"
//}
63 changes: 63 additions & 0 deletions src/models/Plant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const mongoose = require('mongoose');

const PlantSchema = new mongoose.Schema({
scientificName: {
type: String,
require: true,
unique: true
},
family_name: {
type: String,
require: true
},
gender_name: {
type: String,
require: true
},
specie_name: {
type: String,
require: true
},
common_name: {
type: String,
require: true
},
usage: {
type: String,
require: true
},
first_User: {
type: String,
require: true
},
collection_count: {
type: Number,
require: true
},
extinction: {
type: Boolean,
require: true
},
profile_picture: {
type: String,
require : true
},
gbifID: {
type: Number,
require: true,
},
stateProvince: {
type: String,
require : true
},
topicos: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'topic',

}],
});


const Plant = mongoose.model('plant',PlantSchema);

module.exports = Plant;
21 changes: 21 additions & 0 deletions src/models/Topico.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const mongoose = require('mongoose');

const TopicoSchema = new mongoose.Schema({
plant:{
type:mongoose.Schema.Types.ObjectId,
ref:'plant',
require:true

},
description: {
type: String,
require: true
}

});



const Topico = mongoose.model('topic',TopicoSchema);

module.exports = Topico;
19 changes: 19 additions & 0 deletions src/routes/commentRoutes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const express = require('express');
const Plant = require('../models/Plant');
const Topico = require('../models/Topico');
const Comment = require('../models/Comment');

const router = express.Router();

router.post('/register', async (req , res) => {
try{
const comment = await Comment.create(req.body);

return res.send({ comment });
}catch (err){
return res.status(400).send({ error: 'Registration failed'});
}
});


module.exports = router;
90 changes: 90 additions & 0 deletions src/routes/plantRoutes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
const express = require('express');

const Plant = require('../models/Plant');
const Topico = require('../models/Topico');

const router = express.Router();

//registro de uma nova planta
router.post('/register', async (req , res) => {
try{
const { scientificName,family_name,gender_name,specie_name,common_name,usage,first_User,collection_count,extinction,profile_picture, gbifID,stateProvince,topicos} = req.body;

const plant = await Plant.create({scientificName,family_name,gender_name,specie_name,common_name,usage,first_User,collection_count,extinction,profile_picture, gbifID,stateProvince});

// await Promise.all(topicos.map(async topico =>{
// const plantTopic = new Topico({...topico,plant : plant._id});

// await plantTopic.save();

// plant.topicos.push(plantTopic);
// }));

await plant.save();

return res.send({ plant });
}catch (err){
return res.send(err);
}
});
//Listagem de Todas as plantas
router.get('/', async (req , res) => {
try{
const plants = await Plant.find().populate('topicos');

return res.send({ plants });
}catch (err){
return res.status(400).send({ error: 'Loading plants failed'});
}
});
//Procurando planta por id
router.get('/:plantId', async (req , res) => {
try{
const plant = await Plant.findById(req.params.plantId).populate('topicos');

return res.send({ plant });
}catch (err){
return res.status(400).send({ error: 'error when searching for this plant '});
}
});
//Deletando planta por id
router.delete('/:plantId', async (req , res) => {
try{
const deleted = await Plant.findByIdAndRemove(req.params.plantId);

return res.send(deleted);
}catch (err){
return res.status(400).send({ error: 'Error when Delete this plant'});
}
});
//Dando upgrade planta por id
router.put('/:plantId', async (req , res) => {

try{
const { scientificName,family_name,gender_name,specie_name,common_name,usage,first_User,collection_count,extinction,profile_picture, gbifID,stateProvince,topicos} = req.body;

const plant = await Plant.findByIdAndUpdate(req.params.plantId,
{scientificName,family_name,gender_name,specie_name,common_name,usage,first_User,collection_count,extinction,profile_picture, gbifID,stateProvince},{ new: true});

plant.topicos = [];
await Topico.remove({plant: plant._id});

await Promise.all(topicos.map(async topico =>{
const plantTopic = new Topico({...topico,plant : plant._id});

await plantTopic.save();

plant.topicos.push(plantTopic);
}));

await plant.save();


return res.send({ plant });
}catch (err){
return res.status(400).send({ error: 'Registration failed'});
}

});

module.exports = router;
63 changes: 63 additions & 0 deletions src/routes/topicRoutes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const express = require('express');
const Plant = require('../models/Plant');
const Topico = require('../models/Topico');

const router = express.Router();
//Criar um novo topico pelo id da planta
router.put('/:plantId', async (req , res) => {
try{
const {topicos} = req.body;
const plant = await Plant.findByIdAndUpdate(req.params.plantId,
{},{ new: true}).populate('topicos');
await Promise.all(topicos.map(async topico =>{
const plantTopic = new Topico({...topico,plant : plant._id});

await plantTopic.save();

plant.topicos.push(plantTopic);
}));
await plant.save();
return res.send({ plant });
}catch (err){
return res.status(400).send({ error: 'Registration failed'});
}
});
//Listando todos os topics
router.get('/', async (req , res) => {
try{
const topics = await Topico.find();
return res.send({ topics });
}catch (err){
return res.status(400).send({ error: 'Loading plants failed'});
}
});
//Procurando topic por id
router.get('/:topicId', async (req , res) => {
try{
const topico = await Topico.findById(req.params.topicId);
return res.send({ topico });
}catch (err){
return res.status(400).send({ error: 'error when searching for this topic '});
}
});
//Deletando topic por id
router.delete('/:topicId', async (req , res) => {
try{
await Topico.findByIdAndRemove(req.params.topicId);
return res.send();
}catch (err){
return res.status(400).send({ error: 'Error when Delete this topic'});
}
});
//Dando upgrade topic por id
router.put('/:topicId', async (req , res) => {
try{
await Topico.findByIdAndUpdate(req.params.topicId,{description: 'marcos felipe'},{new : true});
return res.send();
}catch (err){
console.log(err);
return res.status(400).send({ error: 'Error when Delete this plant'});
}
});

module.exports = router;
Loading

0 comments on commit b8f854b

Please sign in to comment.