public async Task <ActionResult <Sale> > PostSale([FromBody] SaleVMStr saleVMStr) { // string customerNumber = (saleVM != null && saleVM.SelectedCustomer != null && Convert.ToInt32(saleVM.SelectedCustomer.CustomerNumber) > 0) ? saleVM.SelectedCustomer.CustomerNumber : ""; SaleVM saleVM = saleVMStr.SaleVm.GetObjectFronJson <SaleVM>(); //using var transaction = _context.Database.BeginTransaction(); { try { //List<SaleArticle> saleArticles = new List<SaleArticle>(); //if (saleVM != null && saleVM.articleRows != null && saleVM.articleRows.Count > 0) //{ // foreach (var item in saleVM.articleRows) // { // saleArticles.Add(new SaleArticle() // { // ArticleNumber = item.article.articleNumber, // Price = item.article.salesPrice, // Quantity = item.quantity, // TotalSum = item.article.salesPrice * item.quantity, // Description = item.article.description, // }); // } //} Sale sale = new Sale(); sale.CustomerNumber = saleVM.customer.customerNumber; sale.YourReference = saleVM.reference; //sale.SaleArticles = saleArticles; sale.DateCreated = DateTime.Parse(saleVM.dateCreated.Substring(0, 10)); sale.DateSold = DateTime.Parse(saleVM.dateSold.Substring(0, 10)); sale.StatusId = (Status)saleVM.statusId; sale.UserId = 1; // HACK: Fixxa // sale.UserId = 2; _context.Sales.Add(sale); await _context.SaveChangesAsync(); //transaction.Commit(); } catch (Exception e) { //transaction.Rollback(); } } return(CreatedAtAction(nameof(GetSale), new { id = saleVM.id }, saleVM)); }
public async Task <IActionResult> PutSale(int id, [FromBody] SaleVMStr saleVMStr) { SaleVM saleVM = saleVMStr.SaleVm.GetObjectFronJson <SaleVM>(); var existingSale = await _context.Sales.FindAsync(id); if (existingSale is null) { return(NotFound()); } if (id != saleVM.id) { return(BadRequest()); } //List<SaleArticle> saleArticles = new List<SaleArticle>(); //foreach (ArticleRow item in saleVM.articleRows) //{ // saleArticles.Add(new SaleArticle() // { // ArticleNumber = item.article.articleNumber, // Price = item.article.salesPrice, // Quantity = item.quantity, // TotalSum = item.article.salesPrice * item.quantity, // Description = item.article.description // }); //} existingSale.CustomerNumber = saleVM.customer.customerNumber; existingSale.YourReference = saleVM.reference; existingSale.DateCreated = DateTime.Parse(saleVM.dateCreated.Substring(0, 10)); existingSale.DateSold = DateTime.Parse(saleVM.dateSold.Substring(0, 10)); existingSale.StatusId = (Status)saleVM.statusId; //existingSale.SaleArticles = saleArticles; try { _context.Entry(existingSale).State = EntityState.Modified; _context.SaveChanges(); } catch (DbUpdateConcurrencyException ex) { throw (ex); } return(StatusCode((int)HttpStatusCode.OK)); }