UNPKG

contact-service

Version:

A simple array based service layer for address book apps

135 lines (126 loc) 3.11 kB
module.exports = {}; module.exports.getAll = ()=>{ return contacts; }; module.exports.getById = id=>{ if(!id){ throw new Error("Id is required but missing"); } if(isNaN(id)){ throw new Error(`Id must be a "number", but got "${typeof id}"`); } id = parseInt(id); var c = contacts.filter(c=>c.id==id)[0]; return c? c: null; }; module.exports.addNew = (contact) => { if(!contact || typeof contact != "object"){ throw new Error(`contact was not supplied or was not an object`); } var requiredFields = ["first_name", "last_name", "email", "phone", "dob", "city"]; var missingFields = []; requiredFields.forEach(fld=>{ if(!(fld in contact)){ missingFields.push(fld); } }); if(missingFields.length){ throw new Error(`Missing fields - ${missingFields.join()}`); } contact.id = contacts.map(c=>c.id).sort((a, b)=>a>b?1:-1)[contacts.length-1] + 1; contacts.push(contact); return contact; }; module.exports.update = (contact) => { if(!contact || typeof contact != "object"){ throw new Error(`contact was not supplied or was not an object`); } var requiredFields = ["id", "first_name", "last_name", "email", "phone", "dob", "city"]; var missingFields = []; requiredFields.forEach(fld=>{ if(!(fld in contact)){ missingFields.push(fld); } }); if(missingFields.length){ throw new Error(`Missing fields - ${missingFields.join()}`); } var index = contacts.findIndex(c=>c.id==contact.id); if(index==-1){ throw new Error(`No contact for id ${contact.id}`); } contacts[index] = contact; return contact; }; module.exports.delete = (id) => { if(!id){ throw new Error(`id was not supplied`); } if(isNaN(id)){ throw new Error(`Id must be a "number", but got "${typeof id}"`); } id = parseInt(id); var index = contacts.findIndex(c=>c.id==id); if(index==-1){ throw new Error(`No contact for id ${contact.id}`); } var result = contacts.splice(index, 1); return result[0]; }; var contacts = [{ "id": 3, "first_name": "Allen", "last_name": "Smith", "email": "allensmith@mailinator.com", "phone": "5556278822", "dob": "1989-02-12", "city": "Chicago" }, { "id": 17, "first_name": "Amy", "last_name": "Stanley", "email": "astanleyg@netlog.com", "phone": "4689686754", "dob": "1972-03-10", "city": "Boto" }, { "id": 28, "first_name": "Annie", "last_name": "Simpson", "email": "asimpsonr@so-net.ne.jp", "phone": "6861508148", "dob": "1999-08-15", "city": "Pra\u017emo" }, { "id": 18, "first_name": "Barbara", "last_name": "Nichols", "email": "bnicholsh@virginia.edu", "phone": "7733856499", "dob": "2002-04-18", "city": "T\u00e2n Tru\u0323" }, { "id": 13, "first_name": "Beverly", "last_name": "Shaw", "email": "bshawc@wikimedia.org", "phone": "8046272581", "dob": "1950-01-27", "city": "Minna" }, { "id": 34, "first_name": "Carlos", "last_name": "Henry", "email": "chenryx@google.cn", "phone": "4853676264", "dob": "1941-09-08", "city": "Nanterre" }, { "id": 8, "first_name": "Chris", "last_name": "Simpson", "email": "csimpson7@weibo.com", "phone": "5774832826", "dob": "1940-12-15", "city": "Ningmute" }];