コード例 #1
0
        public async Task <IActionResult> Get(GetModel model)
        {
            var product = await db.GetProductAsync(model.Id);

            // Set custom values description
            model.Values = ReflectionHelper.GetCharacteristics(product.Description);
            // Set standart (ProductInfo) values description
            model.StandartValues = ReflectionHelper.GetCharacteristics(product.ProductInfo);
            // Set image field
            model.Image = product.ProductInfo.Image;
            // Set can vote field
            if (TempData.ContainsKey("id"))
            {
                model.CanVote = !await db.IsCommentedAsync((int)TempData.Peek("id"), model.Id);
            }
            // Set comments
            var comments = await db.GetCommentsAsync(model.Id);

            bool isLiked = false;

            foreach (var comment in comments)
            {
                var user = await db.GetUserAsync(comment.UserId);

                if (TempData.ContainsKey("id"))
                {
                    isLiked = await db.CanLikeAsync((int)TempData.Peek("id"), comment.Id);
                }
                var vote = await db.GetVoteAsync(model.Id, comment.UserId);

                model.Comments.Add(Tuple.Create(comment, user.Email, isLiked, vote));
            }
            var id  = model.Id;
            var ids = (await db.GetRelationsAsync())
                      .Where(r => r.LesserId == id || r.BiggerId == id)
                      .OrderByDescending(r => r.Count)
                      .Select(r => r.LesserId == id ? r.BiggerId : r.LesserId)
                      .Take(4);
            var products = new List <ProductInfo>();

            foreach (var productId in ids)
            {
                products.Add(await db.GetProductInfoAsync(productId));
            }
            model.RelatedProducts = products;
            return(View(model));
        }