private static string BuildFullArticlePageHtml(FullArticleModel model) { var pageTemplate = File.ReadAllText("./$Content/Templates/FullArticle.hbs"); var pageHtml = pageTemplate .Replace("{{header}}", model.Article.Header) .Replace("{{date}}", model.Article.Date.ToString("d MMM yyyy", culture)) .Replace("{{content}}", model.Article.ContentHtml); return(pageHtml); }
public IHttpActionResult Get(string urlName) { Guid?currentUserId = null; if (this.User.Identity.IsAuthenticated) { currentUserId = new Guid(this.User.Identity.GetUserId()); } ArticlesProvider articlesProvider = new ArticlesProvider(); FullArticleModel articleModel = articlesProvider.GetFullArticleByUrlName(urlName, currentUserId); if (articleModel == null) { return(this.NotFound()); } return(this.Ok(articleModel)); }
public FullArticleModel GetFullArticleByUrlName(string urlName, Guid?currentUserId) { FullArticleModel articleModel = null; using (PoshBoutiqueData dataContext = new PoshBoutiqueData()) { string l = string.Empty; dataContext.Database.Log = s => l += s; Article article = dataContext.Articles .Include(a => a.ArticleImages) .Include(a => a.Stocks) .Include(a => a.Stocks.Select(s => s.Color)) .Include(a => a.Stocks.Select(s => s.Size)) .FirstOrDefault(a => a.UrlName == urlName); articleModel = new FullArticleModel() { Id = article.Id, Title = article.Title, UrlName = article.UrlName, Description = article.Description, MaterialDescription = article.MaterialDescription, Price = article.Price, OriginalPrice = article.OriginalPrice, HasDiscount = article.OriginalPrice != null, DiscountDescription = article.DiscountDescription, ThumbnailUrl = article.ThumbnailUrl, ShortDescription = article.ShortDescription }; if (currentUserId == null) { articleModel.IsLiked = false; } else { articleModel.IsLiked = dataContext.UserLikes.Any(like => like.ArticleId == article.Id && like.UserId == currentUserId.Value); } Dictionary <int, SizeModel> sizesDictionary = article.Stocks.Select(s => s.Size).Distinct().ToDictionary(s => s.Id, s => new SizeModel() { Id = s.Id, Name = s.Name, OrderIndex = s.OrderIndex, Quantity = 0 }); foreach (Stock stock in article.Stocks) { SizeModel sizeModel = sizesDictionary[stock.SizeId]; if (stock.ColorId == 0) { sizeModel.AddColor(null, stock.Quantity); break; } else { sizeModel.AddColor(stock.Color, stock.Quantity); } } articleModel.Sizes = sizesDictionary.Values.ToList().OrderBy(s => s.OrderIndex); articleModel.Images = article.ArticleImages.OrderBy(i => i.OrderIndex).Select(i => new ImageModel() { SmallUrl = i.SmallImageUrl, MediumUrl = i.MediumImageUrl, LargeUrl = i.LargelImageUrl }).ToList(); } return(articleModel); }