public async Task <Unit> Handle(OrderCreateCommand request, CancellationToken cancellationToken) { var userId = identityService.GetUserId(); var basketItems = await context.BasketItems .Include(x => x.Product) .Where(x => x.UserId == userId) .ToListAsync(cancellationToken); var orderItems = basketItems.Select(x => new OrderItem { ProductId = x.ProductId, Quantity = x.Quantity, PricePerPiece = x.Product.Price } ).ToList(); var order = new Order { UserId = userId, OrderItems = orderItems, Name = request.Name, PhoneNumber = request.PhoneNumber, Address = request.Address, State = Order.OrderState.Unsent, DateTimeOfOrder = DateTime.UtcNow }; context.Orders.Add(order); context.BasketItems.RemoveRange(basketItems); await context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <Unit> Handle(BasketItemAddCommand request, CancellationToken cancellationToken) { var user = await context.Users .Include(x => x.BasketItems) .SingleOrDefaultAsync(x => x.Id == identityService.GetUserId(), cancellationToken); var product = await context.Products.FindAsync(request.ProductId); if (product == null) { throw new EntityNotFoundException("Product with requested ID is not found"); } if (product.Quantity < request.Quantity) { throw new ValidationException("Requested quantity of product is more than available quantity in stock"); } var existingBasketItem = user.BasketItems.SingleOrDefault(x => x.ProductId == request.ProductId); if (existingBasketItem == null) { user.BasketItems.Add(mapper.Map <BasketItem>(request)); } else { existingBasketItem.Quantity += request.Quantity; } product.Quantity -= request.Quantity; await context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <Unit> Handle(SocketCreateCommand request, CancellationToken cancellationToken) { context.Sockets.Add(mapper.Map <Socket>(request)); await context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <Unit> Handle(ProductEditCommand request, CancellationToken cancellationToken) { var product = await context.Products .Include(x => x.PropertyValues) .Include(x => x.ProductSockets) .SingleOrDefaultAsync(x => x.Id == request.Id); if (product != null) { context.PropertyValues.RemoveRange(product.PropertyValues); context.ProductSockets.RemoveRange(product.ProductSockets); product.Name = request.Name; product.Description = request.Description; product.Price = request.Price; product.Quantity = request.Quantity; product.ProductSockets = mapper.Map <List <ProductSocket> >(request.ProductSockets); context.PropertyValues.AddRange(request.PropertyValues.Select(x => new PropertyValue { ProductId = product.Id, PropertyTypeId = x.PropertyTypeId, Value = x.Value })); //product.PropertyValues = mapper.Map<List<PropertyValue>>(request.PropertyValues); } else { context.Products.Add(mapper.Map <Product>(request)); } await context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <Unit> Handle(OrderStateEditCommand request, CancellationToken cancellationToken) { var order = await context.Orders.FindAsync(request.Id); order.State = request.State; await context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <Unit> Handle(SocketRemoveCommand request, CancellationToken cancellationToken) { var socket = await context.Sockets.FindAsync(request.Id); context.Sockets.Remove(socket); await context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <Unit> Handle(OrderRemoveCommand request, CancellationToken cancellationToken) { var order = await context.Orders.FindAsync(request.Id); context.Orders.Remove(order); await context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <Unit> Handle(ProductRemoveCommand request, CancellationToken cancellationToken) { var product = await context.Products.FindAsync(request.ProductId); context.Products.Remove(product); await context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <Unit> Handle(CategoryRemoveCommand request, CancellationToken cancellationToken) { var category = await context.Categories .SingleOrDefaultAsync(x => x.Id == request.Id, cancellationToken); context.Categories.Remove(category); await context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <Unit> Handle(SocketEditCommand request, CancellationToken cancellationToken) { var result = await context.Sockets.FindAsync(request.Id); if (result == null) { throw new EntityNotFoundException(); } result.Name = request.Name; await context.SaveChangesAsync(); return(Unit.Value); }
public async Task <Unit> Handle(OrderAddressEditCommand request, CancellationToken cancellationToken) { var order = await context.Orders.FindAsync(request.Id); if (order.State != Order.OrderState.Unsent) { return(Unit.Value); } order.Address = request.Address; await context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <Unit> Handle(CategoryEditCommand request, CancellationToken cancellationToken) { var category = await context.Categories .Include(x => x.PropertyTypes) .Include(x => x.CategorySockets) .SingleOrDefaultAsync(x => x.Id == request.Id, cancellationToken); if (category == null) { throw new EntityNotFoundException("No category with the given id was found."); } category.Name = request.Name; // Property types // Remove property types to be removed context.PropertyTypes.RemoveRange( category.PropertyTypes.Where(x => request.PropertyTypes.All(pt => pt.Id != x.Id))); // Update existing property types foreach (var propertyType in category.PropertyTypes) { propertyType.Name = request.PropertyTypes.SingleOrDefault(x => x.Id == propertyType.Id)?.Name; } // Add property types to be added context.PropertyTypes.AddRange( request.PropertyTypes.Where(x => category.PropertyTypes.All(pt => pt.Id != x.Id)) .Select(x => new PropertyType { CategoryId = category.Id, Name = x.Name })); // Category Sockets context.CategorySockets.RemoveRange(category.CategorySockets); context.CategorySockets.AddRange(request.CategorySockets.Select(x => new CategorySocket { CategoryId = category.Id, SocketId = x.SocketId })); await context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <Unit> Handle(BasketItemRemoveCommand request, CancellationToken cancellationToken) { var basketItem = await context.BasketItems .Include(x => x.Product) .SingleOrDefaultAsync(x => x.Id == request.Id); if (basketItem == null) { throw new EntityNotFoundException("BasketItem with requested ID is not found"); } context.BasketItems.Remove(basketItem); basketItem.Product.Quantity += basketItem.Quantity; await context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <Unit> Handle(CommentEditCommand request, CancellationToken cancellationToken) { var comment = await context.Comments.FindAsync(request.Id, cancellationToken); if (comment != null) { comment.Text = request.Text; comment.Rating = request.Rating; } else { context.Comments.Add(mapper.Map <Comment>(request)); } await context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <Unit> Handle(CategoryCreateCommand request, CancellationToken cancellationToken) { context.Categories.Add(new Category { Name = request.Name, PropertyTypes = request.PropertyTypes.Select(x => new PropertyType { Name = x }).ToList(), CategorySockets = request.SocketIds.Select(x => new CategorySocket { SocketId = x }).ToList() }); await context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <Unit> Handle(BasketItemsRemoveCommand request, CancellationToken cancellationToken) { var user = await context.Users .Include(x => x.BasketItems) .ThenInclude(x => x.Product) .SingleOrDefaultAsync(x => x.Id == request.UserId, cancellationToken); if (user == null) { throw new EntityNotFoundException("User not found"); } context.BasketItems.RemoveRange(user.BasketItems); foreach (var item in user.BasketItems) { item.Product.Quantity += item.Quantity; } await context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <Unit> Handle(BasketItemEditCommand request, CancellationToken cancellationToken) { var user = await context.Users .Include(x => x.BasketItems) .ThenInclude(x => x.Product) .SingleOrDefaultAsync(x => x.Id == identityService.GetUserId(), cancellationToken); var basketItem = user.BasketItems .SingleOrDefault(x => x.Id == request.Id); if (basketItem == null) { user.BasketItems.Add(mapper.Map <BasketItem>(request)); } else { if (basketItem.ProductId != request.ProductId) { throw new ValidationException(); } if ((request.Quantity - basketItem.Quantity) > basketItem.Product.Quantity) { throw new ValidationException(); } basketItem.Product.Quantity += (basketItem.Quantity - request.Quantity); basketItem.Quantity = request.Quantity; if (basketItem.Quantity == 0) { context.BasketItems.Remove(basketItem); } } await context.SaveChangesAsync(cancellationToken); return(Unit.Value); }