public DataSourceResult Save(BlindTypesModel viewModel, ModelStateDictionary modelState) { if (viewModel != null && modelState.IsValid) { var repo = this.RepoFactory.Get<BlindTypeRepository>(); var entity = repo.GetById(viewModel.Id); var exists = repo.GetIfExists(viewModel.Name, viewModel.Id); if (exists) { return new DataSourceResult { Errors = GlobalConstants.BlindTypeExistsMessage }; } if (entity == null) { entity = new BlindType(); repo.Add(entity); } Mapper.Map(viewModel, entity); if (viewModel.File != null) { int fileSizeInBytes = viewModel.File.ContentLength; MemoryStream target = new MemoryStream(); viewModel.File.InputStream.CopyTo(target); entity.Content = target.ToArray(); } try { repo.SaveChanges(); viewModel.Id = entity.Id; viewModel.File = null; return null; } catch (DbEntityValidationException e) { return new DataSourceResult { Errors = this.HandleDbEntityValidationException(e) }; } } else { return this.HandleErrors(modelState); } }
public DataSourceResult Delete(BlindTypesModel viewModel, ModelStateDictionary modelState) { if (viewModel != null && modelState.IsValid) { var repo = this.RepoFactory.Get<BlindTypeRepository>(); var entity = repo.GetById(viewModel.Id); entity.Deleted = true; entity.DeletedOn = DateTime.Now; repo.SaveChanges(); return null; } else { return this.HandleErrors(modelState); } }