public static async Task <bool> Insertar(Order cabecera, IEnumerable <OrderItem> detalle) { bool exito = true; try { using (var data = new SalesContext()) { await data.Orders.AddAsync(cabecera); //Id no existe await data.SaveChangesAsync(); //Si existe el Id int newOrderID = cabecera.Id; decimal totalAmount = 0; foreach (var item in detalle) { item.OrderId = newOrderID; totalAmount = totalAmount + (item.UnitPrice * item.Quantity); } await data.OrderItems.AddRangeAsync(detalle); cabecera.TotalAmount = totalAmount; await data.SaveChangesAsync(); } } catch (Exception) { exito = false; } return(exito); }
public async Task <IActionResult> Edit(int id, [Bind("SalesOrderId,CustomerName,PONumber")] SalesOrder salesOrder) { if (id != salesOrder.SalesOrderId) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(salesOrder); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!SalesOrderExists(salesOrder.SalesOrderId)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(salesOrder)); }
public async Task <IActionResult> Post([FromBody] Order order) { salesContext.Add(order); await salesContext.SaveChangesAsync(); return(Created(order)); }
// metodo para inserir os dados na base public async Task InsertAsync(Seller obj) { _context.Add(obj); // confirmando a inserção await _context.SaveChangesAsync(); }
public async Task <IActionResult> PutCustomer(int id, Customer customer) { if (id != customer.Id) { return(BadRequest()); } _context.Entry(customer).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CustomerExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutProduct(int id, Product product) { if (id != product.Id) { return(BadRequest()); } _context.Entry(product).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ProductExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutOrderline(int id, Orderline orderline) { if (id != orderline.Id) { return(BadRequest()); } _context.Entry(orderline).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!OrderlineExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> Create([Bind("Id,FirstName,LastName,City,Country,Phone")] Customer customer) { if (ModelState.IsValid) { _context.Add(customer); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(customer)); }
public async Task <IActionResult> Create([Bind("AddressId,AddressLine1,AddressLine2,City,StateProvince,CountryRegion,PostalCode,rowguid,ModifiedDate")] Address address) { if (ModelState.IsValid) { _context.Add(address); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(address)); }
public async Task <IActionResult> Create([Bind("Id,CompanyName,ContactName,ContactTitle,City,Country,Phone,Fax")] Supplier supplier) { if (ModelState.IsValid) { _context.Add(supplier); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(supplier)); }
public async Task <IActionResult> Create([Bind("Id,Name")] Department department) { if (ModelState.IsValid) { _context.Add(department); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(department)); }
public async Task <IActionResult> Create([Bind("id,name,quantity,price,detail")] Product product) { if (ModelState.IsValid) { _context.Add(product); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(product)); }
public async Task <IActionResult> Create([Bind("Id,Date,Amount,Status")] SalesRecord salesRecord) { if (ModelState.IsValid) { _context.Add(salesRecord); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(salesRecord)); }
public async Task <ActionResult> Create([Bind(Include = "Id,Name,Price")] Product product) { if (ModelState.IsValid) { db.Products.Add(product); await db.SaveChangesAsync(); return(RedirectToAction("Index")); } return(View(product)); }
public async Task <IActionResult> Create([Bind("Id,ProductName,SupplierId,UnitPrice,Package,IsDiscontinued")] Product product) { if (ModelState.IsValid) { _context.Add(product); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["SupplierId"] = new SelectList(_context.Suppliers, "Id", "CompanyName", product.SupplierId); return(View(product)); }
public async Task <IHttpActionResult> Post(ProductSales product) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } db.ProductSales.Add(product); await db.SaveChangesAsync(); return(Created(product)); }
public async Task <ActionResult> Create([Bind(Include = "Id,Name,Surname")] Customer customer) { if (ModelState.IsValid) { db.Customers.Add(customer); await db.SaveChangesAsync(); return(RedirectToAction("Index")); } return(View(customer)); }
public async Task RemoveAsync(long id) { try { var seller = await FindSellerByIdAsync(id); _context.Seller.Remove(seller); await _context.SaveChangesAsync(); }catch (DbUpdateException ex) { throw new IntegrityException(ex.Message); } }
public async Task <ActionResult> Create([Bind(Include = "Id,Sum,CustomerId,ManagerId,ProductId,Date")] Sale sale) { if (ModelState.IsValid) { db.Sales.Add(sale); await db.SaveChangesAsync(); return(RedirectToAction("Index")); } ViewBag.CustomerId = new SelectList(db.Customers, "CustomerId", "Name", sale.CustomerId); ViewBag.ManagerId = new SelectList(db.Managers, "ManagerId", "Name", sale.ManagerId); ViewBag.ProductId = new SelectList(db.Products, "ProductId", "Name", sale.ProductId); return(View(sale)); }
public async Task Handle(SubmitOrderCommand message, IMessageHandlerContext context) { using (var _context = new SalesContext()) { Log.Info("Handle SubmitOrderCommand"); var items = new List <Item>(); var products = _context.Products.ToList(); message.Products.ForEach(p => items.Add(new Item() { Product = products.Single(s => s.Id == p) })); var order = new Divergent.Sales.Data.Models.Order() { CustomerId = message.CustomerId, DateTimeUtc = DateTime.UtcNow, Items = items, State = "New" }; _context.Orders.Add(order); await _context.SaveChangesAsync(); } }
public async Task Handle(SubmitOrderCommand message, IMessageHandlerContext context) { _log.LogInformation("Handle SubmitOrderCommand"); var items = new List <Item>(); var products = _db.Products.ToList(); message.Products.ForEach(p => items.Add(new Item { Product = products.Single(s => s.Id == p) })); var order = new Order { CustomerId = message.CustomerId, DateTimeUtc = DateTime.UtcNow, Items = items, State = "New" }; await _db.Orders.AddAsync(order); await _db.SaveChangesAsync(); // Publish event await context.Publish(new OrderSubmittedEvent { OrderId = order.Id, CustomerId = message.CustomerId, Products = message.Products, }); }
public async Task Handle(SubmitOrderCommand message, IMessageHandlerContext context) { using (var db = new SalesContext()) { Log.Info("Handle SubmitOrderCommand"); var items = new List <Item>(); var products = db.Products.ToList(); message.Products.ForEach(p => items.Add(new Item { Product = products.Single(s => s.Id == p) })); var order = new Data.Models.Order { CustomerId = message.CustomerId, DateTimeUtc = DateTime.UtcNow, Items = items, State = "New" }; db.Orders.Add(order); await db.SaveChangesAsync(); await context.Publish <OrderSubmittedEvent>(e => { e.OrderId = order.Id; e.CustomerId = message.CustomerId; e.Products = message.Products; }); } }
public static async Task UpdateDatabase(SalesContext db) { try { await db.SaveChangesAsync(); } catch (Exception e) { MessageBox.Show(e.StackTrace); } }
public async Task <IActionResult> Post([FromBody] string jsonValues) { try { var department = new Department(); JsonConvert.PopulateObject(jsonValues, department); if (!TryValidateModel(department)) { return(BadRequest(ModelState)); } _context.Department.Add(department); await _context.SaveChangesAsync(); return(Ok()); } catch (System.Exception) { throw; } }
public async Task <IHttpActionResult> AddToCart(dynamic data) { var cartId = (int)data.CartId; var productId = (int)data.ProductId; var quantity = (int)data.Quantity; var requestId = Request.Headers.GetValues("request-id").Single(); using (var db = new SalesContext()) { var cart = db.ShoppingCarts .Include(c => c.Items) .Where(o => o.Id == cartId) .SingleOrDefault(); if (cart == null) { cart = new ShoppingCart() { Id = data.CartId }; db.ShoppingCarts.Add(cart); } var alreadyAdded = cart.Items.Any(item => item.RequestId == requestId); if (!alreadyAdded) { var product = db.ProductsPrices .Where(o => o.ProductId == productId) .Single(); cart.Items.Add(new ShoppingCartItem() { CartId = cartId, RequestId = requestId, ProductId = productId, ProductPrice = product.Price, Quantity = quantity }); await ServiceBus.Instance.Publish <ProductAddedToCart>(e => { e.CartId = cartId; e.ProductId = productId; }); } await db.SaveChangesAsync(); } return(StatusCode(HttpStatusCode.OK)); }
public async Task Consume(ConsumeContext <OrderCompletedEvent> context) { var order = _salesContext.Orders.FirstOrDefault(t => t.OrderId == context.Message.OrderId && t.CustomerId == context.Message.CustomerId); if (order != null) { order.Status &= ~OrderStatus.Submitted; // unsettings Submitted state order.Status |= OrderStatus.Shipped; // setting Shipped state await _salesContext.SaveChangesAsync(); } _logger.LogInformation($"由顾客 {context.Message.CustomerId} 提交的订单 {context.Message.OrderId} 被标记为 已发货"); }
public static async Task <int> Insertar(Order cabecera, List <OrderItem> detalle) { //bool exito = true; int newID = -1; try { using (var data = new SalesContext()) { await data.Orders.AddAsync(cabecera);//Id no existe await data.SaveChangesAsync(); newID = cabecera.Id; decimal totalAmount = 0; foreach (var item in detalle) { totalAmount = totalAmount + (item.UnitPrice * item.Quantity); item.OrderId = newID; } await data.OrderItems.AddRangeAsync(detalle); cabecera.TotalAmount = totalAmount; await data.SaveChangesAsync(); } } catch (Exception) { newID = -1; throw; } return(newID); }
public static async Task <bool> Insertar(Customer customer) { bool exito = true; try { using var data = new SalesContext(); data.Customer.Add(customer); await data.SaveChangesAsync(); } catch (Exception) { exito = false; } return(exito); }
public async Task <string> GenPromoCode() { //сгенерим рандомную строку, и представим ее как промокод var promo = Guid.NewGuid().ToString("N").Substring(0, 6).ToUpper(); var user = await _db.Users.FirstOrDefaultAsync(u => u.PromotionalCode == promo); if (user != null || string.IsNullOrEmpty(promo)) { return("Ошибка генерации промокода, попробуйте снова"); } // добавляем пользователя в бд _db.Users.Add(new User { PromotionalCode = promo }); await _db.SaveChangesAsync(); return(promo); }
public static async Task <bool> Eliminar(int id) { bool exito = true; try { using var data = new SalesContext(); var customerNow = await Obtener(id); data.Customer.Remove(customerNow); await data.SaveChangesAsync(); } catch (Exception) { exito = false; } return(exito); }