Advertisement
sazid_iiuc

Untitled

Feb 3rd, 2021
760
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  /*'use strict';
  2.  
  3. const fs = require('fs');
  4.  
  5. let rawData = fs.readFile('contacts.json');
  6.  
  7. let allContacts = JSON.parse(rawData);
  8.  
  9. */
  10.  
  11. class Contact{
  12.     constructor() {
  13.         this.contacts = [];
  14.  
  15.         // this.contacts = allContacts;
  16.     }
  17.  
  18.     getAllContacts(){
  19.         return this.contacts;
  20.     }
  21.  
  22.     getContactsByID(id){
  23.         return this.contacts.find(contact => contact.id === id);
  24.     }
  25.  
  26.     createContact(contact){
  27.         contact.id = this.contacts.length + 1;
  28.         this.contacts.push(contact);
  29.  
  30.         return contact;
  31.     }
  32.  
  33.     updateContactByID(id, updatedValue){
  34.         let index = this.contacts.findIndex(contact => contact.id === id);
  35.  
  36.         this.contacts[index].name = updatedValue.name || this.contacts.name;
  37.         this.contacts[index].phone = updatedValue.phone || this.contacts.phone;
  38.  
  39.         return this.contacts[index];
  40.     }
  41.  
  42.     deleteContactByID(id){
  43.         let index = this.contacts.findIndex(contact => contact.id === id);
  44.         this.contacts = this.contacts.filter(contact => contact.id !== id);
  45.     }
  46.  
  47. }
  48.  
  49.  
  50. module.exports = new Contact()
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement