diff --git a/src/controller/CommentController.js b/src/controller/CommentController.js index d08bb8c..16be82c 100644 --- a/src/controller/CommentController.js +++ b/src/controller/CommentController.js @@ -71,6 +71,7 @@ class CommentController { .send({ error: `Error while deleting topic.\n${err}` }); } } + static async likeComment(req, res) { try { const user = await User.findById(req.userId); @@ -104,6 +105,7 @@ class CommentController { return res.status(400).send({ error: `Error while commenting.${err}` }); } } + static async dislikeComment(req, res) { try { const comment = await Comment.findById(req.params.commentId); @@ -121,7 +123,7 @@ class CommentController { if (index > -1) { comment.likes.splice(index, 1); } - + comment.save(); await Like.findByIdAndRemove(like._id); const topicTrue = await Topic.findById(comment.topic).populate([ diff --git a/src/controller/MyPlantsController.js b/src/controller/MyPlantsController.js index 0949c20..49ed1a2 100644 --- a/src/controller/MyPlantsController.js +++ b/src/controller/MyPlantsController.js @@ -39,7 +39,7 @@ class MyPlantsController { try { const user = await User.findById(req.userId); const plant = await Plant.findById(req.params.plantId); - + const result = myPlantSchema.validate({ nickname: req.body.nickname }); if (result.error) { return res.status(400).send(result.error); @@ -52,7 +52,7 @@ class MyPlantsController { }); await user.myPlants.push(myPlant._id); await user.save(); - + return res.status(200).send({ myPlant }); } catch (err) { return res @@ -65,7 +65,7 @@ class MyPlantsController { try { const user = await User.findById(req.params.userId); const index = user.myPlants.indexOf(req.params.myPlantId); - + if (index > -1) { const myPlant = await MyPlant.findById(req.params.myPlantId); return res.send({ @@ -87,14 +87,14 @@ class MyPlantsController { static async updatePlant(req, res) { try { const newNick = req.body; - + const result = myPlantSchema.validate(newNick); if (result.error) { return res .status(400) .send({ error: `Error while editing plant. ${result.error}` }); } - + const myPlant = await MyPlant.findOneAndUpdate( { _id: req.params.myPlantId }, newNick, @@ -103,7 +103,7 @@ class MyPlantsController { new: true, } ); - + const newUser = await User.findById(myPlant.user).populate([ { path: 'topics', populate: 'plants' }, { path: 'myPlants', populate: 'plant' }, @@ -134,7 +134,7 @@ class MyPlantsController { { path: 'myPlants', populate: 'plant' }, { path: 'favorites', populate: 'plant' }, ]); - + return res.send(newUser); } catch (err) { return res diff --git a/src/controller/PlantController.js b/src/controller/PlantController.js index 7b9131a..57c1bc2 100644 --- a/src/controller/PlantController.js +++ b/src/controller/PlantController.js @@ -52,10 +52,10 @@ class PlantController { // Listagem de Todas as plantas static async fetchAll(req, res) { try { - const plants = await Plant.find().sort({ "topics": -1 }).populate([ - { path: 'topics' }, - ]); - return res.send( plants ); + const plants = await Plant.find() + .sort({ topics: -1 }) + .populate([{ path: 'topics' }]); + return res.send(plants); } catch (err) { return res.status(400).send({ error: 'Loading plants failed' }); } diff --git a/src/controller/TopicController.js b/src/controller/TopicController.js index df60013..c3e5170 100644 --- a/src/controller/TopicController.js +++ b/src/controller/TopicController.js @@ -9,29 +9,29 @@ class TopicController { try { const user = await User.findById(req.userId); const plant = await Plant.findById(req.params.plantId); - + const result = topicSchema.validate(req.body); - + if (result.error) { return res .status(400) .send({ error: `Error while creating topic. ${result.error}` }); } - + const topic = await Topic.create({ ...req.body, user: req.userId, plant: req.params.plantId, }); - + await topic.save(); - + user.topics.push(topic); await user.save(); - + plant.topics.push(topic); await plant.save(); - + return res.send({ topic }); } catch (err) { return res @@ -43,7 +43,7 @@ class TopicController { static async updateTopic(req, res) { try { const topic = await Topic.findById(req.params.topicId); - + const newData = req.body; if (!('title' in newData)) { @@ -60,7 +60,7 @@ class TopicController { .status(400) .send({ error: `Error while creating topic. ${result.error}` }); } - + const topicNew = await Topic.findOneAndUpdate( { _id: req.params.topicId }, newData, @@ -86,20 +86,20 @@ class TopicController { const topic = await Topic.findById(req.params.topicId); const user = await User.findById(topic.user); const plant = await Plant.findById(topic.plant); - + const indexAtUser = user.topics.indexOf(req.params.topicId); const indexAtPlant = plant.topics.indexOf(req.params.topicId); - + if (indexAtUser > -1) { user.topics.splice(indexAtUser, 1); } if (indexAtPlant > -1) { plant.topics.splice(indexAtPlant, 1); } - + user.save(); plant.save(); - + await Topic.findByIdAndRemove(req.params.topicId, { useFindAndModify: false, }); @@ -133,7 +133,7 @@ class TopicController { { path: 'user' }, { path: 'plant' }, ]); - + return res.send(topic); } catch (err) { return res @@ -141,7 +141,7 @@ class TopicController { .send({ error: `Error while find topic id.\n${err}` }); } } - + static async likeTopic(req, res) { try { const user = await User.findById(req.userId); @@ -170,12 +170,13 @@ class TopicController { return res.send(topictrue); } console.log(topic.likes.length); - + return res.send(topic); } catch (err) { return res.status(400).send({ error: `Error while commenting.${err}` }); } } + static async dislikeTopic(req, res) { try { const topic = await Topic.findById(req.params.topicId).populate([ @@ -205,7 +206,6 @@ class TopicController { } } - static async refreshTopicContents(res, topicId) { const topicTrue = await Topic.findById(topicId).populate( defaultTopicPopulate diff --git a/src/models/Plant.js b/src/models/Plant.js index 6daade4..ba6cca7 100644 --- a/src/models/Plant.js +++ b/src/models/Plant.js @@ -1,5 +1,4 @@ const mongoose = require('mongoose'); -const User = require('./User'); const PlantSchema = new mongoose.Schema({ scientificName: { diff --git a/src/models/User.js b/src/models/User.js index bd1246a..31f7bf4 100644 --- a/src/models/User.js +++ b/src/models/User.js @@ -12,7 +12,7 @@ const userSchema = new Schema({ type: String, required: true, }, - photo:{ + photo: { type: String, }, email: { diff --git a/src/routes/commentRoutes.js b/src/routes/commentRoutes.js index 351428d..643786b 100644 --- a/src/routes/commentRoutes.js +++ b/src/routes/commentRoutes.js @@ -1,7 +1,6 @@ const express = require('express'); const { auth } = require('../lib/auth'); const CommentController = require('../controller/CommentController'); -const LikeController = require('../controller/LikeController'); const router = new express.Router(); diff --git a/src/routes/topicRoutes.js b/src/routes/topicRoutes.js index eba0e87..19fa96f 100644 --- a/src/routes/topicRoutes.js +++ b/src/routes/topicRoutes.js @@ -2,7 +2,6 @@ const express = require('express'); const { auth } = require('../lib/auth'); const TopicController = require('../controller/TopicController'); - const router = new express.Router(); router.post('/create/:plantId', auth, TopicController.createTopic); router.put('/update/:topicId', TopicController.updateTopic);