public async Task <IActionResult> CreateNewArticle(ArticlesView av) { byte[] imageData = null; Article artical = new Article { Name = av.Name, CategoryId = av.CategoryId, ShortDescription = av.ShortDescription, Description = av.Description, Date = av.Date }; if (av.Image != null) { using (var binaryReader = new System.IO.BinaryReader(av.Image.OpenReadStream())) { imageData = binaryReader.ReadBytes((int)av.Image.Length); } artical.HeroImage = imageData; } db.Add(artical); // adding artical await db.SaveChangesAsync(); foreach (var tagId in av.Tags) { var tag = db.Tags.Find(tagId); ArticleTag articleTag = new ArticleTag { Article = artical, Tag = tag }; db.Add(articleTag); } await db.SaveChangesAsync(); //adding tags return(RedirectToAction("AdminView")); }
public async Task <ArticlesView> GetBy([FromQuery] string author = "", [FromQuery] string favorited = "", [FromQuery] string tag = "") { IEnumerable <Article> articles = null; ArticlesView articlesView = new ArticlesView(); if (!string.IsNullOrEmpty(author)) { throw new NotImplementedException(); articles = await this._repository.RetrieveByAuthor(author); } else if (!string.IsNullOrEmpty(favorited)) { throw new NotImplementedException(); // articles = await this._repository.RetrieveByFavorited(favorited); } else if (!string.IsNullOrEmpty(tag)) { articles = await this._repository.RetrieveByTag(tag); } articlesView.Articles = articles.Select(x => new ArticleView(x)).ToList(); articlesView.ArticlesCount = articles.Count(); return(articlesView); }
public async Task <ArticlesView> Feed() { var list = await _handler.RetrieveAll(); var articles = new ArticlesView(); // map domain objects to view objects articles.Articles = list.Select(x => new ArticleView(x)).ToList(); articles.ArticlesCount = list.Count(); return(articles); }
public ActionResult Index(int page = 1) { int articlesOnPage = 5; IEnumerable <Article> articles = db.Articles .OrderBy(a => a.Id) .AsEnumerable() // Вызываем как Enumerable для reverse .Reverse() // Меняем полярность массива от нового к старому .Skip((page - 1) * articlesOnPage) .Take(articlesOnPage) .ToList(); PageInfo pageInfo = new PageInfo { CurrentPage = page, CountOfArticles = db.Articles.Count(), MaxArticlesOnPage = articlesOnPage }; ArticlesView view = new ArticlesView { Articles = articles, Page = pageInfo }; return(View(view)); }
private ArticlesView GetListArticlesModel(int page) { using (EFArticleContext articleContext = new EFArticleContext()) { int tp = (Int32)Math.Ceiling((decimal)articleContext.Articles.Count() / PageSize); page = page <0 ? 1 : page> tp ? tp : page; ArticlesView model = new ArticlesView { Articles = articleContext.Articles.Count() == 0 ? articleContext.Articles.ToList() : articleContext.Articles.OrderByDescending(a => a.DatePublish).Skip((page - 1) * PageSize).Take(PageSize).ToList(), PagingInfo = new PagingInfo { CurrentPage = page, ItemPerPage = PageSize, TotalItems = articleContext.Articles.Count() } }; return(model); } }