public async Task <IActionResult> CreateNewCustomer(CreateCustomerViewModel vm) { var existingCustomerId = ctx.Customers.Where(x => x.CustomerId == vm.CustomerId).FirstOrDefault(); if (existingCustomerId != null) { var ErrorMessage = "Det finns redan en produkt med detta kundnummret, välj ett annat."; return(Ok(ErrorMessage)); } else { try { var NewCustomer = new Customer(); NewCustomer.Name = vm.Name; NewCustomer.CustomerId = vm.CustomerId; NewCustomer.InvoiceAddress = vm.InvoiceAddress; NewCustomer.SecondInvoiceAddress = vm.SecondInvoiceAddress; NewCustomer.ZipCode = vm.ZipCode; NewCustomer.City = vm.City; NewCustomer.Country = vm.Country; NewCustomer.OrganisationNumber = vm.OrganisationNumber; NewCustomer.Fax = vm.Fax; NewCustomer.PhoneNumber = vm.PhoneNumber; NewCustomer.SecondPhoneNumber = vm.SecondPhoneNumber; NewCustomer.EmailAddress = vm.EmailAddress; NewCustomer.WebAddress = vm.WebAddress; NewCustomer.Description = vm.Description; ctx.Customers.Add(NewCustomer); await ctx.SaveChangesAsync(); } catch (Exception e) { throw e; } return(Ok()); } }
public async Task <IActionResult> CreateNewProduct(CreateNewProductViewModel vm) { var existingProductSku = ctx.Products.Where(x => x.ArticleNumber == vm.ArticleNumber).FirstOrDefault(); if (existingProductSku != null) { var ErrorMessage = "Det finns redan en produkt med detta artikelnumret, välj ett annat artikelnumer."; return(Ok(ErrorMessage)); } else { try { var NewProduct = new Product(); NewProduct.Name = vm.Name; NewProduct.ArticleNumber = vm.ArticleNumber; NewProduct.DateUpdated = DateTime.Now; NewProduct.DateCreated = DateTime.Now; NewProduct.Price = vm.Price; NewProduct.EAN = vm.EAN; NewProduct.Manufacturer = vm.Manufacturer; NewProduct.ManufacturerSku = vm.ManufacturerSku; NewProduct.Supplier = vm.Manufacturer; NewProduct.Description = vm.Description; NewProduct.CostPerItem = vm.CostPerItem; NewProduct.StockBalance = vm.StockBalance; NewProduct.ProductType = vm.ProductType; ctx.Products.Add(NewProduct); await ctx.SaveChangesAsync(); } catch (Exception e) { throw e; } return(Ok()); } }