Exemplo n.º 1
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;
            }
        }
Exemplo n.º 2
0
        protected override void ExecuteRequest(HttpContext context)
        {
            if (Request == null)
            {
                context.Response.StatusCode = StatusCodes.Status400BadRequest;
                return;
            }

            OpenFoodFactsDatasCategory openFoodFactsDataCategory = _oFFDatasCategoryRepository.GetByCategoryName(_categoryName).GetAwaiter().GetResult();

            if (openFoodFactsDataCategory != null)
            {
                OpenFoodFactsData openFoodFactsData = _oFFDatasCategoryRepository.GetOFFDataByOFFCategory(openFoodFactsDataCategory).GetAwaiter().GetResult();
                if (openFoodFactsData != null)
                {
                    string openFoodFactsDataCategoryJson = JsonSerializer.Serialize(openFoodFactsData);
                    context.Response.StatusCode = StatusCodes.Status200OK;
                    context.Response.WriteAsync(openFoodFactsDataCategoryJson).GetAwaiter().GetResult();
                    return;
                }
            }
            context.Response.StatusCode = StatusCodes.Status204NoContent;
        }
Exemplo n.º 3
0
        protected override void ExecuteRequest(HttpContext context)
        {
            if (Request == null)
            {
                context.Response.StatusCode = StatusCodes.Status400BadRequest;
                return;
            }

            OpenFoodFactsData openFoodFactsData = _OFFDataRepository.GetByBarcode(_barcode).GetAwaiter().GetResult();

            if (openFoodFactsData != null)
            {
                string openFoodFactsDataJson = JsonSerializer.Serialize(openFoodFactsData);
                context.Response.StatusCode = StatusCodes.Status200OK;
                context.Response.WriteAsync(openFoodFactsDataJson).GetAwaiter().GetResult();
            }
            else
            {
                ProductData productData;
                (openFoodFactsData, productData) = _OFFDataRepository.InsertFromBarcode(_barcode).GetAwaiter().GetResult();

                if (openFoodFactsData != null && productData != null)
                {
                    string[] categories = productData.Categories.Split(", ");
                    foreach (string category in categories)
                    {
                        if (_OFFCategoryRepository.GetByName(category).GetAwaiter().GetResult() == null)
                        {
                            OpenFoodFactsCategory openFoodFactsCategory = new OpenFoodFactsCategory
                            {
                                Name = category
                            };
                            openFoodFactsCategory = _OFFCategoryRepository.Insert(openFoodFactsCategory).GetAwaiter().GetResult();
                            if (openFoodFactsCategory == null)
                            {
                                context.Response.StatusCode = StatusCodes.Status204NoContent;
                                return;
                            }

                            OpenFoodFactsDatasCategory openFoodFactsDatasCategory = new OpenFoodFactsDatasCategory
                            {
                                CategoryId = openFoodFactsCategory.Id,
                                DataId     = openFoodFactsData.Id
                            };
                            openFoodFactsDatasCategory = _OFFDatasCategoryRepository.Insert(openFoodFactsDatasCategory).GetAwaiter().GetResult();
                            if (openFoodFactsDatasCategory == null)
                            {
                                context.Response.StatusCode = StatusCodes.Status204NoContent;
                                return;
                            }
                        }
                    }

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