Exemplo n.º 1
0
        public IHttpActionResult GetTrainerLibraryFilterChallengeList(TrainerLibraryChallengeFilterParam model)
        {
            StringBuilder traceLog = null;
            ServiceResponse <List <MainChallengeVM> > objResponse = null;

            try
            {
                traceLog = new StringBuilder();
                traceLog.AppendLine("Start: GetTrainerLibraryFilterChallengeList with Request data-Type" + model.Type + ",BodyZone" + model.BodyZone + ",Difficulty" + model.Difficulty + ",Equipment" + model.Equipment + ",ExerciseType" + model.ExerciseType);
                objResponse              = new ServiceResponse <List <MainChallengeVM> >();
                objResponse.jsonData     = ProgramApiBL.GetTrainerLibraryFilterChallengeList(model);
                objResponse.IsResultTrue = true;
                return(Ok(objResponse));
            }
            catch (Exception ex)
            {
                LogManager.LogManagerInstance.WriteErrorLog(ex);
                return(BadRequest(ex.Message));
            }
            finally
            {
                traceLog.AppendLine("End:GetTrainerLibraryFilterChallengeList() Response with Result Status-" + objResponse.IsResultTrue + ",FetchDatatime" + DateTime.Now.ToLongDateString());
                LogManager.LogManagerInstance.WriteTraceLog(traceLog);
                traceLog    = null;
                objResponse = null;
            }
        }
        /// <summary>
        ///  Get Trainer Library Filter Challenge List
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static List <MainChallengeVM> GetTrainerLibraryFilterChallengeList(TrainerLibraryChallengeFilterParam model)
        {
            StringBuilder traceLog = new StringBuilder();

            using (LinksMediaContext dataContext = new LinksMediaContext())
            {
                try
                {
                    traceLog.AppendLine("Start: GetFilterChallengeList---- " + DateTime.Now.ToLongDateString());
                    int trainerCredId = -1;
                    if (!string.IsNullOrEmpty(model.UserType) && model.UserType.Equals(Message.UserTypeTrainer, StringComparison.OrdinalIgnoreCase))
                    {
                        trainerCredId = (from tr in dataContext.Trainer
                                         join crd in dataContext.Credentials
                                         on tr.TrainerId equals crd.UserId
                                         where crd.UserType == Message.UserTypeTrainer && tr.TrainerId == model.UserId
                                         select crd.Id).FirstOrDefault();
                    }
                    if (trainerCredId > 0)
                    {
                        // Manipulate value in model(ChallengeFilterParam) object according to request parameteres.
                        //We have modify the Type/Difficulty/Equipment/Bodyzone properties
                        model.Type         = !string.IsNullOrEmpty(model.Type) ? (model.Type.Equals(Message.All) ? null : model.Type) : null;
                        model.Difficulty   = !string.IsNullOrEmpty(model.Difficulty) ? (model.Difficulty.Equals(Message.All) ? null : model.Difficulty) : null;
                        model.Equipment    = !string.IsNullOrEmpty(model.Equipment) ? (model.Equipment.Equals(Message.All) ? null : model.Equipment) : null;
                        model.ExerciseType = !string.IsNullOrEmpty(model.ExerciseType) ? (model.ExerciseType.Equals(Message.All) ? null : model.ExerciseType) : null;
                        model.BodyZone     = !string.IsNullOrEmpty(model.BodyZone) ? (model.BodyZone.Equals(Message.All) ? null : model.BodyZone) : null;
                        DateTime today = DateTime.Now.Date;

                        //Get All ChallengeId which is currently belong to Challenge of the day
                        List <int> exceptListCOD = (from cod in dataContext.ChallengeofTheDayQueue
                                                    join c in dataContext.Challenge on cod.ChallengeId equals c.ChallengeId
                                                    where c.IsActive && cod.StartDate <= today && cod.EndDate >= today
                                                    select c.ChallengeId).ToList();

                        //Get All ChallengeId which is currently belong to Sponsor Challenge.
                        List <int> exceptListSponsor = (from tc in dataContext.TrainerChallenge
                                                        join c in dataContext.Challenge on tc.ChallengeId equals c.ChallengeId
                                                        where c.IsActive && tc.StartDate <= today && tc.EndDate >= today
                                                        select c.ChallengeId).ToList();

                        //Get All Filter Challenge list exclude Challenge Of the Day(exceptListCOD) and Sponsor Challenge(exceptListSponsor)
                        List <MainChallengeVM> listMainVM = null;
                        string challengeType = ConstantHelper.FreeFormChallengeType;
                        if (!string.IsNullOrEmpty(model.ExerciseType))
                        {
                            listMainVM = (from c in dataContext.Challenge
                                          join ct in dataContext.ChallengeType on c.ChallengeSubTypeId equals ct.ChallengeSubTypeId
                                          join etc in dataContext.ETCAssociations on c.ChallengeId equals etc.ChallengeId
                                          join etype in dataContext.ExerciseTypes on etc.ExerciseTypeId equals etype.ExerciseTypeId
                                          where !exceptListCOD.Contains(c.ChallengeId) &&
                                          !exceptListSponsor.Contains(c.ChallengeId) &&
                                          c.IsActive &&
                                          (model.Difficulty == null || c.DifficultyLevel == model.Difficulty) &&
                                          (model.BodyZone == null || (from trzone in dataContext.TrainingZoneCAssociations
                                                                      join bp in dataContext.BodyPart
                                                                      on trzone.PartId equals bp.PartId
                                                                      where trzone.ChallengeId == c.ChallengeId
                                                                      select bp.PartName).ToList <string>().Contains(model.BodyZone)) &&
                                          (model.Equipment == null || (from trzone in dataContext.ChallengeEquipmentAssociations
                                                                       join bp in dataContext.Equipments
                                                                       on trzone.EquipmentId equals bp.EquipmentId
                                                                       where trzone.ChallengeId == c.ChallengeId
                                                                       select bp.Equipment).ToList <string>().Contains(model.Equipment)) &&
                                          (model.Type == null || ct.ChallengeType.Contains(model.Type)) &&
                                          (model.ExerciseType == null || etype.ExerciseName.Contains(model.ExerciseType)) &&
                                          (c.TrainerId == trainerCredId) &&
                                          ct.ChallengeType != challengeType &&
                                          ct.ChallengeType != ConstantHelper.ProgramChallengeType
                                          orderby c.CreatedDate descending
                                          select new MainChallengeVM
                            {
                                ChallengeId = c.ChallengeId,
                                ChallengeName = c.ChallengeName,
                                DifficultyLevel = c.DifficultyLevel,
                                ChallengeType = ct.ChallengeType,
                                IsSubscription = c.IsSubscription,
                                TempEquipments = (from trzone in dataContext.ChallengeEquipmentAssociations
                                                  join bp in dataContext.Equipments
                                                  on trzone.EquipmentId equals bp.EquipmentId
                                                  where trzone.ChallengeId == c.ChallengeId
                                                  select bp.Equipment).Distinct().ToList <string>(),
                                TempTargetZone = (from trzone in dataContext.TrainingZoneCAssociations
                                                  join bp in dataContext.BodyPart
                                                  on trzone.PartId equals bp.PartId
                                                  where trzone.ChallengeId == c.ChallengeId
                                                  select bp.PartName).Distinct().ToList <string>(),
                                Strenght = dataContext.UserChallenge.Where(uc => uc.ChallengeId == c.ChallengeId).Select(y => y.UserId).Distinct().Count(),
                                ResultUnit = ct.ResultUnit
                            }).ToList();
                        }
                        else
                        {
                            listMainVM = (from c in dataContext.Challenge
                                          join ct in dataContext.ChallengeType on c.ChallengeSubTypeId equals ct.ChallengeSubTypeId
                                          where !exceptListCOD.Contains(c.ChallengeId) &&
                                          !exceptListSponsor.Contains(c.ChallengeId) &&
                                          c.IsActive == true && c.IsPremium &&
                                          (model.Difficulty == null || c.DifficultyLevel == model.Difficulty) &&
                                          (model.BodyZone == null || (from trzone in dataContext.TrainingZoneCAssociations
                                                                      join bp in dataContext.BodyPart
                                                                      on trzone.PartId equals bp.PartId
                                                                      where trzone.ChallengeId == c.ChallengeId
                                                                      select bp.PartName).ToList <string>().Contains(model.BodyZone)) &&
                                          (model.Equipment == null || (from trzone in dataContext.ChallengeEquipmentAssociations
                                                                       join bp in dataContext.Equipments
                                                                       on trzone.EquipmentId equals bp.EquipmentId
                                                                       where trzone.ChallengeId == c.ChallengeId
                                                                       select bp.Equipment).ToList <string>().Contains(model.Equipment)) &&
                                          (model.Type == null || ct.ChallengeType.Contains(model.Type)) &&
                                          (c.TrainerId == trainerCredId) &&
                                          ct.ChallengeType != challengeType &&
                                          ct.ChallengeType != ConstantHelper.ProgramChallengeType
                                          orderby c.CreatedDate descending
                                          select new MainChallengeVM
                            {
                                ChallengeId = c.ChallengeId,
                                ChallengeName = c.ChallengeName,
                                DifficultyLevel = c.DifficultyLevel,
                                ChallengeType = ct.ChallengeType,
                                IsSubscription = c.IsSubscription,
                                TempEquipments = (from trzone in dataContext.ChallengeEquipmentAssociations
                                                  join bp in dataContext.Equipments
                                                  on trzone.EquipmentId equals bp.EquipmentId
                                                  where trzone.ChallengeId == c.ChallengeId
                                                  select bp.Equipment).Distinct().ToList <string>(),
                                TempTargetZone = (from trzone in dataContext.TrainingZoneCAssociations
                                                  join bp in dataContext.BodyPart
                                                  on trzone.PartId equals bp.PartId
                                                  where trzone.ChallengeId == c.ChallengeId
                                                  select bp.PartName).Distinct().ToList <string>(),
                                Strenght = dataContext.UserChallenge.Where(uc => uc.ChallengeId == c.ChallengeId).Select(y => y.UserId).Distinct().Count(),
                                ResultUnit = ct.ResultUnit
                            }).ToList();
                        }
                        listMainVM.ForEach(r =>
                        {
                            r.ChallengeType = r.ChallengeType.Split(' ')[0];
                            if (r.TempTargetZone != null && r.TempTargetZone.Count > 0)
                            {
                                r.TargetZone = string.Join(", ", r.TempTargetZone);
                            }
                            r.TempTargetZone = null;
                            if (r.TempEquipments != null && r.TempEquipments.Count > 0)
                            {
                                r.Equipment = string.Join(", ", r.TempEquipments);
                            }
                            r.TempEquipments = null;
                        });
                        if (listMainVM != null)
                        {
                            listMainVM = listMainVM.OrderByDescending(item => item.Strenght).ToList();
                        }
                        return(listMainVM);
                    }
                    return(null);
                }
                catch
                {
                    throw;
                }
                finally
                {
                    traceLog.AppendLine("End  GetFilterChallengeList : --- " + DateTime.Now.ToLongDateString());
                    LogManager.LogManagerInstance.WriteTraceLog(traceLog);
                }
            }
        }