Skip to content
Snippets Groups Projects
Commit 685959d8 authored by Mariana Molina's avatar Mariana Molina
Browse files

Termino la API localmente

parent a2334768
No related branches found
No related tags found
No related merge requests found
class Comment {
constructor(id, user, text) {
this.id = id;
this.user = user;
this.text = text;
this.comment = [];
}
getId() {
return this.id;
}
getUser() {
return this.user;
}
getText() {
return this.text;
}
getComments() {
return this.comment;
}
setNewComment(id) {
this.comment.push(id);
}
}
\ No newline at end of file
exports.comments = [{id: '0'}];
let User = {
name:'',
email: ''
};
\ No newline at end of file
exports.users = [
{
email: 'email1',
comment: []
}
];
\ No newline at end of file
const express = require('express');
const router = express.Router();
const comments = require('../models/Comment').comments;
const users = require('../models/User').users;
// [POST] CREAR UN COMENTARIO: /comment
router.get('/comment', (req, res) => {
res.send('Comentarios');
let commentId = 1;
// [GET] OBTENER UN COMENTARIO: /comment/:id
router.get('/comment/:id', (req, res) => {
if (comments) {
// Busco el comentario con id = parámetro id del request
const id = req.params.id;
const comment = comments.find((data) => data.id === id);
if (!comment) {
res.status(500).send('Comentario no encontrado.');
} else {
res.json(comment);
}
} else {
res.status(500).send('No se ha agregado ningún comentario aún.');
}
});
// [PUT] AGREGAR UN COMENTARIO A UN COMENTARIO: /comment/add
router.get('/comment/add', (req, res) => {
res.send('Comentarios');
// [POST] CREAR UN COMENTARIO: /comment
router.post('/comment', (req, res) => {
// Si tengo un body en el request y tiene un usuario, agrego el comentario con una lista de comentarios vacía
if (req.body) {
if (req.body.user) {
const user = req.body.user.toString();
if (users) {
const foundUser = users.find(data => data.email === user);
if (foundUser) {
if (req.body.text) {
const id = commentId.toString();
commentId += 1;
const text = req.body.text.toString();
const newComment = {id: id, user: user, text: text, comment: [] };
console.log(newComment);
comments.push(newComment);
res.json(comments);
} else {
res.status(500).send('El comentario no debe ser vacío.');
}
} else {
res.status(500).send('El usuario autor del comentario no existe.');
}
} else {
res.status(500).send('El usuario autor del comentario no existe.');
}
} else {
res.status(500).send('El comentario debe pertenecer a un usuario.');
}
} else {
res.status(500).send('Se produjo un error al procesar la solicitud.');
}
});
// [GET] OBTENER TODOS LOS COMENTARIOS: /comment
router.get('/comment', (req, res) => {
res.send('Comentarios');
// [PUT] AGREGAR UN COMENTARIO A UN COMENTARIO: /comment
router.put('/comment/:id', (req, res) => {
if (comments) {
// Busco el comentario con id = parámetro id del request
const id = req.params.id;
const comment = comments.find((data) => data.id === id);
if (!comment) {
res.status(500).send('Comentario no encontrado.');
} else {
// *********** Empieza a agregar el comentario ***********
if (req.body) {
if (req.body.user) {
const user = req.body.user.toString();
if (users) {
const foundUser = users.find(data => data.email === user);
if (foundUser) {
if (req.body.text) {
const id = commentId.toString();
commentId += 1;
const text = req.body.text.toString();
// Creo el nuevo comentario
const newComment = {id: id, user: user, text: text, comment: [] };
console.log(newComment);
// Agrego el nuevo comentario a la lista de TODOS los comentarios
comments.push(newComment);
// Agrego el nuevo comentario a la lista de los comentarios del comentario :id
comment.comment.push({id: id});
res.json(comments);
} else {
res.status(500).send('El comentario no debe ser vacío.');
}
} else {
res.status(500).send('El usuario autor del comentario no existe.');
}
} else {
res.status(500).send('El usuario autor del comentario no existe.');
}
} else {
res.status(500).send('El comentario debe pertenecer a un usuario.');
}
} else {
res.status(500).send('Se produjo un error al procesar la solicitud.');
}
// ************ Termina **************
}
} else {
res.status(500).send('No se ha agregado ningún comentario aún.');
}
});
module.exports = router;
\ No newline at end of file
const express = require('express');
const bodyParser = require('body-parser');
const users = require('../models/User').users;
const app = express();
const router = express.Router();
const users = [
{
email: 'email1',
comment: []
}
];
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// [GET] OBTENER TODOS LOS USUARIOS: /users
router.get('/users', (req, res) => {
res.json(users);
if (users) {
res.json(users);
} else {
res.status(500).send('No se ha agregado ningún usuario aún.');
}
});
// [GET] OBTENER UN USUARIO: /user/:id
router.get('/user/:id', (req, res) => {
const userEmail = req.params.id;
const user = users.find((data) => data.email === userEmail);
if (!user) {
res.status(500).send('Usuario no encontrado');
if (users) {
// Busco el usuario con email = parámetro id del request
const userEmail = req.params.id;
const user = users.find((data) => data.email === userEmail);
if (!user) {
res.status(500).send('Usuario no encontrado.');
} else {
res.json(user);
}
} else {
res.json(user);
res.status(500).send('No se ha agregado ningún usuario aún.');
}
});
// [POST] CREAR UN NUEVO USUARIO: /user/new
// [POST] CREAR UN NUEVO USUARIO: /user
router.post('/user', (req, res) => {
// Si tengo un body en el request y tiene un email, agrego el usuario con una lista de comentarios vacía
if (req.body) {
console.log(req.body);
if (req.body.email) {
......@@ -37,21 +45,30 @@ router.post('/user', (req, res) => {
users.push(newUser);
res.json(users);
} else {
res.status(500).send('Se debe ingresar un email');
res.status(500).send('Se debe ingresar un email.');
}
} else {
res.status(500).send('Existió un error al procesar la solicitud');
res.status(500).send('Se produjo un error al procesar la solicitud.');
}
});
// [GET] OBTENER LOS COMENTARIOS DE UN USUARIO: /user/:id/comments
router.get('/user/:id/comments', (req, res) => {
const userId = Number(req.params.id);
const user = users.find((data) => data.id === userId);
if (!user) {
res.status(500).send('Usuario no encontrado')
// Obtengo los comentarios de un usuario con email = parámetro id del request
if (req.params) {
const userEmail = req.params.id;
if (userEmail) {
const user = users.find((data) => data.email === userEmail);
if (!user) {
res.status(500).send('Usuario no encontrado.');
} else {
res.json(user.comment);
}
} else {
res.status(500).send('Se debe ingresar un email.');
}
} else {
res.json(user.comment);
res.status(500).send('Se produjo un error al procesar la solicitud.');
}
});
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment