Пример #1
0
        public HttpResponseMessage PostReview(ProductHelper.AddProducReviewJson json)
        {
            try
            {
                using (var context = new FSCCContext())
                {
                    var product = context.Products.Find(json.ID);

                    if (product == null)
                    {
                        return(Request.CreateResponse(HttpStatusCode.OK, new { success = true, error = true, message = "There is no product" }));
                    }

                    product.ProductReviews.Add(new ProductReview
                    {
                        ProductRating = json.Review.Rating,
                        ReviewDate    = DateTime.Now,
                        ReviewText    = json.Review.ReviewText
                    });

                    CacheHelper.SetProductCacheExpired();

                    context.SaveChanges();

                    return(Request.CreateResponse(HttpStatusCode.OK, new { success = true, error = false }));
                }
            }
            catch (Exception)
            {
                // Depending on client side how it should handel error, UI side can parse message and show some kind notification
                return(Request.CreateResponse(HttpStatusCode.OK, new { success = false, error = true, message = "Error" }));
            }
        }
Пример #2
0
        public static void Emulate()
        {
            using (var context = new FSCCContext())
            {
                List <Product> products = new List <Product>();
                foreach (var symbol in new[] { "A", "B", "C" })
                {
                    var kindName = $"Tipas {symbol}";
                    if (!context.ProductKinds.Where(_ => _.Name == kindName).Any())
                    {
                        context.ProductKinds.Add(new ProductKind {
                            Name = kindName
                        });
                        context.SaveChanges();
                    }

                    for (int i = 0; i < 3; i++)
                    {
                        var productName = $"Produktas {symbol} {i + 1}";
                        if (!context.Products.Where(_ => _.ProductTitle == productName).Any())
                        {
                            var pr = new Product
                            {
                                ProductTitle  = productName,
                                ProductKindID = context.ProductKinds.Where(_ => _.Name == kindName).FirstOrDefault().ID,
                            };

                            AddReviews(pr);

                            products.Add(pr);
                        }
                    }
                }

                context.Products.AddRange(products);
                context.SaveChanges();
            }
        }