Esempio n. 1
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;
        }
Esempio n. 2
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;
                }
            }
        }