public async Task <OrderDetails> Create(OrderDetails model, CancellationToken cancellationToken = default) { var order = await DbContext.Orders.AddAsync(new Persistence.EntityFramework.Entities.Order() { OrderDate = DateTime.Now, CustomerId = model.Customer.CustomerId, EmployeeId = model?.Employee?.EmployeeId, ShipperId = model.Shipper.ShipperId }, cancellationToken); model.OrderId = order.Entity.OrderId; foreach (var modelProduct in model.Products) { await DbContext.OrderDetails.AddAsync(new Persistence.EntityFramework.Entities.OrderDetails() { ProductId = modelProduct.Product.ProductId, OrderId = order.Entity.OrderId, Quantity = modelProduct.Quantity, }, cancellationToken); } await DbContext.SaveChangesAsync(cancellationToken); return(model); }
public async Task <Models.Products.Product> Create(Models.Products.Product model, CancellationToken cancellationToken = default) { var product = await DbContext.Products.AddAsync(Map(model), cancellationToken); await DbContext.SaveChangesAsync(cancellationToken); return(Map(product.Entity)); }
public async Task Delete(Models.Costumers.Customer model, CancellationToken cancellationToken = default) { var customerDb = await DbContext.Customers .Where(x => x.CustomerId == model.CustomerId) .FirstOrDefaultAsync(cancellationToken); DbContext.Customers.Remove(customerDb); await DbContext.SaveChangesAsync(cancellationToken); }
public async Task <Models.Shippers.Shipper> Edit(Models.Shippers.Shipper model, CancellationToken cancellationToken = default) { var shipperDb = await DbContext.Shippers .FirstOrDefaultAsync(x => x.ShipperId == model.ShipperId, cancellationToken); shipperDb = DbContext.Shippers.Update(Map(shipperDb, model)).Entity; await DbContext.SaveChangesAsync(cancellationToken); return(Map(shipperDb)); }
public async Task <CartViewModel> Handle(GetCartQuery request, CancellationToken cancellationToken) { // TODO: investigate why works on test and not in the real thing //var cart = await _context // .Carts.Where(p => p.UserId == request.UserId) // .Include(i => i.Items) // .ThenInclude(cartItem => cartItem.Product) // Works on tests on sqlite but not on MSSQL // .FirstOrDefaultAsync(cancellationToken); //Workaround use CartItems var cart = await _context.Carts.SingleOrDefaultAsync(w => w.UserId == request.UserId, cancellationToken); var cartItems = await _context.CartItems .Where(p => p.Cart.UserId == request.UserId) .Include(i => i.Product) .ToListAsync(cancellationToken); if (cart == null) { _context.Carts.Add(new Domain.Entities.Cart() { UserId = request.UserId }); var result = await _context.SaveChangesAsync(cancellationToken); cartItems = await _context.CartItems.Where(w => w.Cart.CartId == result).ToListAsync(cancellationToken); } return(new CartViewModel() { Items = _mapper.Map <IEnumerable <CartItemModel> >(cartItems) }); }
public async Task <Unit> Handle(AddProductCommand request, CancellationToken cancellationToken) { var procuct = await _context.Products.SingleOrDefaultAsync(w => w.ProductId == request.ProcuctId, cancellationToken); //Validate if it's real product if (procuct == null) { throw new NotFoundException(nameof(Product), request.ProcuctId); } var UserCart = await _context.Carts.Include(i => i.Items).SingleOrDefaultAsync(w => w.UserId == request.UserId, cancellationToken); //Verify if user have a cart if (UserCart == null) { throw new NotFoundException(nameof(Domain.Entities.Cart), request.UserId); } //A user must not be able to add duplicate products into their cart if (UserCart.Items.Any(w => w.ProductId == request.ProcuctId)) { throw new Exception("Can't add duplicated products into the cart"); } _context.CartItems.Add(new CartItem() { Cart = UserCart, ProductId = procuct.ProductId, Quantity = 1, }); await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <Unit> Handle(SubmitOrderCommand request, CancellationToken cancellationToken) { var UserCart = await _context.Carts.Include(i => i.Items).SingleOrDefaultAsync(w => w.UserId == request.UserId, cancellationToken); //Verify if user have a cart if (UserCart == null) { throw new NotFoundException(nameof(Domain.Entities.Cart), request.UserId); } //A user must not be able to submit an order for an empty cart. if (UserCart.Items.Count == 0) { throw new Exception("Can't submit order because the cart is empty"); } //TODO: Add submit order logic here //clean user cart UserCart.Items = new List <CartItem>(); _context.Carts.Update(UserCart); await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <Models.Employees.Employee> Edit(EmployeeDetails model, CancellationToken cancellationToken = default) { var employee = await DbContext.Employees .FirstOrDefaultAsync(x => x.EmployeeId == model.EmployeeId, cancellationToken); employee.Title = model.Title; employee.TitleOfCourtesy = model.TitleOfCourtesy; employee.Address = model.Address; employee.BirthDate = model.BirthDate; employee.City = model.City; employee.PostalCode = model.PostalCode; employee.Country = model.Country; employee.FullName = model.FullName; employee.HomePhone = model.HomePhone; employee.Region = model.Region; DbContext.Employees.Update(employee); await DbContext.SaveChangesAsync(cancellationToken); return(await GetId(employee.EmployeeId, cancellationToken)); }
public async Task <OrderDTOs.OrderBase> Handle(CreateOrderCommand request, CancellationToken cancellationToken) { // Save Order Items in the database and evaluate RequiredBinWidth var(persistedOrderItems, requiredBinWidth) = await PersistOrderItem(request); // Save Order in the database var persistedOrder = await PersistOrder(persistedOrderItems, requiredBinWidth); // Persist all the transactions in the database await _storeDbContext.SaveChangesAsync(cancellationToken); return(await Task.FromResult(_mapper.Map <OrderDTOs.OrderBase>(persistedOrder))); }
public async Task <Unit> Handle(AdjustQuantityCommand request, CancellationToken cancellationToken) { var UserCartItem = await _context.CartItems.SingleOrDefaultAsync(w => w.ProductId == request.ProcuctId && w.Cart.UserId == request.UserId , cancellationToken); if (UserCartItem == null) { throw new NotFoundException(nameof(Domain.Entities.CartItem), request.ProcuctId); } UserCartItem.Quantity = request.Quantity; await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <Unit> Handle(RemoveProductCommand request, CancellationToken cancellationToken) { var UserCartItem = await _context.CartItems.FirstAsync(w => w.ProductId == request.ProcuctId && w.Cart.UserId == request.UserId , cancellationToken); if (UserCartItem == null) { throw new NotFoundException(nameof(Domain.Entities.CartItem), request.ProcuctId); } _context.CartItems.Remove(UserCartItem); await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }