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 3 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,157 @@ | ||
| // const fs = require('fs/promises') | ||
|
|
||
| const listContacts = async () => {} | ||
| import fs from 'node:fs/promises'; | ||
| import path from 'node:path'; | ||
| 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'; | ||
| import { Buffer } from 'node:buffer'; | ||
| import { error } from 'node:console'; | ||
|
|
||
| const getContactById = async (contactId) => {} | ||
|
|
||
| const removeContact = async (contactId) => {} | ||
| const __dirname = dirname(fileURLToPath(import.meta.url)); | ||
| // console.log(`Calea este :" ${__dirname }`); | ||
|
|
||
| const addContact = async (body) => {} | ||
| const contactsPath = `${__dirname}/contacts.json`; | ||
| // console.log(contactsPath); | ||
Ancasab marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| const updateContact = async (contactId, body) => {} | ||
|
|
||
| module.exports = { | ||
| 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) { | ||
| // console.table(contact); | ||
Ancasab marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return contact; | ||
| } else { | ||
Ancasab marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| console.log(`Contact with ID ${contactId} not found.`); | ||
| } | ||
| } 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, | ||
| }; | ||
| // console.dir(newContact); | ||
Ancasab marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| contacts.push(newContact); | ||
| const parsedContact = JSON.stringify(contacts, null, 2); | ||
| await writeFile(contactsPath, parsedContact); | ||
|
|
||
|
|
||
| console.log('The contact has been ceated successfully !'); | ||
Ancasab marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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); | ||
|
|
||
| // Găsim contactul după ID | ||
Ancasab marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const contactIndex = contacts.findIndex((contact) => contact.id === contactId); | ||
| if (contactIndex === -1) { | ||
| return null; // Contactul nu există | ||
Ancasab marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // Actualizam doar câmpurile specificate în body | ||
Ancasab marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const updatedContact = { | ||
| ...contacts[contactIndex], | ||
| ...updatedFields, | ||
| }; | ||
| contacts[contactIndex] = updatedContact; | ||
|
|
||
| // Salvam modificările în fișier | ||
| await writeFile(contactsPath, JSON.stringify(contacts, null, 2)); | ||
| return updatedContact; // Returnează contactul actualizat | ||
| } catch (error) { | ||
| console.error("There is an error", error); | ||
| throw error; // Aruncă eroarea pentru a fi gestionată de apelant | ||
Ancasab marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| export async function removeContact(contactId) { | ||
| try { | ||
| const contents = await readFile(contactsPath, { encoding: "utf8" }); | ||
| const contacts = JSON.parse(contents); | ||
|
|
||
| // Găsim contactul care trebuie șters | ||
| 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; // Returnează null dacă nu există | ||
Ancasab marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // Salvam contactul care urmează să fie șters | ||
Ancasab marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const removedContact = contacts[contactIndex]; | ||
|
|
||
| // Filtrăm contactele | ||
| 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); | ||
|
|
||
| // Returnează contactul șters | ||
| 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.