From 2cc71cdaad35a478f63e5e56ac74581541c3bb57 Mon Sep 17 00:00:00 2001 From: rodrox11 Date: Mon, 1 Apr 2024 19:49:42 +0200 Subject: [PATCH 001/102] No permitir usuario repetido --- users/userservice/user-service.js | 6 ++++++ webapp/src/components/Register/Register.js | 9 ++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/users/userservice/user-service.js b/users/userservice/user-service.js index 4a483b8c..925cc6d3 100644 --- a/users/userservice/user-service.js +++ b/users/userservice/user-service.js @@ -32,6 +32,12 @@ app.post('/adduser', async (req, res) => { // Check if required fields are present in the request body validateRequiredFields(req, ['username', 'password']); + // Check if the username already exists + const existingUser = await User.findOne({ username: req.body.username }); + if (existingUser) { + return res.status(400).json({ error: 'Username already exists. Please choose a different username.' }); + } + // Encrypt the password before saving it const hashedPassword = await bcrypt.hash(req.body.password, 10); diff --git a/webapp/src/components/Register/Register.js b/webapp/src/components/Register/Register.js index f5ee9695..847488e9 100644 --- a/webapp/src/components/Register/Register.js +++ b/webapp/src/components/Register/Register.js @@ -38,7 +38,14 @@ const AddUser = () => { axios .post(`${apiEndpoint}/adduser`, { username, password }) .then(() => setOpenSnackbar(true)) - .catch((error) => setError(error.message)); + .catch((error) => { + if (error.response && error.response.data && error.response.data.error) { + setError(error.response.data.error); + } else { + setError("Ha ocurrido un error. Por favor, inténtalo de nuevo."); + } + }); + }; return ( From 427bb24f4f18cb85c5e9ac612adbd754f50232d8 Mon Sep 17 00:00:00 2001 From: rodrox11 Date: Thu, 4 Apr 2024 12:58:00 +0200 Subject: [PATCH 002/102] Implementar las rutas --- webapp/src/App.js | 2 ++ webapp/src/components/Nav/Nav.js | 1 + 2 files changed, 3 insertions(+) diff --git a/webapp/src/App.js b/webapp/src/App.js index 06a34187..bd9d4379 100644 --- a/webapp/src/App.js +++ b/webapp/src/App.js @@ -13,6 +13,7 @@ import Config from "./pages/Config/Config.js"; import Login from "./components/Login/Login.js"; import Register from "./components/Register/Register.js"; import Perfil from "./pages/Perfil/Perfil.js"; +import UsersPage from "./pages/Social/UsersPage.js"; function App() { return ( @@ -30,6 +31,7 @@ function App() { } /> } /> } /> + } /> } /> } /> diff --git a/webapp/src/components/Nav/Nav.js b/webapp/src/components/Nav/Nav.js index 8c62a3a7..347e6132 100644 --- a/webapp/src/components/Nav/Nav.js +++ b/webapp/src/components/Nav/Nav.js @@ -45,6 +45,7 @@ const Nav = () => { + From f7b6003f98a722974c515b745e749c1361f34c9b Mon Sep 17 00:00:00 2001 From: rodrox11 Date: Thu, 4 Apr 2024 14:45:22 +0200 Subject: [PATCH 003/102] Consulta de usuarios --- users/userservice/user-service.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/users/userservice/user-service.js b/users/userservice/user-service.js index 925cc6d3..93d2f09c 100644 --- a/users/userservice/user-service.js +++ b/users/userservice/user-service.js @@ -52,6 +52,16 @@ app.post('/adduser', async (req, res) => { res.status(400).json({ error: error.message }); }}); +// Route to get all users +app.get('/users', async (req, res) => { + try { + const users = await User.find(); + res.json(users); + } catch (error) { + res.status(500).json({ error: 'Internal Server Error' }); + } +}); + app.get('/userInfo', async (req, res) => { try { const user = await User.findOne({username:req.query.user}); From 8d3e359ca5b85c6fade81be199074df249fbe8d3 Mon Sep 17 00:00:00 2001 From: rodrox11 Date: Thu, 4 Apr 2024 14:45:56 +0200 Subject: [PATCH 004/102] Lista de usuarios --- webapp/src/pages/Social/UsersPage.js | 100 +++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 webapp/src/pages/Social/UsersPage.js diff --git a/webapp/src/pages/Social/UsersPage.js b/webapp/src/pages/Social/UsersPage.js new file mode 100644 index 00000000..407810a9 --- /dev/null +++ b/webapp/src/pages/Social/UsersPage.js @@ -0,0 +1,100 @@ +import React, { useState, useEffect } from "react"; +import { Input, Button, Heading, Table, Thead, Tbody, Tr, Th, Td } from "@chakra-ui/react"; +import Nav from "../../components/Nav/Nav.js"; +import Footer from "../../components/Footer/Footer.js"; + +const UserList = ({ users, handleAddFriend }) => { + return ( +
+ Lista de Usuarios + + + + + + + + + {users.map(user => ( + + + + + ))} + +
Nombre de UsuarioAcciones
{user.username} + +
+
+ ); +}; + +const UsersPage = () => { + const [searchQuery, setSearchQuery] = useState(''); + const [users, setUsers] = useState([]); + const [isLoading, setIsLoading] = useState(true); + const [error, setError] = useState(null); + const [friends, setFriends] = useState([]); + + useEffect(() => { + fetchUsers(); + }, []); + + const fetchUsers = () => { + setIsLoading(true); + fetch('http://localhost:8001/users', { + method: 'GET', + headers: { + 'Content-Type': 'application/json' + }, + }) + .then((response) => response.json()) + .then((data) => { + setUsers(data); + setIsLoading(false); + }) + .catch((error) => { + console.error('Error fetching users:', error); + setError(error.message || 'Ha ocurrido un error al obtener los usuarios'); + setIsLoading(false); + }); + }; + + + const handleAddFriend = (user) => { + // Verifica si el usuario ya está en la lista de amigos + if (friends.find(friend => friend._id === user._id)) { + console.log(`El usuario ${user.username} ya está en tu lista de amigos.`); + return; + } + + // Agrega el usuario a la lista de amigos + setFriends(prevFriends => [...prevFriends, user]); + + // Aquí puedes implementar la lógica para enviar una solicitud al backend para agregar el usuario como amigo + console.log(`Agregando usuario ${user.username} como amigo...`); + }; + + return ( + <> +