public static EditNewsModel CopyFrom(this EditNewsModel model, NewsData data) { model.Id = data.Id; model.Title = data.Title; model.Description = data.Description; model.WriterName = data.WriterName; model.NewsCategoryId = data.NewsCategory.Id; model.PlatformIds = data.PlatformIds.ToList(); model.HiddenPlatformIds = string.Join(",", data.PlatformIds); model.ImagePath = data.ImagePath; model.Annotation = data.Annotation; model.BlockComments = data.BlockComments; return model; }
public static ViewNewsModel CopyFrom(this ViewNewsModel model, NewsData data) { model.Id = data.Id; model.Title = data.Title; model.Description = data.Description; model.WriterId = data.WriterId; model.WriterName = data.WriterName; model.NewsCategoryId = data.NewsCategory.Id; model.Platforms = data.Platforms; model.ImagePath = data.ImagePath; model.CreateDate = data.CreateDate; model.CountViews = ++data.CountViews; model.NewsCategory = data.NewsCategory; model.CountComments = data.CountComments; model.BlockComments = data.BlockComments; return model; }
private void FillViewNewsModel(ViewNewsModel model, NewsData data) { model.PreviousNews = Execute(() => _newsService.GetPreviousNews(model.Id.GetValueOrDefault(), Constants.PreviousNewsCount)) ?? new List<NewsData>(); model.CopyFrom(data); }
public ActionResult EditNews(EditNewsModel model) { if (ModelIsValid) { model.PlatformIds.Clear(); if (!string.IsNullOrWhiteSpace(model.HiddenPlatformIds)) { var platformIds = model.HiddenPlatformIds.Split(','); foreach (var platformId in platformIds) { int id; if (int.TryParse(platformId, out id)) model.PlatformIds.Add(id); } } ValidateUploadFile(model); if (ModelIsValid) { var data = new NewsData { Id = model.Id.GetValueOrDefault(), Description = model.Description, Title = model.Title, NewsCategory = new NewsCategoryData { Id = model.NewsCategoryId }, WriterId = CurrentUser.Id, PlatformIds = model.PlatformIds, ImagePath = model.ImagePath, Annotation = model.Annotation, BlockComments = model.BlockComments }; Execute(() => _newsService.SaveNews(data)); if (ModelIsValid) { return data.Id == default(int) ? RedirectToAction<NewsController>(o => o.News()) : RedirectToAction<NewsController>(o => o.ViewNews(data.Id)); } } } FillResourceEditNewsModel(model); return View(model); }
void INewsService.SaveNews(NewsData data) { var entity = data.Id != default(int) ? _newsRepository.Query(o => o.Id == data.Id).SingleOrDefault() : new News(); if (entity == null) throw new ArgumentException("Данной новости не существует."); var newsCategory = _newsCategoryRepository.Query(o => o.Id == data.NewsCategory.Id).SingleOrDefault(); if (newsCategory == null) throw new ArgumentException("Данной категории не существует."); entity.Title = data.Title; entity.Description = data.Description; entity.NewsCategory = newsCategory; entity.ImagePath = data.ImagePath; entity.Annotation = data.Annotation; entity.BlockComments = data.BlockComments; if (data.Id == default(int)) { var writer = _userRepository.Query(o => o.Id == data.WriterId).SingleOrDefault(); if (writer == null) throw new ArgumentException("Данного пользователя не существует."); entity.CreateDate = DateTime.UtcNow; entity.Writer = writer; if (data.PlatformIds.Any()) { var validePlatforms = _platformRepository.Query(o => data.PlatformIds.Contains(o.Id)).ToList(); foreach (var platform in validePlatforms) { _newsPlatformRepository.Add(new NewsPlatform { News = entity, Platform = platform }); } } _newsRepository.Add(entity); } else { if (data.PlatformIds.Any()) { var validePlatforms = _platformRepository.Query(o => data.PlatformIds.Contains(o.Id)).ToList(); var validePlatformIds = validePlatforms.Select(o => o.Id).ToArray(); var platforms = entity.NewsPlatforms.Select(o => o.Platform).ToList(); var platformIds = platforms.Select(o => o.Id).ToArray(); var newPlatforms = validePlatforms.Where(o => !platformIds.Contains(o.Id)).ToList(); var deletePlatforms = entity.NewsPlatforms.Where(o => !validePlatformIds.Contains(o.Platform.Id)).ToList(); foreach (var newPlatform in newPlatforms) { _newsPlatformRepository.Add(new NewsPlatform { News = entity, Platform = newPlatform }); } foreach (var deletePlatform in deletePlatforms) { _newsPlatformRepository.Delete(deletePlatform); } } } UnitOfWork.Commit(); }