public ChallengesListModel GetChallenges(Guid providerUserKey, int page, int pageSize, int orderBy, int? status, int? category, string search)
        {
            using (var context = new greenMoneyEntities())
            {
                var user = new UserRepository().GetUserById(providerUserKey);
                ChallengesListModel model = new ChallengesListModel();

                var challenges = context.Challenges.Where(r => r.Instance_Id == user.Instance_Id)
                                                    .Where(r => (category == null || category == 0) || r.ChallengeCategoryId == category)
                                                    .Where(r => search == null || r.Name.Contains(search) || r.About.Contains(search));

                DateTime now = DateTime.Now.Date;

                DateTime inWeek = DateTime.Now.Date.AddDays(7);

                switch (status)
                {
                    case 1: challenges = challenges.Where(x => x.Display == false);
                        break;
                    case 2: challenges = challenges.Where(x => (x.Display == true) && (x.StartDate <= now || x.StartDate == null) && (x.EndDate >= now || x.EndDate == null));
                        break;
                    case 3: challenges = challenges.Where(x => (x.Display == false) || (x.StartDate > now) || (x.EndDate < now));
                        break;
                    case 4: challenges = challenges.Where(x => x.EndDate > now && x.EndDate <= inWeek);
                        break;

                }

                model.NumChallenges = challenges.Count();
                model.Page = page;
                model.NumPages = (int)Math.Ceiling((double)challenges.Count() / pageSize);

                if (pageSize == 0)
                    pageSize = model.NumChallenges;

                List<ChallengeModel> list = new List<ChallengeModel>();
                //IEnumerable<ChallengeModel> list = new IEnumerable<ChallengeModel>();
                foreach (var challenge in challenges)
                {
                    var challengeModel = new ChallengeModel();
                    Utils.CopyProperties(challenge, challengeModel);
                    challengeModel.Participants = challenge.UserChallenges.Count();
                    //  challengeModel.AustraliaState = challenge.State;
                    if (challenge.ChallengeCategoryId != null)
                    {
                        challengeModel.ChallengeCategory = challenge.ChallengeCategories.Name;
                    }
                    else
                    {
                        challengeModel.ChallengeCategory = "";
                    }

                    if ((challenge.StartDate <= DateTime.Now || challenge.StartDate == null) && (challenge.EndDate >= now || challenge.EndDate == null) && (challenge.Display == true))
                    {
                        challengeModel.ChallengeStatus = "Active";
                    }
                    else
                    {
                        challengeModel.ChallengeStatus = "Not active";
                    }

                    list.Add(challengeModel);
                }

                switch (orderBy)
                {
                    case 0: list = list.OrderByDescending(x => x.DateAdded).ToList();
                        break;
                    case 1: list = list.OrderBy(x => x.DateAdded).ToList();
                        break;
                    case 2: list = list.OrderByDescending(x => x.Participants).ToList();
                        break;
                    case 3: list = list.OrderBy(x => x.EndDate).ToList();
                        break;
                    case 4: list = list.OrderByDescending(x => x.Popularity).ToList();
                        break;

                }

                model.ChallengesList = list.AsEnumerable<ChallengeModel>().Skip((page - 1) * pageSize).Take(pageSize).ToList();

                return model;
            }
        }
Esempio n. 2
0
        public List<string> GetAllUsers()
        {
            //var users = GM.DataAccess.Entities.Users.
            var test = new UserRepository().Users();

            return new List<string>();
        }