protected override void ExecuteRequest(HttpContext context)
        {
            (bool isOk, User user) = CheckClaimsForUser(Request, context, _userRepository);
            if (!isOk || user == null)
            {
                return;
            }

            IEnumerable <Quest> quests = _questRepository.Get(user, _questTypeRepository, QuestState.InProgress).GetAwaiter().GetResult();

            foreach (Quest quest in quests)
            {
                QuestHandler questHandler = QuestHandlers.Create(quest, user, _questTypeRepository);
                if (questHandler != null)
                {
                    switch (questHandler.Update(Request))
                    {
                    case QuestState.InProgress:
                        break;

                    case QuestState.Failed:
                        break;

                    case QuestState.Finished:
                        break;

                    default:
                        break;
                    }
                    _questRepository.Update(quest).GetAwaiter().GetResult();
                }
            }
            context.Response.StatusCode = StatusCodes.Status200OK;
        }
Beispiel #2
0
        protected override void ExecuteRequest(HttpContext context)
        {
            (bool isOk, User user) = CheckClaimsForUser(Request, context, _userRepository);
            if (!isOk || user == null)
            {
                return;
            }

            OpenFoodFactsData offData = _oFFDataRepository.Insert(new OpenFoodFactsData()
            {
                Name     = Request.Name,
                IsCustom = true,
                // 100g
                Energy100g       = Request.Energy100g,
                Proteins100g     = Request.Proteins100g,
                Sodium100g       = Request.Sodium100g,
                Fat100g          = Request.Fat100g,
                Salt100g         = Request.Salt100g,
                SaturatedFat100g = Request.SaturatedFat100g,
                Sugars100g       = Request.Sugars100g,
                // Serving
                EnergyServing       = Request.EnergyServing,
                ProteinsServing     = Request.ProteinsServing,
                SodiumServing       = Request.SodiumServing,
                FatServing          = Request.FatServing,
                SaltServing         = Request.SaltServing,
                SaturatedFatServing = Request.SaturatedFatServing,
                SugarsServing       = Request.SugarsServing,
            }).GetAwaiter().GetResult();

            FoodEntry foodEntryData = _foodEntryRepository.Insert(new FoodEntry()
            {
                Datetime            = DateTime.Now,
                UserId              = user.Id,
                OpenFoodFactsDataId = offData.Id
            }).GetAwaiter().GetResult();

            if (foodEntryData != null)
            {
                // update all quests based on datas
                var quests = _questRepository.Get(user, _questTypeRepository, QuestState.InProgress).GetAwaiter().GetResult();
                foreach (var quest in quests)
                {
                    var questHandler = QuestHandlers.Create(quest, user, _questTypeRepository);
                    questHandler.Update("Calories", (foodEntryData.Servings * offData.EnergyServing).ToString());
                    _questRepository.Update(quest).GetAwaiter().GetResult();
                }

                string foodEntryJson = JsonSerializer.Serialize(foodEntryData);
                context.Response.StatusCode = StatusCodes.Status200OK;
                context.Response.WriteAsync(foodEntryJson).GetAwaiter().GetResult();
            }
            else
            {
                context.Response.StatusCode = StatusCodes.Status204NoContent;
            }
        }
        protected override void ExecuteRequest(HttpContext context)
        {
            (bool isOk, User user) = CheckClaimsForUser(Request, context, _userRepository);
            if (!isOk || user == null)
            {
                return;
            }

            Quest                 quest        = _questRepository.GetById(user, Request.QuestId).GetAwaiter().GetResult();
            QuestHandler          questHandler = QuestHandlers.Create(quest, user, _questTypeRepository);
            ClaimQuestDTOResponse claimQuestDTOResponse;
            string                serializedString;

            if (questHandler != null)
            {
                switch (questHandler.GetState())
                {
                case QuestState.InProgress:
                    context.Response.StatusCode = StatusCodes.Status400BadRequest;
                    claimQuestDTOResponse       = new ClaimQuestDTOResponse(QuestState.InProgress.ToString(), "0", "you cannot claim this quest, it's in progress");
                    break;

                case QuestState.Claimed:
                    context.Response.StatusCode = StatusCodes.Status400BadRequest;
                    claimQuestDTOResponse       = new ClaimQuestDTOResponse(QuestState.Claimed.ToString(), "0", "you cannot claim this quest, it was already claimed");
                    break;

                case QuestState.Failed:
                    context.Response.StatusCode = StatusCodes.Status400BadRequest;
                    claimQuestDTOResponse       = new ClaimQuestDTOResponse(QuestState.Failed.ToString(), "0", "you cannot claim this quest, it was already failed");
                    break;

                case QuestState.Finished:
                    context.Response.StatusCode = StatusCodes.Status200OK;
                    quest = _questRepository.SetIsClaimedById(user, quest.Id).GetAwaiter().GetResult();
                    _avatarRepository.AddXp(user, quest).GetAwaiter().GetResult();
                    claimQuestDTOResponse = new ClaimQuestDTOResponse(QuestState.Finished.ToString(), quest.XpValue.ToString(), "you have claimed this quest");
                    break;

                default:
                    throw new NotSupportedException();
                }
                serializedString = JsonSerializer.Serialize(claimQuestDTOResponse);
                dtoResponse      = claimQuestDTOResponse;
                context.Response.WriteAsync(serializedString).GetAwaiter().GetResult();
            }
        }
Beispiel #4
0
        protected override void ExecuteRequest(HttpContext context)
        {
            (bool isOk, User user) = CheckClaimsForUser(Request, context, _userRepository);
            if (!isOk || user == null)
            {
                return;
            }

            PhysicalActivity PA = _physicalActivitiesRepository.GetPhysicalActivity(Request.Name);

            if (PA == null)
            {
                context.Response.StatusCode = StatusCodes.Status204NoContent;
                return;
            }

            PhysicalActivityEntry PAEntry = _physicalActivitiesEntryRepository.Insert(new PhysicalActivityEntry()
            {
                UserId = user.Id,
                PhysicalActivitiesId = PA.Id,
                DatetimeStart        = DateTime.Parse(Request.DateTimeStart),
                DatetimeEnd          = DateTime.Parse(Request.DateTimeEnd)
            }).GetAwaiter().GetResult();

            if (PAEntry != null)
            {
                // update all quests based on datas
                var quests = _questRepository.Get(user, _questTypeRepository, QuestState.InProgress).GetAwaiter().GetResult();
                foreach (var quest in quests)
                {
                    var questHandler = QuestHandlers.Create(quest, user, _questTypeRepository);
                    questHandler.Update(PhysicalActivityQuestHandler.PHYSICAL_ACTIVTY_KEY, "1");
                    _questRepository.Update(quest).GetAwaiter().GetResult();
                }

                string PAEntryJson = JsonSerializer.Serialize(PAEntry);
                context.Response.StatusCode = StatusCodes.Status200OK;
                context.Response.WriteAsync(PAEntryJson).GetAwaiter().GetResult();
            }
            else
            {
                context.Response.StatusCode = StatusCodes.Status204NoContent;
            }
        }
Beispiel #5
0
        protected override void ExecuteRequest(HttpContext context)
        {
            (bool isOk, User user) = CheckClaimsForUser(Request, context, _userRepository);
            if (!isOk || user == null)
            {
                return;
            }

            FoodEntry foodEntryData = _foodEntryRepository.Insert(new FoodEntry()
            {
                UserId = user.Id,
                OpenFoodFactsDataId = Request.OFFDataId,
                Datetime            = DateTime.Now
            }).GetAwaiter().GetResult();

            if (foodEntryData != null)
            {
                var offData = _offDataRepository.Get(foodEntryData.OpenFoodFactsDataId).GetAwaiter().GetResult().FirstOrDefault();

                // update all quests based on datas
                var quests = _questRepository.Get(user, _questTypeRepository, InProgress).GetAwaiter().GetResult();
                foreach (var quest in quests)
                {
                    var questHandler = QuestHandlers.Create(quest, user, _questTypeRepository);
                    questHandler.Update("Calories", (foodEntryData.Servings * offData.EnergyServing).ToString());
                    _questRepository.Update(quest).GetAwaiter().GetResult();
                }

                string foodEntryJson = JsonSerializer.Serialize(foodEntryData);
                context.Response.StatusCode = StatusCodes.Status200OK;
                context.Response.WriteAsync(foodEntryJson).GetAwaiter().GetResult();
            }
            else
            {
                context.Response.StatusCode = StatusCodes.Status204NoContent;
            }
        }