Пример #1
0
        public RewardsListModel GetSupplierRewards(Guid providerUserKey, int page, int pageSize)
        {
            using (var context = new greenMoneyEntities())
            {

                var rewards = context.Rewards.Where(r => r.Owner_Id == providerUserKey);
                //var uploads = context.Uploads.Where(u => u.UploadedBy_Id == providerUserKey);

                //Include pagination
                var rewardsList = rewards.OrderByDescending(r => r.DateAdded).Skip((page - 1) * pageSize).Take(pageSize);

                List<RewardModel> list = new List<RewardModel>();
                foreach (var reward in rewardsList)
                {
                    RewardModel rewardModel = new RewardModel();
                    Utils.CopyProperties(reward, rewardModel);

                    list.Add(rewardModel);
                }

                RewardsListModel model = new RewardsListModel();
                model.RewardsList = list;
                model.NumRewards = rewards.Count();
                model.Page = page;
                model.NumPages = (int)Math.Ceiling((double)rewards.Count() / pageSize);

                return model;
            }
        }
Пример #2
0
        public RewardsListModel GetRewards(int? instanceId, int? excludeInstanceId, string sortBy, string category, string filter, int page, int pageSize)
        {
            using (var context = new greenMoneyEntities())
            {

                if (pageSize == 0)
                {
                    pageSize = 10;
                }

                //Get all rewards
                var rewards = context.Rewards
                    .Where(r => r.AvailableFrom == null || r.AvailableFrom.Value <= DateTime.Now)
                    .Where(r => r.AvailableTo == null || r.AvailableTo.Value > DateTime.Now)
                    .Where(r => r.Display)
                    .Where(
                        r =>
                            r.State == null || (int)r.State == (int)RewardState.Approved ||
                            (int)r.State == (int)RewardState.NotModerated).ToList();

                if (instanceId != null && instanceId != 0)
                {
                    rewards = rewards.Where(r => r.Instance_Id == instanceId).ToList();
                }
                else if (excludeInstanceId != null)
                {
                    rewards = rewards.Where(r => r.Instance_Id != excludeInstanceId).ToList();
                }

                //Filter all by text query
                if (!string.IsNullOrEmpty(filter))
                    rewards =
                        rewards.Where(
                            r =>
                                r.Name.ToLower().IndexOf(filter.ToLower()) > -1 ||
                                r.Description.ToLower().IndexOf(filter.ToLower()) > -1).ToList();

                List<Rewards> selected = new List<Rewards>();

                //Filter all by selected category
                RewardCategories rewardCategory;
                if (category != null)
                {
                    rewardCategory =
                        context.RewardCategories.FirstOrDefault(
                            c => category.Equals(c.Slug, StringComparison.OrdinalIgnoreCase));
                    if (rewardCategory == null)
                    {
                        throw new Exception("reward category not found");
                    }

                    //get all items, for selected and for subcategories
                    if (rewardCategory.ParentId == 0)
                    {
                        var allAvailableCategories = context.RewardCategories
                            .Where(c => c.Rewards.Any(
                                r => (r.AvailableFrom == null || r.AvailableFrom.Value <= DateTime.Now)
                                     && (r.AvailableTo == null || r.AvailableTo.Value > DateTime.Now)));

                        List<int> subCategoriesIds = allAvailableCategories
                            .Where(c => c.ParentId == rewardCategory.Id).Select(c => c.Id).ToList();

                        subCategoriesIds.Add(rewardCategory.Id);

                        foreach (var subCategoriesId in subCategoriesIds)
                        {
                            selected.AddRange(rewards.Where(r => r.CategoryId == subCategoriesId).ToList());
                        }
                    }
                    else
                    {
                        selected = rewards.Where(r => r.CategoryId == rewardCategory.Id).ToList();
                    }
                }
                else
                {
                    selected = rewards.ToList();
                }

                switch (sortBy)
                {
                    case "newest":
                        selected = selected.OrderByDescending(r => r.AvailableFrom).ThenByDescending(r => r.Id).ToList();
                        break;
                    case "oldest":
                        selected = selected.OrderBy(r => r.AvailableFrom).ThenBy(r => r.Id).ToList();
                        break;
                    case "cheapest":
                        selected = selected.OrderBy(r => r.Price).ToList();
                        break;
                    case "expensive":
                        selected = selected.OrderByDescending(r => r.Price).ToList();
                        break;
                    case "popular":
                        selected = selected.OrderByDescending(r => r.Popularity).ToList();
                        break;
                    default:
                        selected = selected.OrderByDescending(r => r.Popularity).ToList();
                        break;
                }

                //Include pagination
                var list = selected.Skip((page - 1) * pageSize).Take(pageSize);

                List<RewardModel> models = new List<RewardModel>();

                foreach (var item in list)
                {
                    RewardModel m = new RewardModel();
                    Utils.CopyProperties(item, m);

                    models.Add(m);
                }

                RewardsListModel model = new RewardsListModel
                {
                    RewardsList = models,
                    NumPages = (int)Math.Ceiling((double)selected.Count() / pageSize),
                    NumRewards = selected.Count(),
                    Page = page
                };

                return model;
            }
        }