From 5e6307148d69b8b8bc08a6d39d6983f033d76570 Mon Sep 17 00:00:00 2001 From: itsmePo Date: Thu, 5 Sep 2024 17:45:51 +0200 Subject: [PATCH 1/5] adding users archive #214 --- staff/borja-garcia/json-bbdd/database/users.json | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 staff/borja-garcia/json-bbdd/database/users.json diff --git a/staff/borja-garcia/json-bbdd/database/users.json b/staff/borja-garcia/json-bbdd/database/users.json new file mode 100644 index 00000000..e69de29b From b0a745f350502a87fed1154503095c6004264d51 Mon Sep 17 00:00:00 2001 From: itsmePo Date: Sat, 7 Sep 2024 20:29:48 +0200 Subject: [PATCH 2/5] Added all transcripted files and modified users.json. --- .../json-bbdd/database/users.json | 51 +++++++++++++++++++ .../json-bbdd/errors/credentials.js | 14 +++++ staff/borja-garcia/json-bbdd/errors/index.js | 7 +++ .../json-bbdd/errors/not-found.js | 15 ++++++ .../json-bbdd/interface/create-user.js | 39 ++++++++++++++ .../json-bbdd/interface/edit-user.js | 29 +++++++++++ staff/borja-garcia/json-bbdd/scripts/index.js | 3 ++ .../json-bbdd/scripts/users/create-one.js | 34 +++++++++++++ .../json-bbdd/scripts/users/delete-one.js | 29 +++++++++++ .../json-bbdd/scripts/users/index.js | 13 +++++ .../json-bbdd/scripts/users/read-all.js | 17 +++++++ .../json-bbdd/scripts/users/read-one.js | 23 +++++++++ .../json-bbdd/scripts/users/update-by-id.js | 28 ++++++++++ 13 files changed, 302 insertions(+) create mode 100644 staff/borja-garcia/json-bbdd/errors/credentials.js create mode 100644 staff/borja-garcia/json-bbdd/errors/index.js create mode 100644 staff/borja-garcia/json-bbdd/errors/not-found.js create mode 100644 staff/borja-garcia/json-bbdd/interface/create-user.js create mode 100644 staff/borja-garcia/json-bbdd/interface/edit-user.js create mode 100644 staff/borja-garcia/json-bbdd/scripts/index.js create mode 100644 staff/borja-garcia/json-bbdd/scripts/users/create-one.js create mode 100644 staff/borja-garcia/json-bbdd/scripts/users/delete-one.js create mode 100644 staff/borja-garcia/json-bbdd/scripts/users/index.js create mode 100644 staff/borja-garcia/json-bbdd/scripts/users/read-all.js create mode 100644 staff/borja-garcia/json-bbdd/scripts/users/read-one.js create mode 100644 staff/borja-garcia/json-bbdd/scripts/users/update-by-id.js diff --git a/staff/borja-garcia/json-bbdd/database/users.json b/staff/borja-garcia/json-bbdd/database/users.json index e69de29b..1a489d72 100644 --- a/staff/borja-garcia/json-bbdd/database/users.json +++ b/staff/borja-garcia/json-bbdd/database/users.json @@ -0,0 +1,51 @@ +{ + "users": [ + { + "id": 0, + "name": "Limón", + "birth_date": "1985-01-04", + "phone": "+34 111111111", + "email": "limoncode@gmail.com", + "password": "12345678" + }, + { + "id": 1, + "name": "Borja", + "birth_date": "1995-08-30", + "phone": "+34 987654321", + "email": "itspuo@gmail.com", + "password": "12345678" + }, + { + "id": 2, + "name": "Pepito", + "birth_date": "1995-08-30", + "phone": "+34 987654321", + "email": "foo@gmail.com", + "password": "12345678" + }, + { + "id": 3, + "name": "Rafa", + "birth_date": "1983-02-07", + "phone": "+34 123456789", + "email": "nosoyrafa@gmail.com", + "password": "12345678" + }, + { + "id": 4, + "name": "Rafa", + "birth_date": "1983-02-07", + "phone": "+34 123456789", + "email": "nosoyrafa2@gmail.com", + "password": "12345678" + }, + { "id": 5, + "name": "Ventu", + "birth_date": "2000-01-02", + "phone": "+32 123456789", + "email": "ventu@gmail.com", + "password": "123456789" + } + ] + } \ No newline at end of file diff --git a/staff/borja-garcia/json-bbdd/errors/credentials.js b/staff/borja-garcia/json-bbdd/errors/credentials.js new file mode 100644 index 00000000..dfc1b942 --- /dev/null +++ b/staff/borja-garcia/json-bbdd/errors/credentials.js @@ -0,0 +1,14 @@ +function CredentialsError(message) { + const instance = new Error(message); + Object.setPrototypeOf(instance, Object.getPrototypeOf(this)); + if (Error.captureStackTrace) { + Error.captureStackTrace(instance, CredentialsError); + } + return instance +} + +CredentialsError.prototype = Object.create(Error.prototype); + +CredentialsError.prototype.name = "CredentialsError"; + +module.exports = CredentialsError; \ No newline at end of file diff --git a/staff/borja-garcia/json-bbdd/errors/index.js b/staff/borja-garcia/json-bbdd/errors/index.js new file mode 100644 index 00000000..50e87d3c --- /dev/null +++ b/staff/borja-garcia/json-bbdd/errors/index.js @@ -0,0 +1,7 @@ +const CredentialsError = require("./credentials.js"); +const NotFoundError = require("./not-found.js"); + +module.exports = { + CredentialsError, + NotFoundError, +}; \ No newline at end of file diff --git a/staff/borja-garcia/json-bbdd/errors/not-found.js b/staff/borja-garcia/json-bbdd/errors/not-found.js new file mode 100644 index 00000000..7f11ce9e --- /dev/null +++ b/staff/borja-garcia/json-bbdd/errors/not-found.js @@ -0,0 +1,15 @@ +function NotFoundError(message) { + const instance = new Error(message); + Object.setPrototypeOf(instance, Object.getPrototypeOf(this)); + + if (Error.captureStackTrace) { + Error.captureStackTrace(instance, NotFoundError); + } + return instance; +} + +NotFoundError.prototype = Object.create(Error.prototype); + +NotFoundError.prototype.name = "NotFoundError"; + +module.exports = NotFoundError; \ No newline at end of file diff --git a/staff/borja-garcia/json-bbdd/interface/create-user.js b/staff/borja-garcia/json-bbdd/interface/create-user.js new file mode 100644 index 00000000..6c0aa3e0 --- /dev/null +++ b/staff/borja-garcia/json-bbdd/interface/create-user.js @@ -0,0 +1,39 @@ +const readline = require("readline"); +const {users} = require("../scripts"); + +const r1 = readline.createInterface(process.stdin,process.stdout); + +const template = [ + ["¿Cuál es tu nombre?", 'name'], + ['¿Cuál es tu fecha de nacimiento?', 'birthDate'], + ['¿Cuál es tu teléfono?', 'phone'], + ['¿Cuál es tu email?', 'email'], + ['¿Cuál es tu contraseña?', 'password'], +]; +let tupple = [...template]; + +const user = {}; + +function prompt(tupple) { + rawListeners.question(tupple[0][0], function (answer) { + user[tupple[0][1]] = answer; + tupple.shift(); + if (tupple.length > 0) prompt(tupple); + else { + users.createOne(user, (id) => { + console.table ({id}); + rawListeners.question( + '¿Quieres agregar un nuevo usuario? (yes/no)', + function(answer) { + if (answer === 'yes'){ + tupple = [...template]; + prompt(tupple); + } else rawListeners.close(); + } + ); + }); + } + }); +} + +prompt(tupple); \ No newline at end of file diff --git a/staff/borja-garcia/json-bbdd/interface/edit-user.js b/staff/borja-garcia/json-bbdd/interface/edit-user.js new file mode 100644 index 00000000..85a0a942 --- /dev/null +++ b/staff/borja-garcia/json-bbdd/interface/edit-user.js @@ -0,0 +1,29 @@ +const readline = require('readline'); +const {users} = require('../scripts'); + +const rl = readline.createInterface(process.stdin, process.stdout); + +const template =[ + ["Cuál es tu nuevo nombre?", "name"], + ["Cuál es tu nueva fecha de nacimiento?", "birthDate"], + ["Cuál es tu nuevo teléfono?", "phone"], + ]; + let tupple = [...template]; + + const user ={}; + + function prompt(tupple, id) { + rl.question(tupple[0][0], function (answer){ + user[tupple[0][1]] = answer === '' ? null : answer; + tupple.shift(); + if (tupple.length > 0) prompt(tupple, id); + else { + users.updateById(id, user); + rl.close(); + } + }); + } + + rl.question('¿Cuál es el ID del usuario que quieres editar=', (id) => { + prompt(tupple, Number(id)); + }); \ No newline at end of file diff --git a/staff/borja-garcia/json-bbdd/scripts/index.js b/staff/borja-garcia/json-bbdd/scripts/index.js new file mode 100644 index 00000000..188f5d9b --- /dev/null +++ b/staff/borja-garcia/json-bbdd/scripts/index.js @@ -0,0 +1,3 @@ +const users = require('./users'); + +module.exports = {users}; \ No newline at end of file diff --git a/staff/borja-garcia/json-bbdd/scripts/users/create-one.js b/staff/borja-garcia/json-bbdd/scripts/users/create-one.js new file mode 100644 index 00000000..737553f1 --- /dev/null +++ b/staff/borja-garcia/json-bbdd/scripts/users/create-one.js @@ -0,0 +1,34 @@ +const fs = require('fs'); +const path = require('path'); +const readAll = require('./read-all.js'); + +function createOne(data) { + const { name, birthDate, phone, email, password } = data; + readAll((users) => { + const isEmailDuplicated = users.some((user) => user.email === email); + if (isEmailDuplicated) throw new Error("User already exists"); + + const id = users[users.length - 1].id +1; + users.push({ + id, + name, + birth_date: birthDate, + phone, + email, + password, + }); + + fs.writeFile( + path.join(__dirname, "../../database/users.json"), + JSON.stringify({ users: users }), + "utf-8", + (err) => { + if (err) throw err; + } + ); + }); +} + +module.exports = createOne; + +createOne({}); \ No newline at end of file diff --git a/staff/borja-garcia/json-bbdd/scripts/users/delete-one.js b/staff/borja-garcia/json-bbdd/scripts/users/delete-one.js new file mode 100644 index 00000000..a416204f --- /dev/null +++ b/staff/borja-garcia/json-bbdd/scripts/users/delete-one.js @@ -0,0 +1,29 @@ +const fs = require('fs?'); +const path = require('path'); +const read = require('./read-all.js'); +const {NotFoundError, CredentialsError} = require('../..(errors'); + +function deleteOne(id, password) { + read((users) => { + const user = users.filter((_user) => _user.id === id)[0]; + + if (!user) throw new NotFoundError("User Not Found"); + + if (!(user.password === password)) throw + new CredentialsError("Password Doesn't Match"); + + const newUsers = users.filter((_user) => !(_user.id === id )); + + fs.writeFile( + path.join(__dirname, '../../database/users.json'), + JSON.stringify({ users: newUsers}), + "utf-8", + (err) => { + if (err) throw err; + } + ); + } +); +} + +module.exports = deleteOne; \ No newline at end of file diff --git a/staff/borja-garcia/json-bbdd/scripts/users/index.js b/staff/borja-garcia/json-bbdd/scripts/users/index.js new file mode 100644 index 00000000..4be3d649 --- /dev/null +++ b/staff/borja-garcia/json-bbdd/scripts/users/index.js @@ -0,0 +1,13 @@ +const createOne = require("./create-one.js"); +const deleteOne = require("./delete-one.js"); +const readAll = require("./read-all.js"); +const readOne = require("./read-one.js"); +const updateById = require("./update-by-id.js"); + +module.exports = { + createOne, + deleteOne, + readAll, + readOne, + updateById, +}; \ No newline at end of file diff --git a/staff/borja-garcia/json-bbdd/scripts/users/read-all.js b/staff/borja-garcia/json-bbdd/scripts/users/read-all.js new file mode 100644 index 00000000..5b173946 --- /dev/null +++ b/staff/borja-garcia/json-bbdd/scripts/users/read-all.js @@ -0,0 +1,17 @@ +const fs = require('fs'); + const path = require('path'); + + function readAll(callback) { + fs.readFile( + path.join(__dirname, '../../database/users.json'), + 'utf-8', + (err, data) => { + if (err) throw err; + + const data = JSON.parse(_data); + callback(data.users); + } + ); + } + + module.exports = readAll; \ No newline at end of file diff --git a/staff/borja-garcia/json-bbdd/scripts/users/read-one.js b/staff/borja-garcia/json-bbdd/scripts/users/read-one.js new file mode 100644 index 00000000..ccf85433 --- /dev/null +++ b/staff/borja-garcia/json-bbdd/scripts/users/read-one.js @@ -0,0 +1,23 @@ +const fs = require("fs"); +const path = require("path"); +const { NotFoundError } = require("../../errors"); + +function readOne(id, callback) { + fs.readFile( + path.join(__dirname, "../../database/users.json"), + "utf-8", + (err, _data) => { + if (err) throw err; + + const data = JSON.parse(_data); + + const user = data.users.filter((_user) => _user.id === id)[0]; + + if (!user) throw new NotFoundError("User not found"); + + callback(user); + } + ); +} + +module.exports = readOne; \ No newline at end of file diff --git a/staff/borja-garcia/json-bbdd/scripts/users/update-by-id.js b/staff/borja-garcia/json-bbdd/scripts/users/update-by-id.js new file mode 100644 index 00000000..05c76ebf --- /dev/null +++ b/staff/borja-garcia/json-bbdd/scripts/users/update-by-id.js @@ -0,0 +1,28 @@ +const fs = require("fs"); +const path = require("path"); +const read = require("./read-all.js"); + +function updateById(id, data) { + if (typeof id !== "number") throw new TypeError("id is not a number"); + const { name, birthDate, phone } = data; + read((users) => { + users.forEach((user) => { + if (user.id === id) { + user.name = name ?? user.name; + user.birth_date = birthDate ?? user.birth_date; + user.phone = phone ?? user.phone; + } + }); + + fs.writeFile( + path.join(__dirname, "../../database/users.json"), + JSON.stringify({ users: users }), + "utf-8", + (err) => { + if (err) throw err; + } + ); + }); +} + +module.exports = updateById; \ No newline at end of file From c2f4cf362fa926b6fbdea5391c36584d7c3769ee Mon Sep 17 00:00:00 2001 From: itsmePo Date: Sun, 22 Sep 2024 18:39:12 +0200 Subject: [PATCH 3/5] I think im going to start a new life as a panda bears dance instructor #214 --- staff/borja-garcia/json-bbdd/.gitignore | 4 + .../json-bbdd/database/users.json | 52 +------- .../json-bbdd/errors/email-not-valid.js | 14 +++ staff/borja-garcia/json-bbdd/errors/index.js | 9 +- .../json-bbdd/errors/not-an-integer.js | 14 +++ .../json-bbdd/interface/create-user.js | 56 +++++---- .../json-bbdd/interface/delete-user.js | 44 +++++++ .../json-bbdd/interface/list-users.js | 21 ++++ .../json-bbdd/interface/show-user.js | 28 +++++ staff/borja-garcia/json-bbdd/package.json | 25 ++++ .../json-bbdd/scripts/users/create-one.js | 62 ++++----- .../json-bbdd/scripts/users/delete-one.js | 26 ++-- .../json-bbdd/scripts/users/index.js | 10 +- .../json-bbdd/scripts/users/read-all.js | 28 ++--- .../json-bbdd/scripts/users/read-one.js | 22 ++-- .../json-bbdd/scripts/users/update-by-id.js | 34 +++-- staff/borja-garcia/json-bbdd/test/index.js | 5 + .../json-bbdd/test/users/create-one.test.js | 119 ++++++++++++++++++ .../json-bbdd/test/users/delete-one.test.js | 81 ++++++++++++ .../json-bbdd/test/users/index.js | 13 ++ .../json-bbdd/test/users/read-all.test.js | 43 +++++++ .../json-bbdd/test/users/read-one.test.js | 68 ++++++++++ .../json-bbdd/test/users/update-by-id.test.js | 68 ++++++++++ 23 files changed, 676 insertions(+), 170 deletions(-) create mode 100644 staff/borja-garcia/json-bbdd/.gitignore create mode 100644 staff/borja-garcia/json-bbdd/errors/email-not-valid.js create mode 100644 staff/borja-garcia/json-bbdd/errors/not-an-integer.js create mode 100644 staff/borja-garcia/json-bbdd/interface/delete-user.js create mode 100644 staff/borja-garcia/json-bbdd/interface/list-users.js create mode 100644 staff/borja-garcia/json-bbdd/interface/show-user.js create mode 100644 staff/borja-garcia/json-bbdd/package.json create mode 100644 staff/borja-garcia/json-bbdd/test/index.js create mode 100644 staff/borja-garcia/json-bbdd/test/users/create-one.test.js create mode 100644 staff/borja-garcia/json-bbdd/test/users/delete-one.test.js create mode 100644 staff/borja-garcia/json-bbdd/test/users/index.js create mode 100644 staff/borja-garcia/json-bbdd/test/users/read-all.test.js create mode 100644 staff/borja-garcia/json-bbdd/test/users/read-one.test.js create mode 100644 staff/borja-garcia/json-bbdd/test/users/update-by-id.test.js diff --git a/staff/borja-garcia/json-bbdd/.gitignore b/staff/borja-garcia/json-bbdd/.gitignore new file mode 100644 index 00000000..ee976ba1 --- /dev/null +++ b/staff/borja-garcia/json-bbdd/.gitignore @@ -0,0 +1,4 @@ +/node_modules/ +pnpm-lock.yaml +../social-app/ + ../.prettierrc \ No newline at end of file diff --git a/staff/borja-garcia/json-bbdd/database/users.json b/staff/borja-garcia/json-bbdd/database/users.json index 1a489d72..4b527997 100644 --- a/staff/borja-garcia/json-bbdd/database/users.json +++ b/staff/borja-garcia/json-bbdd/database/users.json @@ -1,51 +1 @@ -{ - "users": [ - { - "id": 0, - "name": "Limón", - "birth_date": "1985-01-04", - "phone": "+34 111111111", - "email": "limoncode@gmail.com", - "password": "12345678" - }, - { - "id": 1, - "name": "Borja", - "birth_date": "1995-08-30", - "phone": "+34 987654321", - "email": "itspuo@gmail.com", - "password": "12345678" - }, - { - "id": 2, - "name": "Pepito", - "birth_date": "1995-08-30", - "phone": "+34 987654321", - "email": "foo@gmail.com", - "password": "12345678" - }, - { - "id": 3, - "name": "Rafa", - "birth_date": "1983-02-07", - "phone": "+34 123456789", - "email": "nosoyrafa@gmail.com", - "password": "12345678" - }, - { - "id": 4, - "name": "Rafa", - "birth_date": "1983-02-07", - "phone": "+34 123456789", - "email": "nosoyrafa2@gmail.com", - "password": "12345678" - }, - { "id": 5, - "name": "Ventu", - "birth_date": "2000-01-02", - "phone": "+32 123456789", - "email": "ventu@gmail.com", - "password": "123456789" - } - ] - } \ No newline at end of file +{"users":null} \ No newline at end of file diff --git a/staff/borja-garcia/json-bbdd/errors/email-not-valid.js b/staff/borja-garcia/json-bbdd/errors/email-not-valid.js new file mode 100644 index 00000000..1d8bc20d --- /dev/null +++ b/staff/borja-garcia/json-bbdd/errors/email-not-valid.js @@ -0,0 +1,14 @@ +function EmailNotValidError(message) { + const instance = new Error(message); + Object.setPrototypeOf(instance, Object.getPrototypeOf(this)); + if (Error.captureStackTrace) { + Error.captureStackTrace(instance, EmailNotValidError); + } + return instance; +} + +EmailNotValidError.prototype = Object.create(Error.prototype); + +EmailNotValidError.prototype.name = "EmailNotValidError"; + +module.exports = EmailNotValidError; \ No newline at end of file diff --git a/staff/borja-garcia/json-bbdd/errors/index.js b/staff/borja-garcia/json-bbdd/errors/index.js index 50e87d3c..74a1bf5c 100644 --- a/staff/borja-garcia/json-bbdd/errors/index.js +++ b/staff/borja-garcia/json-bbdd/errors/index.js @@ -1,7 +1,10 @@ const CredentialsError = require("./credentials.js"); +const EmailNotValidError = require("./email-not-valid.js"); const NotFoundError = require("./not-found.js"); - +const NotAnIntegerError = require("./not-an-integer.js"); module.exports = { - CredentialsError, - NotFoundError, + CredentialsError, + EmailNotValidError, + NotAnIntegerError, + NotFoundError, }; \ No newline at end of file diff --git a/staff/borja-garcia/json-bbdd/errors/not-an-integer.js b/staff/borja-garcia/json-bbdd/errors/not-an-integer.js new file mode 100644 index 00000000..84354021 --- /dev/null +++ b/staff/borja-garcia/json-bbdd/errors/not-an-integer.js @@ -0,0 +1,14 @@ +function NotAnIntegerError(message) { + const instance = new Error(message); + Object.setPrototypeOf(instance, Object.getPrototypeOf(this)); + if (Error.captureStackTrace) { + Error.captureStackTrace(instance, NotAnIntegerError); + } + return instance; +} + +NotAnIntegerError.prototype = Object.create(Error.prototype); + +NotAnIntegerError.prototype.name = "NotAnIntegerError"; + +module.exports = NotAnIntegerError; \ No newline at end of file diff --git a/staff/borja-garcia/json-bbdd/interface/create-user.js b/staff/borja-garcia/json-bbdd/interface/create-user.js index 6c0aa3e0..26428703 100644 --- a/staff/borja-garcia/json-bbdd/interface/create-user.js +++ b/staff/borja-garcia/json-bbdd/interface/create-user.js @@ -1,39 +1,43 @@ const readline = require("readline"); -const {users} = require("../scripts"); +const { users } = require("../scripts"); -const r1 = readline.createInterface(process.stdin,process.stdout); +const r1 = readline.createInterface(process.stdin, process.stdout); const template = [ - ["¿Cuál es tu nombre?", 'name'], - ['¿Cuál es tu fecha de nacimiento?', 'birthDate'], - ['¿Cuál es tu teléfono?', 'phone'], - ['¿Cuál es tu email?', 'email'], - ['¿Cuál es tu contraseña?', 'password'], + ["¿Cuál es tu nombre?", "name"], + ["¿Cuál es tu fecha de nacimiento?", "birthDate"], + ["¿Cuál es tu teléfono?", "phone"], + ["¿Cuál es tu email?", "email"], + ["¿Cuál es tu contraseña?", "password"], ]; let tupple = [...template]; const user = {}; function prompt(tupple) { - rawListeners.question(tupple[0][0], function (answer) { - user[tupple[0][1]] = answer; - tupple.shift(); - if (tupple.length > 0) prompt(tupple); - else { - users.createOne(user, (id) => { - console.table ({id}); - rawListeners.question( - '¿Quieres agregar un nuevo usuario? (yes/no)', - function(answer) { - if (answer === 'yes'){ - tupple = [...template]; - prompt(tupple); - } else rawListeners.close(); - } - ); - }); + r1.question(tupple[0][0], function (answer) { + user[tupple[0][1]] = answer; + tupple.shift(); + if (tupple.length > 0) { + prompt(tupple); + } else { + users.createOne(user, (err, id) => { + if (err) { + console.error("Error al crear el usuario:", err.message); + } else { + console.table({ id }); + r1.question("¿Quieres agregar un nuevo usuario? (yes/no)", function (answer) { + if (answer === "yes") { + tupple = [...template]; + prompt(tupple); + } else { + r1.close(); + } + }); } - }); + }); + } + }); } -prompt(tupple); \ No newline at end of file +prompt(tupple); diff --git a/staff/borja-garcia/json-bbdd/interface/delete-user.js b/staff/borja-garcia/json-bbdd/interface/delete-user.js new file mode 100644 index 00000000..818fc21c --- /dev/null +++ b/staff/borja-garcia/json-bbdd/interface/delete-user.js @@ -0,0 +1,44 @@ +const readline = require("readline"); +const { users } = require("../scripts"); + +const rl = readline.createInterface(process.stdin, process.stdout); + +rl.question( + "Por favor, introduce el id del usuario que quieres eliminar", + function (_id) { + const id = Number(_id); + + if ( + typeof id !== "number" || + id < 0 || + id === NaN || + id === Infinity || + !Number.isInteger(id) + ) { + console.log("Introduce un número válido"); + return rl.close(); + } + rl.question( + "Introduce la contraseña del usuario a eliminar", + function (password) { + rl.question( + "Estás seguro de eliminar el usuario? (yes/no)", + function (answer) { + if (!answer === "yes") return rl.close(); + + users.deleteOne(id, password, (err, response) => { + if (err) throw err; + if (response) + console.log("El usuario se ha eliminado correctamente"); + else + console.log( + "No se ha podido eliminar correctamente el usuario" + ); + rl.close(); + }); + } + ); + } + ); + } +); \ No newline at end of file diff --git a/staff/borja-garcia/json-bbdd/interface/list-users.js b/staff/borja-garcia/json-bbdd/interface/list-users.js new file mode 100644 index 00000000..3e95a6bf --- /dev/null +++ b/staff/borja-garcia/json-bbdd/interface/list-users.js @@ -0,0 +1,21 @@ +const readline = require("readline"); +const { users } = require("../scripts"); + +const rl = readline.createInterface(process.stdin, process.stdout); + +rl.question("Cuál es el rango que deseas ver? (ej: 5-8)", function (answer) { + let min = Number(answer.split("-")[0]), + max = Number(answer.split("-")[1]); + + if (min > max) { + console.log("Él mínimo no puede ser mayor al máximo"); + return rl.close(); + } + + users.readAll((err, listUsers) => { + if (err) throw err; + const showList = listUsers.slice(min, max + 1); + console.table(showList); + rl.close(); + }); +}); \ No newline at end of file diff --git a/staff/borja-garcia/json-bbdd/interface/show-user.js b/staff/borja-garcia/json-bbdd/interface/show-user.js new file mode 100644 index 00000000..2689ebab --- /dev/null +++ b/staff/borja-garcia/json-bbdd/interface/show-user.js @@ -0,0 +1,28 @@ +const readline = require("readline"); +const { users } = require("../scripts"); + +const rl = readline.createInterface(process.stdin, process.stdout); + +rl.question( + "Por favor, introduce el id del usuario que quieres ver", + function (_id) { + const id = Number(_id); + + if ( + typeof id !== "number" || + id < 0 || + id === NaN || + id === Infinity || + !Number.isInteger(id) + ) { + console.log("Introduce un número válido"); + return rl.close(); + } + + users.readOne(id, (err, user) => { + if (err) throw err; + console.table(user); + rl.close(); + }); + } +); \ No newline at end of file diff --git a/staff/borja-garcia/json-bbdd/package.json b/staff/borja-garcia/json-bbdd/package.json new file mode 100644 index 00000000..5d823f9f --- /dev/null +++ b/staff/borja-garcia/json-bbdd/package.json @@ -0,0 +1,25 @@ +{ + "name": "json-bbdd", + "version": "0.0.0", + "description": "", + "main": "", + "scripts": { + "create-user": "node ./interface/create-user.js", + "delete-user": "node ./interface/delete-user.js", + "edit-user": "node ./interface/edit-user.js", + "list-users": "node ./interface/list-users.js", + "show-user": "node ./interface/show-user.js", + "test": "mocha ./test/users", + "test-inspect": "mocha --inspect-brk ./test/users" + }, + "keywords": [], + "author": "Borja García", + "license": "ISC", + + + "devDependencies": { + "mocha": "10.7.3", + "chai": "4.5.0" + }, + "dependencies": {} +} \ No newline at end of file diff --git a/staff/borja-garcia/json-bbdd/scripts/users/create-one.js b/staff/borja-garcia/json-bbdd/scripts/users/create-one.js index 737553f1..db0dd3ce 100644 --- a/staff/borja-garcia/json-bbdd/scripts/users/create-one.js +++ b/staff/borja-garcia/json-bbdd/scripts/users/create-one.js @@ -1,34 +1,40 @@ -const fs = require('fs'); -const path = require('path'); -const readAll = require('./read-all.js'); +const fs = require("fs"); +const path = require("path"); +const readAll = require("./read-all.js"); +const { EmailNotValidError } = require("../../errors/index.js"); -function createOne(data) { +function createOne(data, callback) { const { name, birthDate, phone, email, password } = data; - readAll((users) => { - const isEmailDuplicated = users.some((user) => user.email === email); - if (isEmailDuplicated) throw new Error("User already exists"); + const emailRegexp = new RegExp ( + /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|.(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ + ); + if (!emailRegexp.test(email)) + throw new EmailNotValidError("Email is not valid"); + readAll((users) => { + const isEmailDuplicated = users.some((user) => user.email === email); + if (isEmailDuplicated) throw new Error("User already exists"); - const id = users[users.length - 1].id +1; - users.push({ - id, - name, - birth_date: birthDate, - phone, - email, - password, - }); + const id = users[users.length -1].id + 1 ; + users.push({ + id, + name, + birth_date: birthDate, + phone, + email, + password, + }); - fs.writeFile( - path.join(__dirname, "../../database/users.json"), - JSON.stringify({ users: users }), - "utf-8", - (err) => { - if (err) throw err; - } - ); - }); -} + fs.writeFile( + path.join(__dirname, "../../database/users.json"), + JSON.stringify({ users: users}), + "utf-8", + (err) => { + if (err) throw err; -module.exports = createOne; + callback(id); + } + ); + }); +} -createOne({}); \ No newline at end of file +module.exports = createOne; \ No newline at end of file diff --git a/staff/borja-garcia/json-bbdd/scripts/users/delete-one.js b/staff/borja-garcia/json-bbdd/scripts/users/delete-one.js index a416204f..f806b8d0 100644 --- a/staff/borja-garcia/json-bbdd/scripts/users/delete-one.js +++ b/staff/borja-garcia/json-bbdd/scripts/users/delete-one.js @@ -1,29 +1,29 @@ -const fs = require('fs?'); -const path = require('path'); -const read = require('./read-all.js'); -const {NotFoundError, CredentialsError} = require('../..(errors'); +const fs = require("fs"); +const path = require("path"); +const read = require("./read-all.js"); +const { NotFoundError, CredentialsError } = require("../../errors"); -function deleteOne(id, password) { +function deleteOne(id, password, callback) { read((users) => { const user = users.filter((_user) => _user.id === id)[0]; - - if (!user) throw new NotFoundError("User Not Found"); + if (!user) throw new NotFoundError("User not found"); - if (!(user.password === password)) throw - new CredentialsError("Password Doesn't Match"); + if (! (user.password === password)) + throw new CredentialsError("Password doesn't match"); - const newUsers = users.filter((_user) => !(_user.id === id )); + const newUsers = users.filter((_user) => !(_user.id === id)); fs.writeFile( - path.join(__dirname, '../../database/users.json'), + path.join(__dirname, "../..database/users.json"), JSON.stringify({ users: newUsers}), "utf-8", (err) => { if (err) throw err; + + callback(true); } ); - } -); + }); } module.exports = deleteOne; \ No newline at end of file diff --git a/staff/borja-garcia/json-bbdd/scripts/users/index.js b/staff/borja-garcia/json-bbdd/scripts/users/index.js index 4be3d649..fb488004 100644 --- a/staff/borja-garcia/json-bbdd/scripts/users/index.js +++ b/staff/borja-garcia/json-bbdd/scripts/users/index.js @@ -5,9 +5,9 @@ const readOne = require("./read-one.js"); const updateById = require("./update-by-id.js"); module.exports = { - createOne, - deleteOne, - readAll, - readOne, - updateById, + createOne, + deleteOne, + readAll, + readOne, + updateById, }; \ No newline at end of file diff --git a/staff/borja-garcia/json-bbdd/scripts/users/read-all.js b/staff/borja-garcia/json-bbdd/scripts/users/read-all.js index 5b173946..10de567c 100644 --- a/staff/borja-garcia/json-bbdd/scripts/users/read-all.js +++ b/staff/borja-garcia/json-bbdd/scripts/users/read-all.js @@ -1,17 +1,17 @@ -const fs = require('fs'); - const path = require('path'); +const fs = require("fs"); +const path = require("path"); - function readAll(callback) { - fs.readFile( - path.join(__dirname, '../../database/users.json'), - 'utf-8', - (err, data) => { - if (err) throw err; +function readAll(callback) { + fs.readFile( + path.join(__dirname, "/../database/users.json"), + "utf-8", + (err, _data) => { + if (err) throw err; - const data = JSON.parse(_data); - callback(data.users); - } - ); - } + const data = JSON.parse(_data); + callback(data.users); + } + ); +} - module.exports = readAll; \ No newline at end of file +module.exports = readAll; \ No newline at end of file diff --git a/staff/borja-garcia/json-bbdd/scripts/users/read-one.js b/staff/borja-garcia/json-bbdd/scripts/users/read-one.js index ccf85433..ff6b42fb 100644 --- a/staff/borja-garcia/json-bbdd/scripts/users/read-one.js +++ b/staff/borja-garcia/json-bbdd/scripts/users/read-one.js @@ -3,21 +3,21 @@ const path = require("path"); const { NotFoundError } = require("../../errors"); function readOne(id, callback) { - fs.readFile( - path.join(__dirname, "../../database/users.json"), - "utf-8", - (err, _data) => { - if (err) throw err; + fs.readFile( + path.join(__dirname, "../database/users.json"), + "utf-8", + (err, _data) => { + if (err) throw err; - const data = JSON.parse(_data); + const data = JSON.parse(_data); - const user = data.users.filter((_user) => _user.id === id)[0]; + const user = data.users.filter((_user) => _user.id === id)[0]; - if (!user) throw new NotFoundError("User not found"); + if (!user) throw new NotFoundError("User not found"); - callback(user); - } - ); + callback(user); + } + ); } module.exports = readOne; \ No newline at end of file diff --git a/staff/borja-garcia/json-bbdd/scripts/users/update-by-id.js b/staff/borja-garcia/json-bbdd/scripts/users/update-by-id.js index 05c76ebf..3b2af628 100644 --- a/staff/borja-garcia/json-bbdd/scripts/users/update-by-id.js +++ b/staff/borja-garcia/json-bbdd/scripts/users/update-by-id.js @@ -1,28 +1,24 @@ const fs = require("fs"); const path = require("path"); const read = require("./read-all.js"); +const { NotAnIntegerError } = require("../../errors/index.js"); function updateById(id, data) { - if (typeof id !== "number") throw new TypeError("id is not a number"); - const { name, birthDate, phone } = data; - read((users) => { - users.forEach((user) => { - if (user.id === id) { - user.name = name ?? user.name; - user.birth_date = birthDate ?? user.birth_date; - user.phone = phone ?? user.phone; - } + if (typeof id !== "number") throw new TypeError("id is not a number"); + if (id < 0 || id === NaN || id === Infinity) + throw new RangeError("id is out of range"); + if (!Number.isInteger(id)) + throw new NotAnIntegerError("id is not an integer"); + const { name, birthDate, phone } = data; + read((users) => { + users.forEach((user) => { + if (user.id === id) { + user.name = name ?? user.name; + user.birth_date = birthDate ?? user.birth_date; + user.phone = phone ?? user.phone; + } }); - - fs.writeFile( - path.join(__dirname, "../../database/users.json"), - JSON.stringify({ users: users }), - "utf-8", - (err) => { - if (err) throw err; - } - ); - }); +}); } module.exports = updateById; \ No newline at end of file diff --git a/staff/borja-garcia/json-bbdd/test/index.js b/staff/borja-garcia/json-bbdd/test/index.js new file mode 100644 index 00000000..a24bfa5f --- /dev/null +++ b/staff/borja-garcia/json-bbdd/test/index.js @@ -0,0 +1,5 @@ +require("./create-one.test.js"); +require("./delete-one.test.js"); +require("./read-all.test.js"); +require("./read-one.test.js"); +require("./update-by-id.test.js"); \ No newline at end of file diff --git a/staff/borja-garcia/json-bbdd/test/users/create-one.test.js b/staff/borja-garcia/json-bbdd/test/users/create-one.test.js new file mode 100644 index 00000000..49f78924 --- /dev/null +++ b/staff/borja-garcia/json-bbdd/test/users/create-one.test.js @@ -0,0 +1,119 @@ +const { users } = require("../../scripts"); +const fs = require("fs"); +const path = require("path"); +const { assert } = require("chai"); +const { EmailNotValidError } = require("../../errors"); + +describe("Users scripts", function () { + describe("#createOne", function () { + let usersBackup = null; + + before((done) => { + fs.readFile( + path.join(__dirname, "../../database/users.json"), + "utf-8", + (err, _data) => { + if (err) throw err; + const data = JSON.parse(_data); + usersBackup = data.users; + done(); + } + ); + }); + + after(() => { + fs.writeFile( + path.join(__dirname, "../../database/users.json"), + JSON.stringify({ users: usersBackup }), + "utf-8", + (err) => { + if (err) throw err; + } + ); + }); + + it("Create a user with reasonable data", function (done) { + const data = { + name: "Angela", + birthDate: "2000-01-01", + phone: "+34 123456789", + email: "angela@gmail.com", + password: "123456789", + }; + users.createOne(data, (err, id) => { + if (err) throw err; + assert.isNumber(id, "id is not a number"); + fs.readFile( + path.join(__dirname, "../../database/users.json"), + "utf-8", + (err, _data) => { + if (err) throw err; + const data = JSON.parse(_data); + const user = data.users.filter((_user) => _user.id === id)[0]; + assert.isObject(user); + assert.equal(Object.keys(user).length, 6); + assert.isString(user.name); + assert.equal(user.name, "Angela"); + assert.isString(user.birth_date); + assert.equal(user.birth_date, "2000-01-01"); + assert.isString(user.phone); + assert.equal(user.phone, "+34 123456789"); + assert.isString(user.email); + assert.equal(user.email, "angela@gmail.com"); + assert.isString(user.password); + assert.equal(user.password, "123456789"); + done(); + } + ); + }); + }); + + it("Create a user with invalid email", function (done) { + const data = { + name: "Angela", + birthDate: "2000-01-01", + phone: "+34 123456789", + email: "angela @gmail.com", + password: "123456789", + }; + try { + users.createOne(data, (err) => { + if (err) throw err; + }); + } catch (err) { + assert.isTrue(err instanceof EmailNotValidError); + assert.equal(err.message, "Email is not valid"); + done(); + } + }); + + it("Create a user with duplicated email", function (done) { + const data = { + name: "Angela", + birthDate: "2000-01-01", + phone: "+34 123456789", + email: "duplicated@gmail.com", + password: "123456789", + }; + const data2 = { + name: "Perico", + birthDate: "1992-10-01", + phone: "+34 123456789", + email: "duplicated@gmail.com", + password: "123456789", + }; + users.createOne(data, (err) => { + if (err) throw err; + try { + users.createOne(data2, (err) => { + assert.isTrue(err instanceof Error); + assert.equal(err.message, "Email already in use"); + done(); + }); + } catch (err) { + if (err) throw err; + } + }); + }); + }); +}); \ No newline at end of file diff --git a/staff/borja-garcia/json-bbdd/test/users/delete-one.test.js b/staff/borja-garcia/json-bbdd/test/users/delete-one.test.js new file mode 100644 index 00000000..21a6d40d --- /dev/null +++ b/staff/borja-garcia/json-bbdd/test/users/delete-one.test.js @@ -0,0 +1,81 @@ +const { users } = require("../../scripts"); +const fs = require("fs"); +const path = require("path"); +const { assert } = require("chai"); + +describe("Users scripts", function () { + describe("#deleteOne", function () { + let usersBackup = null; + + before((done) => { + fs.readFile( + path.join(__dirname, "../../database/users.json"), + "utf-8", + (err, _data) => { + if (err) throw err; + const data = JSON.parse(_data); + usersBackup = data.users; + done(); + } + ); + }); + + after(() => { + fs.writeFile( + path.join(__dirname, "../../database/users.json"), + JSON.stringify({ users: usersBackup }), + "utf-8", + (err) => { + if (err) throw err; + } + ); + }); + + context("", () => { + const user = { + id: 10000, + name: "Angela", + birthDate: "2000-01-01", + phone: "+34 123456789", + email: "angela@gmail.com", + password: "123456789", + }; + + beforeEach((done) => { + fs.writeFile( + path.join(__dirname, "../../database/users.json"), + JSON.stringify({ + users: [...usersBackup, user], + }), + "utf-8", + (err) => { + if (err) throw err; + done(); + } + ); + }); + + it("Delete user with provided id", function (done) { + users.deleteOne(user.id, user.password, (err, ok) => { + if (err) throw err; + + assert.isTrue(ok, "Response is not true"); + + fs.readFile( + path.join(__dirname, "../../database/users.json"), + "utf-8", + (err, _data) => { + if (err) throw err; + const data = JSON.parse(_data); + const foundUser = data.users.filter( + (_user) => _user.id === user.id + )[0]; + assert.notExists(foundUser, "User has not been removed"); + done(); + } + ); + }); + }); + }); + }); +}); \ No newline at end of file diff --git a/staff/borja-garcia/json-bbdd/test/users/index.js b/staff/borja-garcia/json-bbdd/test/users/index.js new file mode 100644 index 00000000..60870819 --- /dev/null +++ b/staff/borja-garcia/json-bbdd/test/users/index.js @@ -0,0 +1,13 @@ +const createOneTest = require("./create-one.test.js"); +const deleteOneTest = require("./delete-one.test.js"); +const readAllTest = require("./read-all.test.js"); +const readOneTest = require("./read-one.test.js"); +const updateByIdTest = require("./update-by-id.test.js"); + +module.exports = { + createOneTest, + deleteOneTest, + readAllTest, + readOneTest, + updateByIdTest, +}; \ No newline at end of file diff --git a/staff/borja-garcia/json-bbdd/test/users/read-all.test.js b/staff/borja-garcia/json-bbdd/test/users/read-all.test.js new file mode 100644 index 00000000..7bc950b0 --- /dev/null +++ b/staff/borja-garcia/json-bbdd/test/users/read-all.test.js @@ -0,0 +1,43 @@ +const { users } = require("../../scripts"); +const fs = require("fs"); +const path = require("path"); +const { assert } = require("chai"); + +describe("Users scripts", function () { + describe("#readAll", function () { + let usersBackup = null; + + before((done) => { + fs.readFile( + path.join(__dirname, "../../database/users.json"), + "utf-8", + (err, _data) => { + if (err) throw err; + const data = JSON.parse(_data); + usersBackup = data.users; + done(); + } + ); + }); + + after(() => { + fs.writeFile( + path.join(__dirname, "../../database/users.json"), + JSON.stringify({ users: usersBackup }), + "utf-8", + (err) => { + if (err) throw err; + } + ); + }); + + it("All database users listed correctly", function (done) { + users.readAll((err, users) => { + if (err) throw err; + assert.notExists(err); + assert.isArray(users); + done(); + }); + }); + }); +}); \ No newline at end of file diff --git a/staff/borja-garcia/json-bbdd/test/users/read-one.test.js b/staff/borja-garcia/json-bbdd/test/users/read-one.test.js new file mode 100644 index 00000000..f315a4f6 --- /dev/null +++ b/staff/borja-garcia/json-bbdd/test/users/read-one.test.js @@ -0,0 +1,68 @@ +const { users } = require("../../scripts"); +const fs = require("fs"); +const path = require("path"); +const { assert } = require("chai"); + +describe("Users scripts", function () { + describe("#readOne", function () { + let usersBackup = null; + + before((done) => { + fs.readFile( + path.join(__dirname, "../../database/users.json"), + "utf-8", + (err, _data) => { + if (err) throw err; + const data = JSON.parse(_data); + usersBackup = data.users; + done(); + } + ); + }); + + after(() => { + fs.writeFile( + path.join(__dirname, "../../database/users.json"), + JSON.stringify({ users: usersBackup }), + "utf-8", + (err) => { + if (err) throw err; + } + ); + }); + + context("", () => { + const user = { + id: 10000, + name: "Angela", + birthDate: "2000-01-01", + phone: "+34 123456789", + email: "angela@gmail.com", + password: "123456789", + }; + + beforeEach((done) => { + fs.writeFile( + path.join(__dirname, "../../database/users.json"), + JSON.stringify({ + users: [...usersBackup, user], + }), + "utf-8", + (err) => { + if (err) throw err; + done(); + } + ); + }); + + it("User correctly listed", function (done) { + users.readOne(user.id, (err, user) => { + if (err) throw err; + assert.notExists(err); + assert.isObject(user); + done(); + }); + }); + }); + }); +}); \ No newline at end of file diff --git a/staff/borja-garcia/json-bbdd/test/users/update-by-id.test.js b/staff/borja-garcia/json-bbdd/test/users/update-by-id.test.js new file mode 100644 index 00000000..26b7d6a5 --- /dev/null +++ b/staff/borja-garcia/json-bbdd/test/users/update-by-id.test.js @@ -0,0 +1,68 @@ +const { users } = require("../../scripts"); +const fs = require("fs"); +const path = require("path"); +const { assert } = require("chai"); + +describe("Users scripts", function () { + describe("#updateById", function () { + let usersBackup = null; + + before((done) => { + fs.readFile( + path.join(__dirname, "../../database/users.json"), + "utf-8", + (err, _data) => { + if (err) throw err; + const data = JSON.parse(_data); + usersBackup = data.users; + done(); + } + ); + }); + + after(() => { + fs.writeFile( + path.join(__dirname, "../../database/users.json"), + JSON.stringify({ users: usersBackup }), + "utf-8", + (err) => { + if (err) throw err; + } + ); + }); + + context("", () => { + const user = { + id: 10000, + name: "Angela", + birthDate: "2000-01-01", + phone: "+34 123456789", + email: "angela@gmail.com", + password: "123456789", + }; + + beforeEach((done) => { + fs.writeFile( + path.join(__dirname, "../../database/users.json"), + JSON.stringify({ + users: [...usersBackup, user], + }), + "utf-8", + (err) => { + if (err) throw err; + done(); + } + ); + }); + + it("User correctly updated", function (done) { + users.updateById(user.id, { phone: "+34 9876544321" }, (err, ok) => { + if (err) throw err; + assert.notExists(err); + assert.isTrue(ok); + done(); + }); + }); + }); + }); +}); \ No newline at end of file From e1f98848ab3641824ae36371d76b7437926ab77f Mon Sep 17 00:00:00 2001 From: itsmePo Date: Mon, 23 Sep 2024 21:22:35 +0200 Subject: [PATCH 4/5] modified users.json --- staff/borja-garcia/json-bbdd/database/users.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/staff/borja-garcia/json-bbdd/database/users.json b/staff/borja-garcia/json-bbdd/database/users.json index 4b527997..58528dcd 100644 --- a/staff/borja-garcia/json-bbdd/database/users.json +++ b/staff/borja-garcia/json-bbdd/database/users.json @@ -1 +1 @@ -{"users":null} \ No newline at end of file +{"users":[{"id":0,"name":"Limón","birth_date":"1985-01-04","phone":"+34 111111111","email":"limoncode@gmail.com","password":"12345678"},{"id":1,"name":"Borja","birth_date":"1995-08-30","phone":"+34 987654321","email":"itspuo@gmail.com","password":"12345678"},{"id":2,"name":"Pepito","birth_date":"1995-08-30","phone":"+34 987654321","email":"foo@gmail.com","password":"12345678"},{"id":3,"name":"Rafa","birth_date":"1983-02-07","phone":"+34 123456789","email":"nosoyrafa@gmail.com","password":"12345678"},{"id":4,"name":"Rafa","birth_date":"1983-02-07","phone":"+34 123456789","email":"nosoyrafa2@gmail.com","password":"12345678"},{"id":5,"name":"ventu","birth_date":"2000-01-02","phone":"+34 987654321","email":"ventu@gmail.com","password":"123456789"}]} \ No newline at end of file From 3e22ecdc5f4eb43538484a3e2e3289a68f0dc16e Mon Sep 17 00:00:00 2001 From: itsmePo Date: Tue, 24 Sep 2024 13:05:37 +0200 Subject: [PATCH 5/5] Updated files; solving errors; nothing explodes anymore --- .../json-bbdd/scripts/users/create-one.js | 57 +++++++++---------- .../json-bbdd/scripts/users/delete-one.js | 41 ++++++------- .../json-bbdd/scripts/users/read-all.js | 21 +++---- .../json-bbdd/scripts/users/read-one.js | 28 ++++----- .../json-bbdd/scripts/users/update-by-id.js | 34 +++++++---- .../json-bbdd/test/users/create-one.test.js | 2 + .../json-bbdd/test/users/index.js | 8 +-- 7 files changed, 93 insertions(+), 98 deletions(-) diff --git a/staff/borja-garcia/json-bbdd/scripts/users/create-one.js b/staff/borja-garcia/json-bbdd/scripts/users/create-one.js index db0dd3ce..ac6cd15c 100644 --- a/staff/borja-garcia/json-bbdd/scripts/users/create-one.js +++ b/staff/borja-garcia/json-bbdd/scripts/users/create-one.js @@ -1,40 +1,37 @@ const fs = require("fs"); const path = require("path"); const readAll = require("./read-all.js"); -const { EmailNotValidError } = require("../../errors/index.js"); +const { EmailNotValidError } = require("../../errors"); function createOne(data, callback) { const { name, birthDate, phone, email, password } = data; - const emailRegexp = new RegExp ( - /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|.(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ + const emailRegexp = new RegExp( + /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|.(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ ); if (!emailRegexp.test(email)) - throw new EmailNotValidError("Email is not valid"); - readAll((users) => { - const isEmailDuplicated = users.some((user) => user.email === email); - if (isEmailDuplicated) throw new Error("User already exists"); - - const id = users[users.length -1].id + 1 ; - users.push({ - id, - name, - birth_date: birthDate, - phone, - email, - password, - }); - - fs.writeFile( - path.join(__dirname, "../../database/users.json"), - JSON.stringify({ users: users}), - "utf-8", - (err) => { - if (err) throw err; - - callback(id); - } - ); + throw new EmailNotValidError("Email is not valid"); + readAll((err, users) => { + if (err) return callback(err); + const isEmailDuplicated = users.some((user) => user.email === email); + if (isEmailDuplicated) return callback(new Error("Email already in use")); + const id = users[users.length - 1].id + 1; + users.push({ + id, + name, + birth_date: birthDate, + phone, + email, + password, + }); + fs.writeFile( + path.join(__dirname, "../../database/users.json"), + JSON.stringify({ users: users }), + "utf-8", + (err) => { + if (err) return callback(err); + callback(null, id); + } + ); }); -} - + } module.exports = createOne; \ No newline at end of file diff --git a/staff/borja-garcia/json-bbdd/scripts/users/delete-one.js b/staff/borja-garcia/json-bbdd/scripts/users/delete-one.js index f806b8d0..0a06fb01 100644 --- a/staff/borja-garcia/json-bbdd/scripts/users/delete-one.js +++ b/staff/borja-garcia/json-bbdd/scripts/users/delete-one.js @@ -1,29 +1,24 @@ const fs = require("fs"); const path = require("path"); -const read = require("./read-all.js"); +const readAll = require("./read-all.js"); const { NotFoundError, CredentialsError } = require("../../errors"); - function deleteOne(id, password, callback) { - read((users) => { - const user = users.filter((_user) => _user.id === id)[0]; - if (!user) throw new NotFoundError("User not found"); - - if (! (user.password === password)) - throw new CredentialsError("Password doesn't match"); - - const newUsers = users.filter((_user) => !(_user.id === id)); - - fs.writeFile( - path.join(__dirname, "../..database/users.json"), - JSON.stringify({ users: newUsers}), - "utf-8", - (err) => { - if (err) throw err; - - callback(true); - } - ); - }); + readAll((err, users) => { + if (err) throw err; + const user = users?.filter((_user) => _user.id === id)[0]; + if (!user) return callback(new NotFoundError("User not found")); + if (!(user.password === password)) + return callback(new CredentialsError("Password doesn't match")); + const newUsers = users.filter((_user) => !(_user.id === id)); + fs.writeFile( + path.join(__dirname, "../../database/users.json"), + JSON.stringify({ users: newUsers }), + "utf-8", + (err) => { + if (err) return callback(err); + callback(null, true); + } + ); + }); } - module.exports = deleteOne; \ No newline at end of file diff --git a/staff/borja-garcia/json-bbdd/scripts/users/read-all.js b/staff/borja-garcia/json-bbdd/scripts/users/read-all.js index 10de567c..20d7c1d1 100644 --- a/staff/borja-garcia/json-bbdd/scripts/users/read-all.js +++ b/staff/borja-garcia/json-bbdd/scripts/users/read-all.js @@ -1,17 +1,14 @@ const fs = require("fs"); const path = require("path"); - function readAll(callback) { - fs.readFile( - path.join(__dirname, "/../database/users.json"), - "utf-8", - (err, _data) => { - if (err) throw err; - - const data = JSON.parse(_data); - callback(data.users); - } - ); + fs.readFile( + path.join(__dirname, "../../database/users.json"), + "utf-8", + (err, _data) => { + if (err) return callback(err); + const data = JSON.parse(_data); + callback(null, data.users); + } + ); } - module.exports = readAll; \ No newline at end of file diff --git a/staff/borja-garcia/json-bbdd/scripts/users/read-one.js b/staff/borja-garcia/json-bbdd/scripts/users/read-one.js index ff6b42fb..8aba3a5a 100644 --- a/staff/borja-garcia/json-bbdd/scripts/users/read-one.js +++ b/staff/borja-garcia/json-bbdd/scripts/users/read-one.js @@ -1,23 +1,17 @@ const fs = require("fs"); const path = require("path"); const { NotFoundError } = require("../../errors"); - function readOne(id, callback) { - fs.readFile( - path.join(__dirname, "../database/users.json"), - "utf-8", - (err, _data) => { - if (err) throw err; - - const data = JSON.parse(_data); - - const user = data.users.filter((_user) => _user.id === id)[0]; - - if (!user) throw new NotFoundError("User not found"); - - callback(user); - } - ); + fs.readFile( + path.join(__dirname, "../../database/users.json"), + "utf-8", + (err, _data) => { + if (err) return callback(err); + const data = JSON.parse(_data); + const user = data.users.filter((_user) => _user.id === id)[0]; + if (!user) return callback(new NotFoundError("User not found")); + callback(null, user); + } + ); } - module.exports = readOne; \ No newline at end of file diff --git a/staff/borja-garcia/json-bbdd/scripts/users/update-by-id.js b/staff/borja-garcia/json-bbdd/scripts/users/update-by-id.js index 3b2af628..7bb85de9 100644 --- a/staff/borja-garcia/json-bbdd/scripts/users/update-by-id.js +++ b/staff/borja-garcia/json-bbdd/scripts/users/update-by-id.js @@ -1,24 +1,34 @@ const fs = require("fs"); const path = require("path"); -const read = require("./read-all.js"); +const readAll = require("./read-all.js"); const { NotAnIntegerError } = require("../../errors/index.js"); -function updateById(id, data) { +function updateById(id, data, callback) { if (typeof id !== "number") throw new TypeError("id is not a number"); if (id < 0 || id === NaN || id === Infinity) - throw new RangeError("id is out of range"); + throw new RangeError("id is out of range"); if (!Number.isInteger(id)) - throw new NotAnIntegerError("id is not an integer"); + throw new NotAnIntegerError("id is not an integer"); const { name, birthDate, phone } = data; - read((users) => { - users.forEach((user) => { - if (user.id === id) { - user.name = name ?? user.name; - user.birth_date = birthDate ?? user.birth_date; - user.phone = phone ?? user.phone; + readAll((err, users) => { + if (err) return callback(err); + users.forEach((user) => { + if (user.id === id) { + user.name = name ?? user.name; + user.birth_date = birthDate ?? user.birth_date; + user.phone = phone ?? user.phone; } + }); + fs.writeFile( + path.join(__dirname, "../../database/users.json"), + JSON.stringify({ users: users }), + "utf-8", + (err) => { + if (err) return callback(err); + callback(null, true); + } + ); }); -}); -} + } module.exports = updateById; \ No newline at end of file diff --git a/staff/borja-garcia/json-bbdd/test/users/create-one.test.js b/staff/borja-garcia/json-bbdd/test/users/create-one.test.js index 49f78924..9f428706 100644 --- a/staff/borja-garcia/json-bbdd/test/users/create-one.test.js +++ b/staff/borja-garcia/json-bbdd/test/users/create-one.test.js @@ -4,6 +4,7 @@ const path = require("path"); const { assert } = require("chai"); const { EmailNotValidError } = require("../../errors"); + describe("Users scripts", function () { describe("#createOne", function () { let usersBackup = null; @@ -15,6 +16,7 @@ describe("Users scripts", function () { (err, _data) => { if (err) throw err; const data = JSON.parse(_data); + // console.log(path.dirname()); usersBackup = data.users; done(); } diff --git a/staff/borja-garcia/json-bbdd/test/users/index.js b/staff/borja-garcia/json-bbdd/test/users/index.js index 60870819..dc826f17 100644 --- a/staff/borja-garcia/json-bbdd/test/users/index.js +++ b/staff/borja-garcia/json-bbdd/test/users/index.js @@ -6,8 +6,8 @@ const updateByIdTest = require("./update-by-id.test.js"); module.exports = { createOneTest, - deleteOneTest, - readAllTest, - readOneTest, - updateByIdTest, + deleteOneTest, +readAllTest, +readOneTest, +updateByIdTest, }; \ No newline at end of file