public void Update(News item) { if (item == null) { throw new ArgumentNullException(); } var oldItem = context.News.SingleOrDefault(i => i.Id == item.Id); if (oldItem == null) { throw new ArgumentOutOfRangeException("Can't find and update item with id: " + item.Id); } oldItem.Id = item.Id; oldItem.Title = item.Title; oldItem.Description = item.Description; oldItem.EditDate = DateTime.Now; if (!string.IsNullOrWhiteSpace(item.ImagePath)) { bool isEmpty = string.IsNullOrWhiteSpace(oldItem.ImagePath); bool isTheSameImage = string.Compare(oldItem.ImagePath, item.ImagePath) == 0; if (!isEmpty && !isTheSameImage) { ServerFiles.DeleteImageFromLocalFiles(_environment.WebRootPath, oldItem.ImagePath, "news"); } oldItem.ImagePath = item.ImagePath; } context.SaveChanges(); }
public bool Delete(int id) { var item = _context.Services.SingleOrDefault(item => item.Id == id); if (item != null) { if (!string.IsNullOrEmpty(item.ImagePath)) { ServerFiles.DeleteImageFromLocalFiles(_environment.WebRootPath, item.ImagePath, "services"); } _context.Services.Remove(item); _context.SaveChanges(); return(true); } return(false); }
public void Update(Service item) { if (item == null) { throw new ArgumentNullException(); } var oldItem = _context.Services.Include(item => item.ServiceCategory).SingleOrDefault(i => i.Id == item.Id); if (oldItem == null) { throw new ArgumentOutOfRangeException("Can't find and update item with id: " + item.Id); } oldItem.Title = item.Title; oldItem.Price = item.Price; oldItem.Description = item.Description; if (!string.IsNullOrWhiteSpace(item.ImagePath)) { bool isEmpty = string.IsNullOrWhiteSpace(oldItem.ImagePath); bool isTheSameImage = string.Compare(oldItem.ImagePath, item.ImagePath) == 0; if (!isEmpty && !isTheSameImage) { ServerFiles.DeleteImageFromLocalFiles(_environment.WebRootPath, oldItem.ImagePath, "services"); } oldItem.ImagePath = item.ImagePath; } if (item.ServiceCategoryId != 0) { oldItem.ServiceCategoryId = item.ServiceCategoryId; } _context.Services.Update(oldItem); _context.SaveChanges(); }
public bool Delete(int id) { var item = context.Doctors.SingleOrDefault(item => item.Id == id); if (item != null) { var contacts = context.Contacts.SingleOrDefault(i => i.Id == item.ContactsId); if (contacts != null) { context.Contacts.Remove(contacts); } if (!string.IsNullOrEmpty(item.ImagePath)) { ServerFiles.DeleteImageFromLocalFiles(_environment.WebRootPath, item.ImagePath, "doctors"); } context.Doctors.Remove(item); context.SaveChanges(); return(true); } return(false); }
public void Update(Doctor item) { if (item == null) { throw new ArgumentNullException(); } var oldItem = context.Doctors.Include(c => c.Contacts).Include(c => c.DoctorCategory).SingleOrDefault(i => i.Id == item.Id); if (oldItem == null) { throw new ArgumentOutOfRangeException("Can't find and update item with id: " + item.Id); } oldItem.FirstName = item.FirstName; oldItem.LastName = item.LastName; oldItem.MiddleName = item.MiddleName; oldItem.Description = item.Description; if (!string.IsNullOrWhiteSpace(item.ImagePath)) { bool isEmpty = string.IsNullOrWhiteSpace(oldItem.ImagePath); bool isTheSameImage = string.Compare(oldItem.ImagePath, item.ImagePath) == 0; if (!isEmpty && !isTheSameImage) { ServerFiles.DeleteImageFromLocalFiles(_environment.WebRootPath, oldItem.ImagePath, "doctors"); } oldItem.ImagePath = item.ImagePath; } if (item.DoctorCategoryId != 0) { oldItem.DoctorCategoryId = item.DoctorCategoryId; } if (oldItem.Contacts != null && item.Contacts != null) { oldItem.Contacts.Phone = item.Contacts.Phone; oldItem.Contacts.Email = item.Contacts.Email; oldItem.Contacts.Address = item.Contacts.Address; oldItem.Contacts.Country = item.Contacts.Country; oldItem.Contacts.City = item.Contacts.City; oldItem.Contacts.Twitter = item.Contacts.Twitter; oldItem.Contacts.Facebook = item.Contacts.Facebook; oldItem.Contacts.VKontakte = item.Contacts.VKontakte; oldItem.Contacts.LinkedIn = item.Contacts.LinkedIn; oldItem.Contacts.Instagram = item.Contacts.Instagram; oldItem.Contacts.YouTube = item.Contacts.YouTube; oldItem.Contacts.Website = item.Contacts.Website; } else if (item.Contacts != null) { oldItem.ContactsId = item.Contacts.Id; oldItem.Contacts = item.Contacts; context.Contacts.Add(item.Contacts); } context.SaveChanges(); }