public ValidationDictionary <ViewModelNewsItem> SaveNewsItem(int clientId, ViewModelNewsItem viewModelNews) { var validator = new ValidationDictionary <ViewModelNewsItem>(); if (viewModelNews == null) { validator.IsValid = false; validator.AddError("", "The News you're trying to save is null"); return(validator); } // rule based here viewModelNews.DateExpired = viewModelNews.DateExpired.DatePart(); viewModelNews.DatePublished = viewModelNews.DatePublished.DatePart(); NewsItem newsItem; if (viewModelNews.NewsId <= 0) { newsItem = new NewsItem(); newsItem.InjectFrom <CloneInjection>(viewModelNews); newsItem.DateCreated = DateTime.UtcNow; newsItem.DateLastUpdated = DateTime.UtcNow; newsItem.ClientId = clientId; _newsRepository.Add(newsItem); } else { newsItem = _newsRepository.FindBy(o => !o.IsDeleted && o.NewsId == viewModelNews.NewsId && o.ClientId == clientId); if (newsItem != null) { var createdDate = newsItem.DateCreated; newsItem.InjectFrom <CloneInjection>(viewModelNews); newsItem.DateLastUpdated = DateTime.UtcNow; newsItem.DateCreated = createdDate; newsItem.IsDeleted = false; _newsRepository.Update(newsItem); } } // update news url UpdateNewsUrl(newsItem.NewsId); var pingedNews = GetNewsItem(clientId, newsItem.NewsId); validator.IsValid = true; validator.RelatedObject = pingedNews; return(validator); }
public IActionResult Create(CreateNewsItemVM model) { if (ModelState.IsValid) { var entity = _mapper.Map <NewsItem>(model); entity.Image = new Image(); _imageService.Upload(entity, model.File, EImageType.NewsItem); _newsItemRepository.Add(entity); _unitOfWork.Commit(); return(RedirectToAction("Index")); } return(View(model)); }