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

nodejs-hw #12

Open
wants to merge 2 commits into
base: main
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
1,093 changes: 1,093 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"license": "ISC",
"devDependencies": {
"@eslint/js": "^9.2.0",
"@faker-js/faker": "^9.0.3",
"eslint": "^9.2.0",
"globals": "^15.1.0"
}
Expand Down
4 changes: 3 additions & 1 deletion src/constants/contacts.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export const PATH_DB =
import path from "node:path";

export const PATH_DB = path.resolve('src/db/db.json');
38 changes: 37 additions & 1 deletion src/db/db.json
Original file line number Diff line number Diff line change
@@ -1 +1,37 @@
[]
[
{
"id": "e0fcb3b0-7d73-4acb-b53f-69005454cbc4",
"name": "Omar Adams V",
"phone": "517-280-4366 x107",
"email": "[email protected]",
"job": "Product Security Developer"
},
{
"id": "99ff9eb6-46b7-4795-b4b8-a301de7649f0",
"name": "Jack Bernier",
"phone": "1-253-723-3082 x66808",
"email": "[email protected]",
"job": "International Division Planner"
},
{
"id": "f525adf6-0ae0-4102-bdb8-05c1ce7e7973",
"name": "Edgar Wiegand",
"phone": "(346) 943-5360 x4669",
"email": "[email protected]",
"job": "Chief Brand Consultant"
},
{
"id": "fafa257a-bc84-4ae8-b2bc-7216ef4b3461",
"name": "Dora Waelchi",
"phone": "(216) 499-5185 x276",
"email": "[email protected]",
"job": "Internal Applications Agent"
},
{
"id": "60b9f900-ac6a-4ea4-afb6-321aa7c3116b",
"name": "Johnnie Runolfsdottir",
"phone": "(488) 604-4807",
"email": "[email protected]",
"job": "International Paradigm Engineer"
}
]
18 changes: 16 additions & 2 deletions src/scripts/addOneContact.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
export const addOneContact = async () => {};
import { createFakeContact } from '../utils/createFakeContact.js';
import { readContacts } from '../utils/readContacts.js';
import { writeContacts } from '../utils/writeContacts.js';

addOneContact();
export const addOneContact = async () => {
try {
const data = await readContacts();
const contacts = JSON.parse(data);
const fakeContact = createFakeContact();
const dataContacts = [...contacts, fakeContact];
await writeContacts(JSON.stringify(dataContacts));
} catch (error) {
console.log(error);
}
};

addOneContact();
13 changes: 11 additions & 2 deletions src/scripts/countContacts.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
export const countContacts = async () => {};
import { readContacts } from '../utils/readContacts.js';

console.log(await countContacts());
export const countContacts = async () => {
try {
const data = await readContacts();
return JSON.parse(data).length;
} catch (error) {
console.log(error);
}
};

console.log(await countContacts());
17 changes: 16 additions & 1 deletion src/scripts/generateContacts.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
const generateContacts = async (number) => {};
import { createFakeContact } from '../utils/createFakeContact.js';
import { readContacts } from '../utils/readContacts.js';
import { writeContacts } from '../utils/writeContacts.js';

const generateContacts = async (number) => {
try {
const contacts = JSON.parse(await readContacts());
for (let i = 0; i < number; i ++) {
contacts.push(createFakeContact());
}
await writeContacts(JSON.stringify(contacts, undefined, number));

} catch (error) {
console.log(error);
}
};

generateContacts(5);
14 changes: 12 additions & 2 deletions src/scripts/getAllContacts.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
export const getAllContacts = async () => {};
import { readContacts } from '../utils/readContacts.js';

console.log(await getAllContacts());

export const getAllContacts = async () => {
try {
return JSON.parse(await readContacts());

} catch (error) {
console.log(error);
}
};

console.log(await getAllContacts());
15 changes: 13 additions & 2 deletions src/scripts/removeAllContacts.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
export const removeAllContacts = async () => {};
import { readContacts } from '../utils/readContacts.js';
import { writeContacts } from '../utils/writeContacts.js';

removeAllContacts();
export const removeAllContacts = async () => {
try {
const contacts = JSON.parse(await readContacts());
contacts.length = 0;
await writeContacts(JSON.stringify(contacts));
} catch (error) {
console.log(error);
}
};

removeAllContacts();
15 changes: 13 additions & 2 deletions src/scripts/removeLastContact.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
export const removeLastContact = async () => {};
import { readContacts } from '../utils/readContacts.js';
import { writeContacts } from '../utils/writeContacts.js';

removeLastContact();
export const removeLastContact = async () => {
try {
const contacts = JSON.parse(await readContacts());
contacts.pop();
await writeContacts(JSON.stringify(contacts, undefined, 2));
} catch (e) {
console.log(e);
}
};

removeLastContact();
6 changes: 5 additions & 1 deletion src/utils/readContacts.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { PATH_DB } from '../constants/contacts.js';
import fs from 'node:fs/promises';

export const readContacts = async () => {};
export const readContacts = async () => {
const contacts = await fs.readFile(PATH_DB, { encoding: 'utf-8' });
return contacts;
};
5 changes: 4 additions & 1 deletion src/utils/writeContacts.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { PATH_DB } from '../constants/contacts.js';
import fs from 'node:fs/promises';

export const writeContacts = async (updatedContacts) => {};
export const writeContacts = async (updatedContacts) => {
await fs.writeFile(PATH_DB, updatedContacts);
};