forked from oliverplay/nodejs-homework-template
-
Notifications
You must be signed in to change notification settings - Fork 122
Hw02 Anca Sabadeanu #135
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
Open
Ancasab
wants to merge
4
commits into
goit-i18n:master
Choose a base branch
from
Ancasab:hw02
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Hw02 Anca Sabadeanu #135
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,3 +10,6 @@ module.exports = { | |
| }, | ||
| rules: {}, | ||
| } | ||
|
|
||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,149 @@ | ||
| // const fs = require('fs/promises') | ||
|
|
||
| const listContacts = async () => {} | ||
| import fs from 'node:fs/promises'; | ||
| import { dirname } from 'node:path'; | ||
| import { fileURLToPath } from 'node:url'; | ||
| import { readFile } from 'node:fs/promises'; | ||
| import { randomUUID } from 'node:crypto'; | ||
| import { writeFile } from 'node:fs/promises'; | ||
|
|
||
| const getContactById = async (contactId) => {} | ||
|
|
||
| const removeContact = async (contactId) => {} | ||
|
|
||
| const addContact = async (body) => {} | ||
| const __dirname = dirname(fileURLToPath(import.meta.url)); | ||
|
|
||
| const updateContact = async (contactId, body) => {} | ||
|
|
||
| module.exports = { | ||
| const contactsPath = `${__dirname}/contacts.json`; | ||
|
|
||
|
|
||
|
|
||
| export async function listContacts() { | ||
| try { | ||
| const contents = await readFile(contactsPath, { encoding: "utf8" }); | ||
| const contacts = JSON.parse(contents); | ||
| return contacts; | ||
|
|
||
| } catch (error) { | ||
| console.log("There is an error"); | ||
| console.error(error); | ||
| } | ||
| }; | ||
|
|
||
|
|
||
|
|
||
| export async function getContactById(contactId) { | ||
| try { | ||
| const contents = await readFile(contactsPath, { encoding: "utf8" }); | ||
| const contacts = JSON.parse(contents); | ||
|
|
||
| const contact = contacts.find((contact) => contact.id === String(contactId)); | ||
|
|
||
| if (contact) { | ||
| return contact; | ||
| } | ||
|
|
||
| } catch (error) { | ||
| console.log("There is an error"); | ||
| console.error(error); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| export async function addContact(contact) { | ||
| try { | ||
| const contents = await readFile(contactsPath, { encoding: "utf8" }); | ||
| const contacts = JSON.parse(contents); | ||
| const newContactId = randomUUID(); | ||
|
|
||
| const isValid = contact?.name && contact?.email && contact?.phone; | ||
| if (!isValid) { | ||
| throw new Error('The contact does not have all required parametres'); | ||
| } | ||
|
|
||
| const newContact = { | ||
| id: newContactId, | ||
| ...contact, | ||
| }; | ||
|
|
||
| contacts.push(newContact); | ||
| const parsedContact = JSON.stringify(contacts, null, 2); | ||
| await writeFile(contactsPath, parsedContact); | ||
|
|
||
|
|
||
| console.log('The contact has been ceated successfully !'); | ||
| return newContact; | ||
|
|
||
|
|
||
| } catch (error) { | ||
| console.log("There is an error"); | ||
| console.error(error); | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
| export async function updateContact(updatedFields, contactId) { | ||
| try { | ||
| const contents = await readFile(contactsPath, { encoding: "utf8" }); | ||
| const contacts = JSON.parse(contents); | ||
|
|
||
|
|
||
| const contactIndex = contacts.findIndex((contact) => contact.id === contactId); | ||
| if (contactIndex === -1) { | ||
| return null; | ||
| } | ||
|
|
||
|
|
||
| const updatedContact = { | ||
| ...contacts[contactIndex], | ||
| ...updatedFields, | ||
| }; | ||
| contacts[contactIndex] = updatedContact; | ||
|
|
||
|
|
||
| await writeFile(contactsPath, JSON.stringify(contacts, null, 2)); | ||
| return updatedContact; | ||
| } catch (error) { | ||
| console.error("There is an error", error); | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| export async function removeContact(contactId) { | ||
| try { | ||
| const contents = await readFile(contactsPath, { encoding: "utf8" }); | ||
| const contacts = JSON.parse(contents); | ||
|
|
||
|
|
||
| const contactIndex = contacts.findIndex((contact) => contact.id === String(contactId)); | ||
| if (contactIndex === -1) { | ||
| console.log(`Contact with ID ${contactId} does not exist.`); | ||
Ancasab marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return null; | ||
| } | ||
|
|
||
| const removedContact = contacts[contactIndex]; | ||
|
|
||
|
|
||
| const updatedContacts = contacts.filter((contact) => contact.id !== String(contactId)); | ||
| await writeFile(contactsPath, JSON.stringify(updatedContacts, null, 2)); | ||
|
|
||
| console.log(`The contact with ID ${contactId} has been removed successfully!`); | ||
Ancasab marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| console.table(updatedContacts); | ||
|
|
||
|
|
||
| return removedContact; | ||
| } catch (error) { | ||
| console.error("There is an error", error); | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| const contactsService = { | ||
| listContacts, | ||
| getContactById, | ||
| removeContact, | ||
| addContact, | ||
| updateContact, | ||
| } | ||
|
|
||
| export default contactsService; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,14 +49,44 @@ | |
| }, | ||
| { | ||
| "id": "e6ywwRe4jcqxXfCZOj_1e", | ||
| "name": "Thomas Lucas", | ||
| "email": "nec@Nulla.com", | ||
| "phone": "(704) 398-7993" | ||
| "name": "Marcus Lucas", | ||
| "email": "marc@Nulla.com", | ||
| "phone": "(700) 398-7993" | ||
| }, | ||
| { | ||
| "id": "rsKkOQUi80UsgVPCcLZZW", | ||
| "name": "Alec Howard", | ||
| "email": "[email protected]", | ||
| "phone": "(748) 206-2688" | ||
| }, | ||
| { | ||
| "id": "a70cff55-5c3a-4aa1-9c0d-77d12ed7bc55", | ||
| "name": "James Doe", | ||
| "email": "[email protected]", | ||
| "phone": "(705) 398-7993" | ||
| }, | ||
| { | ||
| "id": "04546e12-4811-4154-859f-3f350a1af85b", | ||
| "name": "James Moon", | ||
| "email": "[email protected]", | ||
| "phone": "(711) 398-7993" | ||
| }, | ||
| { | ||
| "id": "bcc8f110-2610-47fd-9f4f-080707fe8085", | ||
| "name": "Maria Calas", | ||
| "email": "[email protected]", | ||
| "phone": "(799) 398-7993" | ||
| }, | ||
| { | ||
| "id": "466b28b7-4722-4ece-add7-fc2b15d961ed", | ||
| "name": "Joe Biden", | ||
| "email": "[email protected]", | ||
| "phone": "(711) 398-7981" | ||
| }, | ||
| { | ||
| "id": "ab895d5a-1806-4b62-93f2-44fcedbb3223", | ||
| "name": "Joe Biden", | ||
| "email": "[email protected]", | ||
| "phone": "(712) 398-7981" | ||
| } | ||
| ] | ||
| ] | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.