/// <summary> /// Constructor allows to intialize ContactRecord fields with Contact DTO object /// </summary> /// <param name="contact">Contact object</param> public ContactRecord(Contact contact) { Id = contact.Id; Owner = contact.Owner; FirstName = contact.FirstName; LastName = contact.LastName; Type = contact.Type; Number = contact.Number; }
/// <summary> /// Implementation of IPhoneBookRepository.AddContact(contact) which adds contact to database /// </summary> /// <param name="contact">Contact object to be added</param> /// <returns>Unique contact identifier</returns> public int AddContact(Contact contact) { if (contact == null || contact.Id != 0 || !IsContactConsistent(contact)) return 0; using (var db = GetPhoneBookContextInstance()) { contact.Owner = Owner; var contactRecord = new ContactRecord(contact); db.Contacts.Add(contactRecord); db.SaveChanges(); contact.Id = contactRecord.Id; return contact.Id; } }
public bool UpdateContact(Contact contact) { return Repository.UpdateContact(contact); }
public bool RemoveContact(Contact contact) { return Repository.RemoveContact(contact); }
public int AddContact(Contact contact) { return Repository.AddContact(contact); }
/// <summary> /// Implementation of IPhoneBookRepository.UpdateContact(contact) which updates existing in database contact /// </summary> /// <param name="contact">Existing contact object to be updated</param> /// <returns>True if contact was updated, false otherwise</returns> public bool UpdateContact(Contact contact) { if (contact == null || contact.Id == 0 || !IsContactConsistent(contact)) return false; using (var db = GetPhoneBookContextInstance()) { contact.Owner = Owner; var contactRecordToEdit = db.Contacts.Find(contact.Id); if (!IsRecordOwner(contactRecordToEdit)) return false; contactRecordToEdit.FirstName = contact.FirstName; contactRecordToEdit.LastName = contact.LastName; contactRecordToEdit.Type = contact.Type; contactRecordToEdit.Number = contact.Number; db.SaveChanges(); return true; } }
/// <summary> /// Implementation of IPhoneBookRepository.RemoveContact(contact) which removes contact if it exists in database /// </summary> /// <param name="contact">Existing contact object to be removed</param> /// <returns>True if contact was removed, false otherwise</returns> public bool RemoveContact(Contact contact) { if (contact == null || contact.Id == 0 || !IsContactConsistent(contact)) return false; using (var db = GetPhoneBookContextInstance()) { var contactRecordToRemove = db.Contacts.Find(contact.Id); if (!IsRecordOwner(contactRecordToRemove)) return false; db.Contacts.Remove(contactRecordToRemove); db.SaveChanges(); return true; } }
/// <summary> /// Method checks if contact object is consistent with repository context /// </summary> /// <param name="contact">Contact object</param> /// <returns>True if contact data is consistent, false otherwise</returns> /// <remarks>Contact is consistent if its Owner guid is equal to repository owner guid or /// if its owner property is not set (Guid.Empty) and there will be no database conflict /// if contact was about to be added. Basically this method enforces authorization rules /// allowing user with proper guid to manipulate database records</remarks> public bool IsContactConsistent(Contact contact) { return contact != null && (contact.Owner == Guid.Empty || contact.Owner == Owner); }