Ejemplo n.º 1
0
        public async Task <JsonResponse> Post([FromBody] extIngredient item, [FromRoute] Guid userId)
        {
            // Add this ingredient to the database, if it doesn't already exist
            Ingredient ingredient = await _ingManager.CheckDB(item);

            // Handle database error
            if (ingredient.IngredientId == -1)
            {
                return(new JsonResponse {
                    success = false, message = "Something went wrong while saving your ingredients to the database, please try again."
                });
            }
            // Add a grocery item record to the database
            var newGroceryItem = new GroceryItem {
                Amount = item.amount, UserId = userId, IngredientId = ingredient.IngredientId
            };

            newGroceryItem.Unit = (item.unit == null) ? "" : item.unit;
            _context.GroceryItem.Add(newGroceryItem);
            try
            {
                await _context.SaveChangesAsync();
            }
            catch
            {
                return(new JsonResponse {
                    success = false, message = "Something went wrong while saving to the database, please try again."
                });
            }
            return(new JsonResponse {
                success = true, message = "Grocery Item added successfully."
            });
        }
Ejemplo n.º 2
0
        // Checks the database for an existing ingredient captured from Spoonacular
        public async Task <Ingredient> CheckDB(extIngredient ingr)
        {
            Ingredient newIngredient = new Ingredient();
            var        dbIngredient  = await _context.Ingredient.SingleOrDefaultAsync(i => i.IngredientId == ingr.id || i.IngredientName == ingr.name);

            if (dbIngredient == null)
            {
                if (ingr.id < 1)
                {
                    newIngredient.IngredientId = rand.Next(100000000, 999999999);
                }
                else
                {
                    newIngredient.IngredientId = ingr.id;
                }
                newIngredient.IngredientName = ingr.name;
                newIngredient.Aisle          = ingr.aisle;
                newIngredient.ImageURL       = ingr.image;
                _context.Ingredient.Add(newIngredient);
                _context.Database.OpenConnection();
                try
                {
                    await _context.Database.ExecuteSqlCommandAsync("SET IDENTITY_INSERT dbo.Ingredient ON");

                    await _context.SaveChangesAsync();

                    await _context.Database.ExecuteSqlCommandAsync("SET IDENTITY_INSERT dbo.Ingredient OFF");
                }
                catch
                {
                    // Return an IngredientId of -1 if there were any errors with the database
                    newIngredient.IngredientId = -1;
                    return(newIngredient);
                }
                finally
                {
                    _context.Database.CloseConnection();
                }
                return(newIngredient);
            }
            ;
            return(dbIngredient);
        }
Ejemplo n.º 3
0
        public async Task <JsonResponse> Post([FromBody] extIngredient item, [FromRoute] Guid userId)
        {
            // Find an existing ingredient item record in the database by Id or by name
            var dbIngredient = await _ingManager.CheckDB(item);

            Ingredient newIngredient = null;

            if (dbIngredient.IngredientId == -1)
            {
                return(new JsonResponse {
                    success = false, message = "Something went wrong while saving an ingredient to the database, please try again."
                });
            }
            // Add a inventory item record to the database
            var newInventoryItem = new InventoryItem {
                Amount = item.amount, Unit = item.unit, UserId = userId
            };

            if (newIngredient != null)
            {
                newInventoryItem.IngredientId = newIngredient.IngredientId;
            }
            else
            {
                newInventoryItem.IngredientId = item.id;
            }
            _context.InventoryItem.Add(newInventoryItem);
            try
            {
                await _context.SaveChangesAsync();
            }
            catch
            {
                return(new JsonResponse {
                    success = false, message = "Something went wrong while saving to the database, please try again."
                });
            }
            return(new JsonResponse {
                success = true, message = "Grocery Item added successfully."
            });
        }