public Model.Contact AddContact(Model.Contact contact) { Validate("firstName", contact.FirstName); Validate("lastName", contact.LastName); Validate("phoneNumber", contact.PhoneNumber); Validate("emailId", contact.EmailId); try { using (BlahEntities entity = new BlahEntities()) { Contact contactToAdd = new Contact() { EmailId = contact.EmailId, FirstName = contact.FirstName, LastName = contact.LastName, PhoneNumber = contact.PhoneNumber, Status = (bool)contact.Status, }; Contact result = entity.Contacts.Add(contactToAdd); int added = entity.SaveChanges(); if (added <= 0) { throw new ApplicationException("Record Not updated"); } } } catch (Exception exception) { throw new ApplicationException(exception.Message); } return(contact); }
public Model.Contact EditContact(string emailId, Model.Contact contact) { Validate("emailId", emailId); Validate("emailId", contact.EmailId); try { using (BlahEntities entity = new BlahEntities()) { Contact item = entity.Contacts.FirstOrDefault(x => x.EmailId.Equals(emailId, System.StringComparison.OrdinalIgnoreCase)); if (item != null) { item.FirstName = contact.FirstName; item.LastName = contact.LastName; item.PhoneNumber = contact.PhoneNumber; item.Status = (bool)contact.Status; } int k = entity.SaveChanges(); if (k <= 0) { throw new ApplicationException("Record Not updated"); } } contact.EmailId = emailId; } catch (Exception exception) { throw new ApplicationException(exception.Message); } return(contact); }
public Model.Contact GetContact(string emailId) { Validate("emailId", emailId); Model.Contact contact = null; using (BlahEntities entity = new BlahEntities()) { Contact item = null; try { item = entity.Contacts.FirstOrDefault(x => x.EmailId.ToLower() == emailId); } catch (Exception exception) { throw new ApplicationException(exception.Message); } if (item != null) { contact = new Model.Contact() { EmailId = item.EmailId, FirstName = item.FirstName, LastName = item.LastName, PhoneNumber = item.PhoneNumber, Status = (bool)item.Status }; } else { throw new ArgumentException("no data found for" + emailId); } } return(contact); }
public void RemoveContact(string emailId) { Validate("emailId", emailId); try { using (BlahEntities entity = new BlahEntities()) { Contact item = entity.Contacts.FirstOrDefault(x => x.EmailId.Equals(emailId, System.StringComparison.OrdinalIgnoreCase)); if (item != null) { entity.Contacts.Remove(item); } } } catch (Exception exception) { throw new ApplicationException(exception.Message); } }
public List <Model.Contact> GetContacts(int page, int pageSize, out int total) { if (page < 0) { page = 1; } if (pageSize < 0 || pageSize > 10) { pageSize = 10; } List <Model.Contact> contacts = new List <Model.Contact>(); try { using (BlahEntities entity = new BlahEntities()) { total = entity.Contacts.Count(); var items = entity.Contacts.OrderBy(x => x.EmailId).Skip((page - 1) * pageSize).Take(pageSize); foreach (var item in items) { contacts.Add(new Model.Contact() { EmailId = item.EmailId, FirstName = item.FirstName, LastName = item.LastName, PhoneNumber = item.PhoneNumber, Status = (bool)item.Status }); } } } catch (Exception exception) { throw new ApplicationException(exception.Message); } return(contacts); }