public async Task <bool> Handle(RemoveCartItemCommand request, CancellationToken cancellationToken) { try { var lineItems = _context.LineItems.Where(l => l.OrderID == request.OrderID && l.ItemID == request.ItemID); _context.LineItems.RemoveRange(lineItems); await _context.SaveChangesAsync(cancellationToken); return(true); } catch (Exception e) { return(false); } }
public async Task <CustomerModel> Handle(CreateCustomerCommand request, CancellationToken cancellationToken) { var entity = new Customer { Active = true, CreatedAt = DateTime.Now, FirstName = request.FirstName, MiddleName = request.MiddleName, LastName = request.LastName, Email = request.Email, Password = request.Password, Phone = request.Phone }; _context.Customers.Add(entity); await _context.SaveChangesAsync(cancellationToken); return(_mapper.Map <CustomerModel>(entity)); }
public async Task <UserModel> Handle(CreateUserCommand request, CancellationToken cancellationToken) { var entity = new User { Username = request.User.Username, Email = request.User.Email, Password = request.User.Password, FirstName = request.User.FirstName, MiddleName = request.User.MiddleName, LastName = request.User.LastName, Active = true, CreatedAt = DateTime.Now, }; _context.Users.Add(entity); await _context.SaveChangesAsync(cancellationToken); return(_mapper.Map <UserModel>(entity)); }
public async Task <CreateCartItemModel> Handle(CreateCartItemCommand request, CancellationToken cancellationToken) { var item = await _context.Items.FindAsync(request.ItemID); #region Create new order if needed if (request.OrderID == null || request.OrderID == 0) { var order = new Order { CustomerID = request.CustomerID, Active = true, CreatedAt = DateTime.Now, MerchantID = item.MerchantID, OrderStatusTypeID = _context.OrderStatusTypes.First().ID, }; _context.Orders.Add(order); await _context.SaveChangesAsync(cancellationToken); request.OrderID = order.ID; } if (request.OrderID == 0) { return(new CreateCartItemModel()); } #endregion var lineItemAmount = 0M; #region Determine Price switch (item.PriceTypeID) { default: lineItemAmount = item.Price.Value; break; } #endregion try { var lineItems = _context.LineItems .Where(l => l.OrderID == request.OrderID && l.ItemID == request.ItemID) .ToList(); var currentQty = lineItems.Count; if (request.AddSingleItem) { //add single item _context.LineItems.Add(new LineItem { OrderID = request.OrderID.Value, ItemID = request.ItemID, ItemAmount = lineItemAmount }); } else if (currentQty > 0 && request.NewQty == 0) { //remove all items var removeAllLineItems = _context.LineItems.Where(l => l.OrderID == request.OrderID && l.ItemID == request.ItemID); _context.LineItems.RemoveRange(removeAllLineItems); } else if (request.NewQty > 0) { var qtyDiff = request.NewQty.Value - currentQty; if (qtyDiff > 0) { for (int i = 0; i < qtyDiff; i++) { _context.LineItems.Add(new LineItem { OrderID = request.OrderID.Value, ItemID = request.ItemID, ItemAmount = lineItemAmount }); } } else if (qtyDiff < 0) { qtyDiff = -qtyDiff; lineItems = lineItems != null && qtyDiff < lineItems.Count ? lineItems.GetRange(lineItems.Count - qtyDiff, qtyDiff) : lineItems; _context.LineItems.RemoveRange(lineItems); } else { //do nothing, the current qty is equal to the NewQty } } await _context.SaveChangesAsync(cancellationToken); return(new CreateCartItemModel { CustomerID = request.CustomerID, ItemID = request.ItemID, OrderID = request.OrderID.Value, NewQty = null }); } catch (Exception e) { return(new CreateCartItemModel()); } }