// GET api/<controller>
        public IEnumerable <MarketLocationView> Get(string lang = "en-US")
        {
            var marketLocationViews = _locations.ConvertAll((ml) =>
            {
                return(new MarketLocationView
                {
                    Key = ml.Key,
                    Name = TranslationInfo.ExtractTranslation(ml.Names, lang)
                });
            });

            return(marketLocationViews);
        }
        // GET api/<controller>
        public IEnumerable <ProductConditionView> Get(string lang = "en-US")
        {
            var productConditionViews = _list.ConvertAll((pc) =>
            {
                return(new ProductConditionView
                {
                    Key = pc.Key,
                    Name = TranslationInfo.ExtractTranslation(pc.Names, lang)
                });
            });

            return(productConditionViews);
        }
        // GET api/<controller>
        public IEnumerable <CategoryView> Get(string lang = "en-US")
        {
            using (DatabaseContext context = new DatabaseContext())
            {
                var categories = (from c in context.Categories
                                  select c).ToList();

                var converted = categories.ConvertAll <CategoryView>(c =>
                {
                    return(new CategoryView
                    {
                        Guid = c.Guid,
                        Name = TranslationInfo.ExtractTranslation(c.NameTranslations, lang)
                    });
                });

                return(converted);
            }
        }
        // GET api/<controller>
        public IEnumerable <ProductColourView> Get(Guid productGuid, string lang = "en-US")
        {
            using (DatabaseContext context = new DatabaseContext())
            {
                var results = (from c in context.ProductColours
                               where c.ProductGuid == productGuid
                               select c).ToList();

                var converted = results.ConvertAll <ProductColourView>(c =>
                {
                    return(new ProductColourView
                    {
                        Guid = c.Guid,
                        ProductGuid = c.ProductGuid,
                        Name = TranslationInfo.ExtractTranslation(c.NameTranslations, lang),
                        Value = c.Value
                    });
                });

                return(converted);
            }
        }
Example #5
0
        public IEnumerable <OrderView> Get(string accountGuid = "", string skuId = "", string orderId = "", string type = "", string query = "", string categories = "", string manufacturers = "", string lang = "en-US", bool showExpired = false, bool showSoldOut = false, bool showCancelled = false)
        {
            using (DatabaseContext context = Util.CreateContext())
            {
                var activeAccounts   = new AccountController().GetAccounts().ToList();
                var relevantSkuViews = new SkuViewController().GetList(query, categories, manufacturers, skuId, true, lang).ToList();


                var orders = (from o in context.Orders.AsNoTracking()
                              select o).ToList();

                //only show orders which match sku filters

                var relevantSkuGuids = relevantSkuViews.ConvertAll(s => s.Guid);
                orders = orders.Where(o => relevantSkuGuids.Contains(o.SkuGuid)).ToList();

                //store filter
                if (!String.IsNullOrEmpty(accountGuid))
                {
                    orders = orders.Where(o =>
                    {
                        return(o.CreatedByAccountGuid == new Guid(accountGuid));
                    }).ToList();
                }

                if (!String.IsNullOrEmpty(type))
                {
                    orders = orders.Where(o => o.Type == type).ToList();
                }

                if (!String.IsNullOrEmpty(skuId))
                {
                    orders = orders.Where(o => o.SkuGuid == new Guid(skuId)).ToList();
                }

                if (!String.IsNullOrEmpty(orderId))
                {
                    orders = orders.Where(o => o.Guid == new Guid(orderId)).ToList();
                }

                if (!showExpired)
                {
                    orders = orders.Where(o => o.ValidTo > DateTime.UtcNow).ToList();
                }

                if (!showSoldOut)
                {
                    orders = (from order in orders
                              where activeAccounts.Any(a => a.Guid == order.CreatedByAccountGuid) &&
                              order.Quantity > 0
                              select order).ToList();
                }

                if (!showCancelled)
                {
                    orders = (from order in orders
                              where !order.IsCancelled
                              select order).ToList();
                }

                var webUrl  = ConfigurationManager.AppSettings["WebUrl"];
                var baseUrl = ConfigurationManager.AppSettings["BaseUrl"];

                var marketLocations   = new MarketLocationController().Get(lang).ToList();
                var productConditions = new ProductConditionController().Get(lang).ToList();
                var productColours    = (from c in context.ProductColours.AsNoTracking()
                                         select c).ToList();

                var orderViews = orders.ConvertAll(o =>
                {
                    var sku            = relevantSkuViews.Where(s => s.Guid == o.SkuGuid).FirstOrDefault();
                    var account        = activeAccounts.Where(a => a.Guid == o.CreatedByAccountGuid).FirstOrDefault();
                    var marketLocation = marketLocations.Where(l => l.Key == o.Location).FirstOrDefault();
                    var isActive       = DateTime.UtcNow > o.ValidFrom &&
                                         DateTime.UtcNow < o.ValidTo &&
                                         o.Quantity > 0 &&
                                         !o.IsCancelled;
                    var status    = o.IsCancelled ? "Cancelled" : o.Quantity == 0 ? "Sold out" : DateTime.UtcNow > o.ValidTo ? "Expired" : "Active";
                    var viewCount = (from c in context.ViewCounts
                                     where c.Guid == o.Guid
                                     select c).FirstOrDefault();

                    var productCondition = productConditions.Where(c => c.Key == o.ProductCondition).FirstOrDefault();
                    var productColour    = (from c in productColours
                                            where c.Guid == o.ProductColourGuid
                                            select c).FirstOrDefault();
                    var imageGuids = JsonConvert.DeserializeObject <Guid[]>(o.ImageGuids).ToList();
                    var imageUrls  = imageGuids.ConvertAll(guid => String.Format("{0}/v1/image/{1}", baseUrl, guid));

                    var orderView = new OrderView
                    {
                        Price                = o.Price,
                        Currency             = o.Currency,
                        Quantity             = o.Quantity,
                        QuantityInitial      = o.QuantityInitial,
                        MinimumOrder         = o.MinimumOrder,
                        IsExclusive          = o.IsExclusive,
                        Guid                 = o.Guid,
                        SkuGuid              = o.SkuGuid,
                        CreatedByAccountGuid = o.CreatedByAccountGuid,
                        AccountUserName      = account.Username,
                        AccountPhone         = account.Phone,
                        Location             = marketLocation.Key,
                        LocationName         = marketLocation.Name,
                        ProductColourGuid    = o.ProductColourGuid,
                        ProductColourName    = TranslationInfo.ExtractTranslation(productColour.NameTranslations, lang),
                        ProductColourValue   = productColour.Value,
                        ProductCondition     = o.ProductCondition,
                        ProductConditionName = productCondition.Name,
                        Type                 = o.Type,
                        Created              = DateTime.SpecifyKind(o.Created, DateTimeKind.Utc),
                        ValidFrom            = DateTime.SpecifyKind(o.ValidFrom, DateTimeKind.Utc),
                        ValidTo              = DateTime.SpecifyKind(o.ValidTo, DateTimeKind.Utc),
                        SkuName              = sku.SkuName,
                        SkuFullName          = sku.FullName,
                        CategoryGuid         = sku.CategoryGuid,
                        CategoryName         = sku.CategoryName,
                        ManufacturerGuid     = sku.ManufacturerGuid,
                        ManufacturerName     = sku.ManufacturerName,
                        ProductGuid          = sku.ProductGuid,
                        ProductName          = sku.ProductName,
                        LastModified         = DateTime.SpecifyKind(o.LastModified, DateTimeKind.Utc),
                        ImageSrc             = sku.ImageSrc,
                        Description          = o.Description,
                        IsActive             = isActive,
                        Status               = status,
                        Url       = String.Format("{0}/{1}/order/{2}", webUrl, lang, o.Guid),
                        ViewCount = viewCount != null ? viewCount.Count : 0,
                        ImageUrls = imageUrls.ToArray()
                    };

                    return(orderView);
                });
                return(orderViews);
            }
        }
Example #6
0
        static IEnumerable <SkuView> GenerateAllSkuViews(string lang)
        {
            using (DatabaseContext context = Util.CreateContext())
            {
                var allAccounts          = context.Database.SqlQuery <Account>(@"SELECT * FROM Accounts").ToList();
                var allSkus              = context.Database.SqlQuery <Sku>(@"SELECT * FROM Skus").ToList();
                var allManufacturers     = context.Database.SqlQuery <Manufacturer>(@"SELECT * FROM Manufacturers").ToList();
                var allProducts          = context.Database.SqlQuery <Product>(@"SELECT * FROM Products").ToList();
                var allCategories        = context.Database.SqlQuery <Category>(@"SELECT * FROM Categories").ToList();
                var allProductCategories = context.Database.SqlQuery <ProductCategory>(@"SELECT * FROM ProductCategories").ToList();
                var allOrders            = context.Database.SqlQuery <Order>(@"SELECT * FROM Orders").ToList();
                var allTrades            = context.Database.SqlQuery <Trade>(@"SELECT * FROM Trades").ToList();
                var allViewCounts        = context.ViewCounts.AsNoTracking().ToList();

                var baseUrl = WebConfigurationManager.AppSettings["BaseUrl"];
                var webUrl  = WebConfigurationManager.AppSettings["WebUrl"];

                var skuViews = allSkus.ConvertAll(sku =>
                {
                    var product         = allProducts.Find(p => p.Guid == sku.ProductGuid);
                    var manufacturer    = allManufacturers.Find(m => m.Guid == product.ManufacturerGuid);
                    var productCategory = allProductCategories.Find(pc => pc.ProductGuid == sku.ProductGuid);
                    var category        = allCategories.Find(c => c.Guid == productCategory.CategoryGuid);
                    var viewCount       = allViewCounts.Find(c => c.Guid == sku.Guid);

                    var skuView = new SkuView
                    {
                        CategoryGuid       = category.Guid,
                        CategoryName       = TranslationInfo.ExtractTranslation(category.NameTranslations, lang),
                        ProductGuid        = product.Guid,
                        ProductName        = TranslationInfo.ExtractTranslation(product.NameTranslations, lang),
                        ProductDescription = TranslationInfo.ExtractTranslation(product.DescriptionTranslations, lang),
                        ManufacturerGuid   = manufacturer.Guid,
                        ManufacturerName   = TranslationInfo.ExtractTranslation(manufacturer.NameTranslations, lang),
                        Guid             = sku.Guid,
                        SkuName          = TranslationInfo.ExtractTranslation(sku.NameTranslations, lang),
                        ImageSrc         = String.Format("{0}/v1/productimages/{1}?lang={2}", baseUrl, product.Guid, lang),
                        Url              = String.Format("{0}/{1}/market/{2}", webUrl, lang, sku.Guid),
                        ManufacturerLink = product.ManufacturerLink,
                        ViewCount        = viewCount == null ? 0 : viewCount.Count
                    };

                    string[] fullNameParts = skuView.SkuName == "null" ? new string[] { skuView.ManufacturerName, skuView.ProductName } :
                    new string[] { skuView.ManufacturerName, skuView.ProductName, skuView.SkuName };
                    skuView.FullName = String.Join(" ", fullNameParts);

                    Func <string, string[]> getValues = (nameTranslations) =>
                    {
                        var translations = JsonConvert.DeserializeObject <TranslationInfo[]>(nameTranslations);
                        return((from t in translations select t.Text == "null" ? "" : t.Text).ToArray());
                    };

                    List <string> keywordParts = new List <string>();
                    keywordParts.AddRange(getValues(category.NameTranslations));
                    keywordParts.AddRange(getValues(manufacturer.NameTranslations));
                    keywordParts.AddRange(getValues(product.NameTranslations));
                    keywordParts.AddRange(getValues(sku.NameTranslations));
                    skuView.Keywords = String.Join(" ", keywordParts.Distinct());

                    ////build price low only for active orders
                    var results = (from o in allOrders
                                   where o.SkuGuid == sku.Guid &&
                                   !o.IsCancelled &&
                                   o.Quantity > 0 &&
                                   o.ValidFrom <DateTime.UtcNow &&
                                                o.ValidTo> DateTime.UtcNow
                                   select o).ToList();

                    skuView.OrdersCount = results.Count;

                    //only summarise sell order pricing
                    var sellOrders = results.Where(o => o.Type == "Sell").ToList();

                    if (sellOrders.Count > 0)
                    {
                        var cheapest        = sellOrders.OrderBy(o => o.Price).First();
                        skuView.PriceLow    = cheapest.Price;
                        skuView.IsExclusive = cheapest.IsExclusive;
                        skuView.Currency    = cheapest.Currency;

                        var latest = (from order in results
                                      orderby order.LastModified descending
                                      select order).First();
                        skuView.LastModified = latest.LastModified;
                    }

                    skuView.SoldCount = (from t in allTrades
                                         where t.SkuGuid == sku.Guid
                                         select t.Quantity).Sum();

                    return(skuView);
                });

                return(skuViews);
            }
        }