private async Task <int> AddLocationToProduct(Product product, ProductRessource productRessource) { foreach (var productLocation in productRessource.ProductsAtLocations) { var location = await _context.Location.FindAsync(productLocation.LocationId); if (location == null) { return(-1); } if (productLocation.Quantity < 0) { return(-2); } await _context.LocationProduct.AddAsync(new LocationProduct() { Location = location, Product = product, Quantity = productLocation.Quantity }); } return(0); }
private async Task <int> AddCategoryToProduct(Product product, ProductRessource productRessource) { foreach (var productCategory in productRessource.Categories) { var category = _context.Category.Find(productCategory.Id); if (category == null) { if (productCategory.Name == null) { return(-1); } var newCategory = new Category() { Name = productCategory.Name }; await _context.Category.AddAsync(newCategory); AddToProductCategoryTable(product, newCategory); } else { if (productCategory.Name != null) { category.Name = productCategory.Name; } AddToProductCategoryTable(product, category); } } return(0); }
private async Task <int> AddPropertyToProduct(Product product, ProductRessource productRessource) { foreach (var productProperty in productRessource.Properties) { //Nach der Property suchen var property = _context.Property.Find(productProperty.Id); //Wenn die Property nicht existiert: Erstelle Property und füge hinzu if (property == null) { //Bei der Erstellung einer Property müssen Name und Value angegeben werden. if (productProperty.Name == null) { return(-1); } if (productProperty.Value == null) { return(-2); } //Neue Property erstellen var newProperty = new Property() { Name = productProperty.Name, Value = productProperty.Value }; //Eintrag in die Tabellen Property und ProductProperty await _context.Property.AddAsync(newProperty); AddToProductPropertyTable(product, newProperty); } //Die Property existiert else { //Falls Property Geändert wird: Änderungen übernehmen if (productProperty.Name != null) { property.Name = productProperty.Name; } if (productProperty.Value != null) { property.Value = productProperty.Value; } AddToProductPropertyTable(product, property); } } return(0); }
public async Task <ActionResult <ProductRessource> > PostProductAsync(ProductRessource productRessource) { // TODO: check/validate/sanitize values. using (var transaction = await _context.Database.BeginTransactionAsync()) { if (productRessource.Name == null) { return(Problem("The Products Price can not be under 0", statusCode: 404, title: "User Error")); } if (productRessource.Price < 0) { return(Problem("The Products Price can not be under 0", statusCode: 404, title: "User Error")); } if (productRessource.ProductsAtLocations.Count == 0) { return(BadRequest("Dem Produkt muss mindestens einen Standort zugewissen sein")); } var newProduct = new Product() { CreatedAt = DateTime.Now.ToUniversalTime(), Name = productRessource.Name, Price = productRessource.Price, Sku = productRessource.Sku }; await _context.Product.AddAsync(newProduct); int error = 0; if (productRessource.Properties != null) { error = await AddPropertyToProduct(newProduct, productRessource); if (error == -1) { return(BadRequest("Der Property muss ein Name zugewissen sein!")); } if (error == -2) { return(BadRequest("Der Property muss ein Value zugewissen sein!")); } } if (productRessource.Categories != null) { error = await AddCategoryToProduct(newProduct, productRessource); if (error == -1) { return(BadRequest("Der Kategorie muss ein Name zugewissen sein!")); } } if (productRessource.ProductsAtLocations != null) { error = await AddLocationToProduct(newProduct, productRessource); if (error == -1) { return(BadRequest("Der Standort existiert nicht")); } if (error == -2) { return(BadRequest("Die Anzahl darf nicht kleiner 0 sein!")); } } var result = await TryContextSaveAsync(); if (result != null) { return(result); } await transaction.CommitAsync(); return(CreatedAtAction(nameof(GetProductAsync), new { id = newProduct.Id }, newProduct)); } }