public Task <bool> AddIngredient(Guid recipeId, IIngredient ingredient) { return(Task.Run(() => { using var context = new HomeAppDbContext(myDbOptions); DbProduct product = context.Products.FirstOrDefault(p => p.Id.Equals(ingredient.ProductId)); if (product == null) { return false; } DbRecipe recipe = context.Recipes.FirstOrDefault(p => p.Id.Equals(recipeId)); if (product == null) { return false; } context.Ingredients.AddAsync(new DbIngredient { Product = product, Recipe = recipe, UnitQuantity = ingredient.UnitQuantity, UnitQuantityType = ingredient.UnitQuantityType }); context.SaveChanges(); return true; })); }
public void SaveRecipe(Services.IRecipe recipe) { using var context = new HomeAppDbContext(myDbOptions); // I create the library var dbRecipe = new DbRecipe { Id = recipe.Id, Name = recipe.Name, Steps = recipe.Steps.Select(rs => Convert(rs)).ToList() }; var ingredients = recipe.Ingredients.Select(r => new DbIngredient { Recipe = dbRecipe, ProductId = r.ProductId, UnitQuantity = r.UnitQuantity, UnitQuantityType = r.UnitQuantityType }).ToList(); // Linking the books (Library2Book table) to the library dbRecipe.Ingredients.AddRange(ingredients); // Adding the data to the DbContext. context.Recipes.Add(dbRecipe); // Save the changes and everything should be working! context.SaveChanges(); }
public async Task <List <TagModel> > GetAllTags() { using var context = new HomeAppDbContext(myDbOptions); return(await context.ProductTag.Select(p => new TagModel { Id = p.Id, Name = p.Name }).ToListAsync()); }
public async Task <List <ProductTagModel> > GetTagsForProduct(Guid productID) { using var context = new HomeAppDbContext(myDbOptions); return(await context.ProductTags.Where(p => p.ProductId == productID).Include(p => p.Tag).Select(p => new ProductTagModel { Id = p.Tag.Id, Name = p.Tag.Name, ProductId = p.ProductId }).ToListAsync()); }
public async Task DeleteProduct(Guid id) { using var context = new HomeAppDbContext(myDbOptions); var toDelete = context.Products.Where(p => p.Id == id).Include(pq => pq.ProductQuantities); context.Products.RemoveRange(toDelete); await context.SaveChangesAsync(); }
public async Task DeleteProductQuantityById(int id) { using var context = new HomeAppDbContext(myDbOptions); var toDelete = context.ProductQuantities.FirstOrDefault(p => p.Id == id); context.ProductQuantities.Remove(toDelete); await context.SaveChangesAsync(); }
public async Task UpdateProductQuantity(int id, ProductQuantity productQuantity) { using var context = new HomeAppDbContext(myDbOptions); var productToChange = context.ProductQuantities.FirstOrDefault(q => q.Id == id); productToChange.UnitQuantity = productQuantity.Quantity; await context.SaveChangesAsync(); }
public List <MeasurementForShopItemModel> GetMeasurementsForCheckedItem(Guid id) { using var context = new HomeAppDbContext(myDbOptions); var result = context.MeasurementsForShoppingListItem.Include(m => m.ProductQuantity) .Where(m => m.ShoppingListItemId == id).ToList(); return(result.Select(m => GetBaseMeasurement(m)) .ToList()); }
public async Task <IEnumerable <RecipeApiModel> > GetAllRecipes() { using var context = new HomeAppDbContext(myDbOptions); var result = await context.Recipes.Include(r => r.Ingredients).ThenInclude(i => i.Recipe).Include(r => r.Steps).ThenInclude(s => s.Recipe).ToListAsync(); result.ForEach(r => r.Ingredients.ForEach(i => i.Product = context.Products.FirstOrDefault(p => p.Id == i.ProductId))); return(result.Select(r => ConvertDbRecipeToRecipe(r))); }
public void SaveRecipe(string name, ERecipeType type) { using var context = new HomeAppDbContext(myDbOptions); context.Recipes.Add(new DbRecipe { Name = name, Type = type }); context.SaveChanges(); }
public void RemoveStep(Guid stepId) { using var context = new HomeAppDbContext(myDbOptions); var step = context.RecipeSteps.FirstOrDefault(r => r.Id == stepId); if (step == null) { return; } context.RecipeSteps.Remove(step); context.SaveChanges(); }
public void AddMeasurementsToItem(Guid shoppingListItemId, int measurementId, int count) { using var context = new HomeAppDbContext(myDbOptions); context.MeasurementsForShoppingListItem.Add(new DbShoppingListItemMeasurement { Count = count, ShoppingListItemId = shoppingListItemId, ProductQuantityId = measurementId }); context.SaveChanges(); }
public Guid GetProductIdForBarcode(string barcode) { using var context = new HomeAppDbContext(myDbOptions); var prod = context.ProductQuantities.Include(pq => pq.ProductId).FirstOrDefault(p => p.Barcode == barcode); if (prod == null) { // prod id does not exists return(Guid.Empty); } return(prod.ProductId.Id); }
public void AddTagToProduct(Guid productId, Guid tagId) { using var context = new HomeAppDbContext(myDbOptions); var newTag = new DbProductTags { ProductId = productId, TagId = tagId }; context.ProductTags.Add(newTag); context.SaveChanges(); }
public void RemoveProductFromShoppingList(Guid itemId) { using var context = new HomeAppDbContext(myDbOptions); DbShoppingListItem item = context.ShoppingListItems.Include(i => i.ShoppingListItemMeasurements).Include(s => s.ItemInfos).ThenInclude(i => i.RecipeItem).FirstOrDefault(s => s.Id == itemId); if (item == null) { throw new KeyNotFoundException($"{nameof(DbShoppingListItem)} with id {itemId} not found"); } context.ShoppingListItems.Remove(item); context.SaveChanges(); }
public void UpdateChecked(Guid itemId, bool isChecked) { using var context = new HomeAppDbContext(myDbOptions); context.ShoppingListItems.FirstOrDefault(sl => sl.Id == itemId).IsChecked = isChecked; if (!isChecked) { var toRemove = context.MeasurementsForShoppingListItem.Where(i => i.ShoppingListItemId == itemId); context.MeasurementsForShoppingListItem.RemoveRange(toRemove); } context.SaveChanges(); }
public void Delete(Guid productId, Guid recipeId) { using var context = new HomeAppDbContext(myDbOptions); var ingredientToRemove = context.Ingredients.FirstOrDefault(i => i.ProductId == productId && i.RecipeId == recipeId); if (ingredientToRemove == null) { return; } context.Ingredients.Remove(ingredientToRemove); context.SaveChanges(); }
public void Insert(IIngredient ingredient) { DbIngredient newIngredient = new DbIngredient { ProductId = ingredient.ProductId, RecipeId = ingredient.RecipeId, UnitQuantity = ingredient.UnitQuantity, UnitQuantityType = ingredient.UnitQuantityType }; using var context = new HomeAppDbContext(myDbOptions); context.Ingredients.Add(newIngredient); context.SaveChanges(); }
public void UpdateRecipe(Guid id, string newName) { using var context = new HomeAppDbContext(myDbOptions); var recipe = context.Recipes.FirstOrDefault(r => r.Id == id); if (recipe == null) { return; } recipe.Name = newName; context.SaveChanges(); }
public async void AddBarcodeToQuantity(int id, string barcode) { using var context = new HomeAppDbContext(myDbOptions); var prod = context.ProductQuantities.FirstOrDefault(p => p.Id == id); if (prod == null) { // prod id does not exists return; } prod.Barcode = barcode; await context.SaveChangesAsync(); }
public void DeleteById(Guid id) { using var context = new HomeAppDbContext(myDbOptions); var recipe = context.Recipes.FirstOrDefault(r => r.Id == id); if (recipe == null) { return; } context.Recipes.Remove(recipe); context.SaveChanges(); }
public async Task DereaseProductQuantity(int id, int value) { using var context = new HomeAppDbContext(myDbOptions); var quantity = context.ProductQuantities.Include(x => x.ProductId).FirstOrDefault(p => p.Id == id); if (quantity.ProductId.MeasurementValues == null) { var values = new MeasurementClassObject { Values = new List <MeasurementAmount> { new MeasurementAmount { Amount = quantity.QuantityTypeVolume, Type = quantity.UnitQuantityType } } }; quantity.ProductId.MeasurementValues = JsonConvert.SerializeObject(values); await context.SaveChangesAsync(); return; } var measurementClass = JsonConvert.DeserializeObject <MeasurementClassObject>(quantity.ProductId.MeasurementValues); measurementClass.Add(new MeasurementAmount { Amount = quantity.QuantityTypeVolume * -1, Type = quantity.UnitQuantityType }); //if (measurementClass.Values.Any(v => v.Type == quantity.UnitQuantityType)) //{ // measurementClass.Values.First(v => v.Type == quantity.UnitQuantityType).Amount += quantity.QuantityTypeVolume; // quantity.ProductId.MeasurementValues = JsonConvert.SerializeObject(measurementClass); // await context.SaveChangesAsync(); // return; //} //measurementClass.Values.Add(new MeasurementAmount //{ // Amount = quantity.QuantityTypeVolume, // Type = quantity.UnitQuantityType //}); quantity.ProductId.MeasurementValues = JsonConvert.SerializeObject(measurementClass); await context.SaveChangesAsync(); return; }
public IEnumerable <RecipeStep> GetStepForRecipe(Guid guid) { using var context = new HomeAppDbContext(myDbOptions); return(context.RecipeSteps .Include(s => s.Recipe) .Where(r => r.Recipe.Id == guid) .Select(s => new RecipeStep { Id = s.Id, Order = s.Order, RecipeId = s.Recipe.Id, Text = s.Text }) .ToList()); }
public async Task <bool> Update(Guid id, string newName) { using var context = new HomeAppDbContext(myDbOptions); var product = context.Products.FirstOrDefault(p => p.Id.Equals(id)); if (product == null) { throw new KeyNotFoundException($"Could not find product with Id: {id}"); } product.Name = newName; await context.SaveChangesAsync(); return(true); }
public List <ShoppingListItemInfoModel> GetInfoItemsForShoppingListItem(Guid shoppingListItemId) { using var context = new HomeAppDbContext(myDbOptions); IQueryable <DbShoppingListItemInfo> items = context.ShoppingListItemInfos.Include(i => i.RecipeItem).Where(ii => ii.ShoppingListItemId == shoppingListItemId); var result = items.Select(i => new ShoppingListItemInfoModel { Id = i.Id, MeasurementAmount = i.MeasurementAmount, MeasurementType = i.MeasurementType, Reason = i.Reason, RecipeItem = GetRecipeShoppingListItem(i.RecipeItem) }); return(result.ToList()); }
public RecipeApiModel GetRecipeById(Guid recipeId) { using var context = new HomeAppDbContext(myDbOptions); var result = context.Recipes .Include(r => r.Ingredients) .ThenInclude(i => i.Recipe).Include(r => r.Steps) .ThenInclude(s => s.Recipe) .FirstOrDefault(d => d.Id == recipeId); result.Ingredients.ForEach(i => i.Product = context.Products.Include(p => p.ProductQuantities).FirstOrDefault(p => p.Id == i.ProductId)); //result.Ingredients.ForEach(i => i.Product = context.Products.FirstOrDefault(p => p.Id == i.ProductId)); return(DbRecipeToRecipe(result)); }
public ProductDetails GetProductById(Guid productId) { using var context = new HomeAppDbContext(myDbOptions); DbProduct p = context.Products.Include(p => p.ProductQuantities).ThenInclude(pq => pq.ProductId).FirstOrDefault(p => p.Id == productId); if (p == null) { return(null); } return(new ProductDetails { Id = p.Id, MeasurementAmounts = JsonConvert.DeserializeObject <MeasurementClassObject>(p.MeasurementValues ?? string.Empty), Name = p.Name, ProductQuantities = ConvertProductQuantities(p.ProductQuantities) }); }
public async Task <IEnumerable <ProductDetails> > GetAllProductDetails() { using var context = new HomeAppDbContext(myDbOptions); List <DbProduct> products = await context.Products.Include(p => p.ProductQuantities).ThenInclude(pq => pq.ProductId).ToListAsync(); var retval = new List <ProductDetails>(); products.ForEach(p => { retval.Add(new ProductDetails { Id = p.Id, Name = p.Name, MeasurementAmounts = JsonConvert.DeserializeObject <MeasurementClassObject>(p.MeasurementValues ?? string.Empty), ProductQuantities = ConvertProductQuantities(p.ProductQuantities) }); }); return(retval); }
public void AddProduct(LightProduct product, out Guid newID) { if (string.IsNullOrEmpty(product.Name)) { newID = Guid.Empty; return; } DbProduct newProduct = new DbProduct { Id = Guid.NewGuid(), Name = product.Name }; //newProduct.ProductQuantities = ConvertProductQuantities(product.ProductQuantities, newProduct); newID = newProduct.Id; using var context = new HomeAppDbContext(myDbOptions); context.Products.Add(newProduct); context.SaveChanges(); }
public async Task <IEnumerable <LightProduct> > GetProducts() { using var context = new HomeAppDbContext(myDbOptions); List <DbProduct> products = await context.Products.Include(p => p.ProductQuantities).ThenInclude(pq => pq.ProductId).ToListAsync(); var retval = new List <LightProduct>(); products.ForEach(p => { retval.Add(new LightProduct { Id = p.Id, Name = p.Name, Tags = context.ProductTags.Include(t => t.Tag).Where(t => t.ProductId == p.Id).Select(t => new ProductTagModel { Id = t.TagId, Name = t.Tag.Name }).ToList() }); }); return(retval); }