예제 #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
        // GET api/Product/GetProductReviews/5
        //[HttpGet]
        public HttpResponseMessage GetProductReviews(long id)
        {
            try
            {
                using (var context = new FSCCContext())
                {
                    var product = CacheHelper.GetProductCache(context).Where(_ => _.ID == id).FirstOrDefault();

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

                    var result = product.ProductReviews.Select(pr => new ProductHelper.ProductReviewJson
                    {
                        Rating     = pr.ProductRating,
                        ReviewDate = pr.ReviewDate,
                        ReviewText = pr.ReviewText
                    })
                                 .ToList();

                    return(Request.CreateResponse(HttpStatusCode.OK, new { success = true, error = false, resutl = result }));
                }
            }
            catch (Exception e)
            {
                // 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" }));
            }
        }
예제 #3
0
        static public Product[] GetProductCache(FSCCContext context)
        {
            string key = "productData";

            return(GetCache(key, () =>
            {
                var p = context.Products
                        .Include("ProductReviews")
                        .Include("ProductKind")
                        .ToArray();
                return p;
            }));
        }
예제 #4
0
 //[HttpGet]
 public HttpResponseMessage GetList(ProductHelper.ProductFilterJson filterJson)
 {
     try
     {
         using (var context = new FSCCContext())
         {
             var products = ProductHelper.FilterProducts(CacheHelper.GetProductCache(context), filterJson).Select(_ => ProductHelper.GetProductJson(_));
             return(Request.CreateResponse(HttpStatusCode.OK, new { success = true, error = false, result = products }));
         }
     }
     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" }));
     }
 }
예제 #5
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();
            }
        }
예제 #6
0
파일: LogFilter.cs 프로젝트: sfomkinas/FSCC
            public override void OnActionExecuting(ActionExecutingContext actionContext)
            {
                HttpRequestBase request = actionContext.RequestContext.HttpContext.Request;

                using (var context = new FSCCContext())
                {
                    var j = context.JournalLogs.Add(new JournalLog()
                    {
                        Date        = DateTime.UtcNow,
                        Cookies     = request.Cookies != null ? string.Join("\r\n", request.Cookies.AllKeys.Select(key => key + "=" + request.Cookies[key].Value)) : string.Empty,
                        Path        = request.Path,
                        QueryString = request.QueryString != null ? string.Join("&", request.QueryString.AllKeys.Select((key, i) => key + "=" + string.Join("; ", request.QueryString[key]))) : string.Empty,
                        RequestType = request.RequestType,
                        UrlReferrer = request.UrlReferrer?.OriginalString,
                        UserAgent   = request.UserAgent,
                        Headers     = request.Headers != null ? string.Join("\r\n", request.Headers.AllKeys.Select(key => key + "=" + string.Join("; ", request.Headers[key]))) : string.Empty,
                    });

                    context.SaveChangesAsync();
                }

                base.OnActionExecuting(actionContext);
            }
예제 #7
0
파일: LogFilter.cs 프로젝트: sfomkinas/FSCC
            public override void OnActionExecuting(HttpActionContext actionContext)
            {
                HttpRequestMessage request = actionContext.Request;

                using (var context = new FSCCContext())
                {
                    var j = context.JournalLogs.Add(new JournalLog()
                    {
                        Date        = DateTime.UtcNow,
                        Cookies     = GetCookies(request),
                        Path        = request.RequestUri.LocalPath,
                        QueryString = request.RequestUri.Query,
                        RequestType = request.Method.ToString(),
                        UrlReferrer = string.Join("\r\n", request.Headers.Where(h => h.Key == "Referer").Select(h => h.Value != null ? string.Join("; ", h.Value.Where(v => !string.IsNullOrEmpty(v))) : string.Empty)),
                        UserAgent   = string.Join("\r\n", request.Headers.Where(h => h.Key == "User-Agent").Select(h => h.Value != null ? string.Join("; ", h.Value.Where(v => !string.IsNullOrEmpty(v))) : string.Empty)),
                        Headers     = string.Join("\r\n", request.Headers.Select(h => h.Key + "=" + h.Value != null ? string.Join("; ", h.Value.Where(v => !string.IsNullOrEmpty(v))) : string.Empty)),
                    });

                    context.SaveChangesAsync();
                }

                base.OnActionExecuting(actionContext);
            }