/// <summary> /// Delete /// </summary> /// <param name="model">Home category model</param> /// <returns>ResponseStatusCodeHelper</returns> public ResponseStatusCodeHelper Delete(HomeCategoryModel model) { try { using (var _context = new TDHEntities()) { WEB_HOME_CATEGORY _md = _context.WEB_HOME_CATEGORY.FirstOrDefault(m => m.category_id == model.CategoryID); if (_md == null) { throw new DataAccessException(FILE_NAME, "Delete", model.CreateBy); } _context.WEB_HOME_CATEGORY.Remove(_md); _context.Entry(_md).State = EntityState.Deleted; _context.SaveChanges(); } } catch (DataAccessException fieldEx) { throw fieldEx; } catch (Exception ex) { throw new ServiceException(FILE_NAME, "Delete", model.CreateBy, ex); } Notifier.Notification(model.CreateBy, Message.DeleteSuccess, Notifier.TYPE.Success); return(ResponseStatusCodeHelper.Success); }
/// <summary> /// Save /// </summary> /// <param name="model">Home category model</param> /// <returns>ResponseStatusCodeHelper</returns> public ResponseStatusCodeHelper Save(HomeCategoryModel model) { try { using (var _context = new TDHEntities()) { WEB_HOME_CATEGORY _md = _md = _context.WEB_HOME_CATEGORY.FirstOrDefault(m => m.category_id == model.CategoryID); if (!model.Selected) { return(Delete(model)); } if (_md == null) { _md = new WEB_HOME_CATEGORY() { id = Guid.NewGuid(), category_id = model.CategoryID, ordering = 1 }; _context.WEB_HOME_CATEGORY.Add(_md); _context.Entry(_md).State = EntityState.Added; } else { _md.ordering = model.Ordering; _context.WEB_HOME_CATEGORY.Attach(_md); _context.Entry(_md).State = EntityState.Modified; } _context.SaveChanges(); } } catch (Exception ex) { throw new ServiceException(FILE_NAME, "Save", model.CreateBy, ex); } Notifier.Notification(model.CreateBy, Message.UpdateSuccess, Notifier.TYPE.Success); return(ResponseStatusCodeHelper.Success); }