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

first #4

Open
wants to merge 1 commit 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,069 changes: 1,069 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": "^8.4.1",
"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';
const pathToWorkDir = path.join(process.cwd());
export const PATH_DB = path.join(pathToWorkDir, '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": "1733d5d9-6474-449f-b804-7a25c90d6bd3",
"name": "Francis Mraz",
"phone": "665-249-5681",
"email": "[email protected]",
"job": "Customer Directives Facilitator"
},
{
"id": "13f656af-c6cd-4df1-8fd0-4af5968900b5",
"name": "Lela Murazik",
"phone": "429.573.5892 x00860",
"email": "[email protected]",
"job": "Product Paradigm Manager"
},
{
"id": "78beecfe-5498-452b-b839-3311b263fea0",
"name": "Donnie Hermann-Bashirian",
"phone": "(484) 235-7310 x4947",
"email": "[email protected]",
"job": "Senior Paradigm Officer"
},
{
"id": "53e94edb-3571-47ac-a6bf-b8fd41ff51e4",
"name": "Warren Mohr",
"phone": "1-367-708-1319 x28897",
"email": "[email protected]",
"job": "Regional Quality Coordinator"
},
{
"id": "ed987314-1fd9-46ab-9aab-76b9b85d6a77",
"name": "Abel Bahringer III",
"phone": "(271) 796-6190 x280",
"email": "[email protected]",
"job": "Regional Directives Coordinator"
}
]
25 changes: 24 additions & 1 deletion src/scripts/addOneContact.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
import { PATH_DB } from '../constants/contacts.js';
import fs from 'node:fs/promises';
import { createFakeContact } from '../utils/createFakeContact.js';
import { getAllContacts } from './getAllContacts.js';

export const addOneContact = async () => {};
const writeContactsToFile = async (contacts) => {
try {
await fs.writeFile(PATH_DB, JSON.stringify(contacts, null, 2), 'utf8');
} catch (error) {
console.error('Error writing to db.json:', error);
}
};

const addOneContact = async () => {
const existingContacts = await getAllContacts();

const newContact = [];

newContact.push(createFakeContact());

const updatedContacts = existingContacts.concat(newContact);

await writeContactsToFile(updatedContacts);

console.log(`Successfully added one new contact.`);
};

addOneContact();
9 changes: 6 additions & 3 deletions src/scripts/countContacts.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { PATH_DB } from '../constants/contacts.js';

export const countContacts = async () => {};
import { getAllContacts } from './getAllContacts.js';
export const countContacts = async () => {
const arrContact = await getAllContacts();
const countContact = arrContact.length;
return countContact;
};

console.log(await countContacts());
28 changes: 26 additions & 2 deletions src/scripts/generateContacts.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
import { PATH_DB } from '../constants/contacts.js';
import fs from 'node:fs/promises';
import { createFakeContact } from '../utils/createFakeContact.js';
import { getAllContacts } from './getAllContacts.js';

const generateContacts = async (number) => {};
const writeContactsToFile = async (contacts) => {
try {
await fs.writeFile(PATH_DB, JSON.stringify(contacts, null, 2), 'utf8');
} catch (error) {
console.error('Error writing to db.json:', error);
}
};

generateContacts(5);
const generateContacts = async (number) => {
const existingContacts = await getAllContacts();

const newContacts = [];
for (let i = 0; i < number; i += 1) {
newContacts.push(createFakeContact());
}

const updatedContacts = existingContacts.concat(newContacts);

await writeContactsToFile(updatedContacts);

console.log(`Successfully added ${number} new contacts.`);
};

generateContacts(2);
11 changes: 10 additions & 1 deletion src/scripts/getAllContacts.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { PATH_DB } from '../constants/contacts.js';
import fs from 'node:fs/promises';

export const getAllContacts = async () => {};
export const getAllContacts = async () => {
try {
const data = await fs.readFile(PATH_DB, 'utf8');
return JSON.parse(data);
} catch (error) {
console.error('Error reading db.json:', error);
return [];
}
};

console.log(await getAllContacts());
10 changes: 8 additions & 2 deletions src/scripts/removeAllContacts.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { PATH_DB } from '../constants/contacts.js';

export const removeAllContacts = async () => {};
import fs from 'node:fs/promises';
export const removeAllContacts = async () => {
try {
await fs.writeFile(PATH_DB, '[]');
} catch (error) {
console.error('Error writing to db.json:', error);
}
};

removeAllContacts();
15 changes: 14 additions & 1 deletion src/scripts/removeLastContact.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import { PATH_DB } from '../constants/contacts.js';
import fs from 'node:fs/promises';
import { getAllContacts } from './getAllContacts.js';

export const removeLastContact = async () => {};
const writeContactsToFile = async (contacts) => {
try {
await fs.writeFile(PATH_DB, JSON.stringify(contacts, null, 2), 'utf8');
} catch (error) {
console.error('Error writing to db.json:', error);
}
};
export const removeLastContact = async () => {
const contacts = await getAllContacts();
const contactsWithoutLast = contacts.slice(0, -1);
writeContactsToFile(contactsWithoutLast);
};

removeLastContact();