Ejemplo n.º 1
0
        public async Task <IActionResult> Create([Bind("BrandId,IngredientId")] BrandIngredient brandIngredient)
        {
            //ViewData["BrandId"] = new SelectList(_context.Brands, "Id", "Id", brandIngredient.BrandId);
            //ViewData["IngredientId"] = new SelectList(_context.Ingredients, "Id", "Id", brandIngredient.IngredientId);
            if (!ModelState.IsValid)
            {
                return(View(brandIngredient));
            }

            try
            {
                var    client   = new HttpClient();
                string json     = JsonConvert.SerializeObject(brandIngredient);
                var    response = await client.PostAsync(_baseUrl, new StringContent(json, Encoding.UTF8, "application/json"));

                if (response.IsSuccessStatusCode)
                {
                    return(RedirectToAction("Index"));
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, $"Unable to create record:{ex.Message}");
            }

            return(View(brandIngredient));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> PutBrandIngredient(int id, BrandIngredient brandIngredient)
        {
            if (id != brandIngredient.BrandId)
            {
                return(BadRequest());
            }

            _context.Entry(brandIngredient).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!BrandIngredientExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Edit(int id, [Bind("BrandId,IngredientId")] BrandIngredient brandIngredient)
        {
            //ViewData["BrandId"] = new SelectList(_context.Brands, "Id", "Id", brandIngredient.BrandId);
            //ViewData["IngredientId"] = new SelectList(_context.Ingredients, "Id", "Id", brandIngredient.IngredientId);

            if (!ModelState.IsValid)
            {
                return(View(brandIngredient));
            }
            var    client   = new HttpClient();
            string json     = JsonConvert.SerializeObject(brandIngredient);
            var    response = await client.PutAsync($"{_baseUrl}/{brandIngredient.BrandId}/{brandIngredient.IngredientId}", new StringContent(json, Encoding.UTF8, "application/json"));

            if (response.IsSuccessStatusCode)
            {
                return(RedirectToAction("Index"));
            }

            return(View(brandIngredient));
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> DeleteConfirmed([Bind("Id")] BrandIngredient brandIngredient)
        {
            try
            {
                var client = new HttpClient();
                HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Delete, $"{_baseUrl}/{brandIngredient.BrandId}/{brandIngredient.IngredientId}")
                {
                    Content = new StringContent(JsonConvert.SerializeObject(brandIngredient), Encoding.UTF8, "application/json")
                };

                var response = await client.SendAsync(request);

                return(RedirectToAction("Index"));
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, $"Unable to delete record:{ex.Message}");
            }

            return(View(brandIngredient));
        }
Ejemplo n.º 5
0
        public async Task <ActionResult <BrandIngredient> > PostBrandIngredient(BrandIngredient brandIngredient)
        {
            _context.BrandIngredients.Add(brandIngredient);
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (BrandIngredientExists(brandIngredient.BrandId))
                {
                    return(Conflict());
                }
                else
                {
                    throw;
                }
            }

            return(CreatedAtAction("GetBrandIngredient", new { id = brandIngredient.BrandId }, brandIngredient));
        }
Ejemplo n.º 6
0
        public async Task <IActionResult> GetRecipeIngredients(RecipeIngredientBrand recipeIngredientBrand)
        {
            var client           = new HttpClient();
            var recipeIngredient = await _context.Ingredients
                                   .Join(_context.BrandIngredients,
                                         ingredient => ingredient.Id,
                                         brandIngredient => brandIngredient.IngredientId,
                                         (ingredient, brandIngredient) => new { Ingredient = ingredient, BrandIngredient = brandIngredient })
                                   .Join(_context.Brands,
                                         x => x.BrandIngredient.BrandId,
                                         brand => brand.Id,
                                         (x, brand) => new { Brand = brand, BrandIngredient = x.BrandIngredient, Ingredient = x.Ingredient })
                                   .Where(joinedData => joinedData.Brand.Name.Equals(recipeIngredientBrand.Brand.Name) &&
                                          joinedData.Ingredient.Name.Equals(recipeIngredientBrand.Ingredient.Name))
                                   .FirstOrDefaultAsync();

            // vezi daca exista brandul, vezi daca exista ingredientul
            Brand brand = await _context.Brands.FirstOrDefaultAsync(b => b.Name.Equals(recipeIngredientBrand.Brand.Name));

            if (brand == null)
            {
                string json = JsonConvert.SerializeObject(recipeIngredientBrand.Brand);
                HttpResponseMessage response = await client.PostAsync(_brandsUrl, new StringContent(json, Encoding.UTF8, "application/json"));

                string brandJSON = await response.Content.ReadAsStringAsync();

                brand = JsonConvert.DeserializeObject <Brand>(brandJSON);
            }

            Ingredient ingredient = await _context.Ingredients.FirstOrDefaultAsync(b => b.Name.Equals(recipeIngredientBrand.Ingredient.Name));

            if (ingredient == null)
            {
                string json = JsonConvert.SerializeObject(recipeIngredientBrand.Ingredient);
                HttpResponseMessage response = await client.PostAsync(_ingredientsUrl, new StringContent(json, Encoding.UTF8, "application/json"));

                string ingredientJSON = await response.Content.ReadAsStringAsync();

                ingredient = JsonConvert.DeserializeObject <Ingredient>(ingredientJSON);
            }

            if (recipeIngredient == null)
            {
                BrandIngredient brandIngredient = new BrandIngredient()
                {
                    BrandId      = brand.Id,
                    IngredientId = ingredient.Id
                };

                string json = JsonConvert.SerializeObject(brandIngredient);
                HttpResponseMessage response = await client.PostAsync(_brandsIngredientsUrl, new StringContent(json, Encoding.UTF8, "application/json"));
            }

            int    amount      = recipeIngredientBrand.RecipeIngredient.Amount;
            string measurement = recipeIngredientBrand.RecipeIngredient.Measurement;
            int    recipeId    = recipeIngredientBrand.Recipe.Id;

            RecipeIngredient recipeIngredientToAdd = new RecipeIngredient()
            {
                RecipeId     = recipeId,
                IngredientId = ingredient.Id,
                BrandId      = brand.Id,
                Amount       = amount,
                Measurement  = measurement
            };

            string recipeIngredientJSON      = JsonConvert.SerializeObject(recipeIngredientToAdd);
            HttpResponseMessage postResponse = await client.PostAsync(_recipeIngredientsUrl, new StringContent(recipeIngredientJSON, Encoding.UTF8, "application/json"));

            //ModelState.Clear();
            return(View());
        }