Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
noSQL
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Container Registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Mariana Molina Reyes
noSQL
Commits
685959d8
Commit
685959d8
authored
5 years ago
by
Mariana Molina
Browse files
Options
Downloads
Patches
Plain Diff
Termino la API localmente
parent
a2334768
No related branches found
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
src/models/Comment.js
+1
-29
1 addition, 29 deletions
src/models/Comment.js
src/models/User.js
+6
-4
6 additions, 4 deletions
src/models/User.js
src/routes/comment.js
+98
-9
98 additions, 9 deletions
src/routes/comment.js
src/routes/user.js
+37
-20
37 additions, 20 deletions
src/routes/user.js
with
142 additions
and
62 deletions
src/models/Comment.js
+
1
−
29
View file @
685959d8
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
'
}];
This diff is collapsed.
Click to expand it.
src/models/User.js
+
6
−
4
View file @
685959d8
let
User
=
{
name
:
''
,
email
:
''
};
\ No newline at end of file
exports
.
users
=
[
{
email
:
'
email1
'
,
comment
:
[]
}
];
\ No newline at end of file
This diff is collapsed.
Click to expand it.
src/routes/comment.js
+
98
−
9
View file @
685959d8
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
This diff is collapsed.
Click to expand it.
src/routes/user.js
+
37
−
20
View file @
685959d8
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.
'
);
}
});
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment