Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
Exersice-1-Cloud-Computing
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
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
Javier Díaz Ortiz
Exersice-1-Cloud-Computing
Commits
722e0603
Commit
722e0603
authored
2 months ago
by
javdiaort
Browse files
Options
Downloads
Patches
Plain Diff
falta base de datos que las tareas se actualicen
parent
1c563937
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
src/config.js
+6
-6
6 additions, 6 deletions
src/config.js
src/index.js
+34
-12
34 additions, 12 deletions
src/index.js
src/taskModel.js
+22
-0
22 additions, 0 deletions
src/taskModel.js
views/home.ejs
+5
-8
5 additions, 8 deletions
views/home.ejs
with
67 additions
and
26 deletions
src/config.js
+
6
−
6
View file @
722e0603
...
...
@@ -4,13 +4,13 @@ const connect = mongoose.connect("mongodb://localhost:27017/Login")
// check database connected or not
//Create a schema
const
loginSchema
=
new
mongoose
.
Schema
({
name
:{
type
:
String
,
required
:
true
const
loginSchema
=
new
mongoose
.
Schema
({
name
:
{
type
:
String
,
required
:
true
},
password
:{
type
:
String
,
password
:
{
type
:
String
,
required
:
true
}
});
...
...
This diff is collapsed.
Click to expand it.
src/index.js
+
34
−
12
View file @
722e0603
...
...
@@ -3,7 +3,7 @@ const pasth = require('path');
const
bcrypt
=
require
(
'
bcrypt
'
);
const
bcrypt2
=
require
(
'
bcryptjs
'
);
const
collection
=
require
(
"
./config
"
);
const
Task
=
require
(
'
./taskModel
'
);
// Asegúrate de requerir el modelo de Task
const
app
=
express
();
const
session
=
require
(
"
express-session
"
);
const
MongoStore
=
require
(
"
connect-mongo
"
);
...
...
@@ -104,24 +104,46 @@ app.post("/login", async (req, res) => {
// PARTE TODO LIST
app
.
get
(
"
/
todo
"
,
async
(
req
,
res
)
=>
{
if
(
!
req
.
session
.
userId
)
{
return
res
.
redirect
(
"
/
"
);
// Redirigir si no está loguead
o
}
app
.
get
(
"
/
home
"
,
async
(
req
,
res
)
=>
{
try
{
// Obtener todas las tareas del usuari
o
const
tasks
=
await
Task
.
find
({
userId
:
req
.
session
.
userId
});
const
user
=
await
collection
.
findById
(
req
.
session
.
userId
);
res
.
render
(
"
index
"
,
{
tasks
:
user
.
tasks
});
// Renderizar la vista "home.ejs" con las tareas del usuario
res
.
render
(
"
home
"
,
{
tasks
:
tasks
});
}
catch
(
error
)
{
console
.
error
(
"
Error al obtener tareas:
"
,
error
);
res
.
status
(
500
).
send
(
"
Error al obtener tareas
"
);
}
});
//agregar tareas
app
.
post
(
"
/add
"
,
async
(
req
,
res
)
=>
{
if
(
!
req
.
session
.
userId
)
return
res
.
redirect
(
"
/
"
);
try
{
// Obtén el usuario de la base de datos usando el userId almacenado en la sesión
const
user
=
await
collection
.
findOne
({
_id
:
req
.
session
.
userId
});
const
user
=
await
collection
.
findById
(
req
.
session
.
userId
);
user
.
tasks
.
push
({
text
:
req
.
body
.
task
}
);
await
user
.
save
();
if
(
!
user
)
{
return
res
.
status
(
404
).
send
(
"
Usuario no encontrado
"
);
}
res
.
redirect
(
"
/todo
"
);
// Crear una nueva tarea
const
newTask
=
new
Task
({
text
:
req
.
body
.
task
,
userId
:
req
.
session
.
userId
// Asocia la tarea al usuario
});
// Guardar la nueva tarea en la base de datos
await
newTask
.
save
();
// Redirigir al usuario a la página de inicio con las tareas actualizadas
res
.
redirect
(
"
/home
"
);
//CUIDADO
}
catch
(
error
)
{
console
.
error
(
"
Error al agregar tarea:
"
,
error
);
res
.
status
(
500
).
send
(
"
Error al agregar tarea
"
);
}
});
//eliminar tareas
...
...
This diff is collapsed.
Click to expand it.
src/taskModel.js
0 → 100644
+
22
−
0
View file @
722e0603
const
mongoose
=
require
(
"
mongoose
"
);
const
taskSchema
=
new
mongoose
.
Schema
({
text
:
{
type
:
String
,
required
:
true
},
createdAt
:
{
type
:
Date
,
default
:
Date
.
now
},
userId
:
{
type
:
mongoose
.
Schema
.
Types
.
ObjectId
,
// Referencia al usuario
required
:
true
,
ref
:
"
users
"
// Esto indica que "userId" es una referencia a la colección "users"
}
});
// Crear el modelo de la colección "tasks"
const
Task
=
mongoose
.
model
(
"
tasks
"
,
taskSchema
);
module
.
exports
=
Task
;
\ No newline at end of file
This diff is collapsed.
Click to expand it.
views/home.ejs
+
5
−
8
View file @
722e0603
...
...
@@ -3,29 +3,26 @@
<head>
<meta
charset=
"UTF-8"
>
<meta
name=
"viewport"
content=
"width=device-width, initial-scale=1.0"
>
<title>
Home Page
</title>
<title>
Tu Lista de Tareas
</title>
</head>
<body>
<h1>
Tu Lista de Tareas
</h1>
<
%
if
(
tasks
&&
tasks.length
>
0) { %>
<ul>
<
%
tasks.forEach(task =
>
{
%
>
<li><
%=
task.text
%
></li>
<li><
%=
task.text
%
>
-
<
%=
task.createdAt.toLocaleString
()
%
></li>
<
%
});
%
>
</ul>
<
%
}
else
{
%
>
<p>
No tienes tareas
aún
.
</p>
<p>
No tienes tareas.
</p>
<
%
}
%
>
<form
action=
"/add"
method=
"POST"
>
<input
type=
"text"
name=
"task"
placeholder=
"Nueva tarea"
required
>
<button
type=
"submit"
>
Agregar
</button>
<button
type=
"submit"
>
Agregar
tarea
</button>
</form>
<a
href=
"/logout"
>
Cerrar Sesión
</a>
<a
href=
"/logout"
>
Cerrar sesión
</a>
</body>
</html>
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