Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#207-JSON-BBDD #208

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions staff/angela-bernaldez/json-bbdd/database/users.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"users": [
{
"id": 0,
"name": "Limón",
"birth_date": "1985-01-04",
"phone": "+34 111111111",
"email": "[email protected]",
"password": "12345678"
},
{
"id": 1,
"name": "Borja",
"birth_date": "1995-08-30",
"phone": "+34 987654321",
"email": "[email protected]",
"password": "12345678"
},
{
"id": 2,
"name": "Pepito",
"birth_date": "1995-08-30",
"phone": "+34 987654321",
"email": "[email protected]",
"password": "12345678"
},
{
"id": 3,
"name": "Rafa",
"birth_date": "1983-02-07",
"phone": "+34 123456789",
"email": "[email protected]",
"password": "12345678"
},
{
"id": 4,
"name": "Rafa",
"birth_date": "1983-02-07",
"phone": "+34 123456789",
"email": "[email protected]",
"password": "12345678"
},
{
"id": 5,
"name": "ventu",
"birth_date": "2000-01-02",
"phone": "+34 987654321",
"email": "[email protected]",
"password": "123456789"
}
]
}
14 changes: 14 additions & 0 deletions staff/angela-bernaldez/json-bbdd/errors/credentials.js
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions staff/angela-bernaldez/json-bbdd/errors/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const CredentialsError = require("./credentials.js")
const NotFoundError = require("./not-found.js")

module.exports = {
NotFoundError,
CredentialsError,
}
14 changes: 14 additions & 0 deletions staff/angela-bernaldez/json-bbdd/errors/not-found.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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
10 changes: 10 additions & 0 deletions staff/angela-bernaldez/json-bbdd/interface/create-user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const readline = require("readline")
const { users } = require("../scripts")

const rl = readline.createInterface(
process.stdin, process.stdout)

rl.question()



3 changes: 3 additions & 0 deletions staff/angela-bernaldez/json-bbdd/scripts/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const users = require("./users");

module.exports = { users };
37 changes: 37 additions & 0 deletions staff/angela-bernaldez/json-bbdd/scripts/users/create-one.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const fs = require("fs")
const path = require("path")
const read = require("./read-all.js")

function createOne(data) {
// destructuring the object data to extract its properties
// user don´t pass id property as they do not know that info
const { name, birthDate, phone, email, password } = data;
read((users) => {
// check if there is already an user registered with that email
const isEmailDuplicated = users.some((user) => user.email === email);
// if there is an user registered with that email, throw an error when that email is trying to be used again
if (isEmailDuplicated) throw new Error("User already exists");

// if there is not an user registered with that email, add that to the database
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
28 changes: 28 additions & 0 deletions staff/angela-bernaldez/json-bbdd/scripts/users/delete-one.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
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

13 changes: 13 additions & 0 deletions staff/angela-bernaldez/json-bbdd/scripts/users/index.js
Original file line number Diff line number Diff line change
@@ -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,
};
17 changes: 17 additions & 0 deletions staff/angela-bernaldez/json-bbdd/scripts/users/read-all.js
Original file line number Diff line number Diff line change
@@ -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
23 changes: 23 additions & 0 deletions staff/angela-bernaldez/json-bbdd/scripts/users/read-one.js
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions staff/angela-bernaldez/json-bbdd/scripts/users/update-by-id.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const fs = require("fs")
const path = require("path")
const read = require("./read-all.js")

function updateById(id, data) {
// destructuring the object data to extract its properties
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