Skip to content

Commit

Permalink
add some json-bbdd files b00tc4mp#207
Browse files Browse the repository at this point in the history
  • Loading branch information
angelabernaldz committed Sep 23, 2024
1 parent 4523002 commit 5e56cb2
Show file tree
Hide file tree
Showing 15 changed files with 145 additions and 48 deletions.
53 changes: 52 additions & 1 deletion staff/angela-bernaldez/json-bbdd/database/users.json
Original file line number Diff line number Diff line change
@@ -1 +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":"Angela","birth_date":"1998-02-06","phone":"+34 669488278","email":"[email protected]","password":"holamellamoangela"}]}
{
"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
12 changes: 0 additions & 12 deletions staff/angela-bernaldez/json-bbdd/index.js

This file was deleted.

9 changes: 9 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,9 @@
const { users } = require("../scripts")

users.createOne({
name: "pepito",
birthDate: "2020-01-01",
phone: "123456789",
email: "[email protected]",
password: "passswordddd"
})
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 };
14 changes: 1 addition & 13 deletions staff/angela-bernaldez/json-bbdd/scripts/users/create-one.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ 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;
Expand All @@ -31,19 +28,10 @@ function createOne(data) {
JSON.stringify({ users: users }),
"utf-8",
(err) => {
console.log(err);
if (err) throw err
}
);
});
}


create({
name: "Portal",
birthDate: "1995-11-03",
phone: "+34 123456789",
email: "[email protected]",
password: "holamellamoportal"
})

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

Empty file.
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,
};
3 changes: 0 additions & 3 deletions staff/angela-bernaldez/json-bbdd/scripts/users/read-all.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
const fs = require("fs")
const path = require("path")



function readAll(callback) {

fs.readFile(
path.join(__dirname, "../../database/users.json"),
"utf-8",
Expand Down
14 changes: 3 additions & 11 deletions staff/angela-bernaldez/json-bbdd/scripts/users/read-one.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
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",
Expand All @@ -15,17 +13,11 @@ function readOne(id, callback) {

const user = data.users.filter((_user) => _user.id === id)[0]

// if (!user) throw new Error
if (!user) throw new NotFoundError("User not found")

callback(user);
}
)
}

module.exports = readOne

const id = 1

readOne(id, (user) => {
console.log(user)
})
module.exports = readOne
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ 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;

Expand All @@ -23,14 +20,10 @@ function updateById(id, data) {
JSON.stringify({ users: users }),
"utf-8",
(err) => {
console.log(err);
if (err) throw err
}
);
});
}


updateById(0, { phone: "+34 111111111" })


module.exports = updateById
Empty file.

0 comments on commit 5e56cb2

Please sign in to comment.