Пример #1
0
        private async Task SeedCategories(CancellationToken cancellationToken)
        {
            Categories.Add(1, new Category {
                CategoryId = Guid.NewGuid(), Name = "Men's Fashion", Thumbnail = "no-image.jpg"
            });
            Categories.Add(2, new Category {
                CategoryId = Guid.NewGuid(), Name = "Women's Fashion", Thumbnail = "no-image.jpg"
            });
            Categories.Add(3, new Category {
                CategoryId = Guid.NewGuid(), Name = "Home & Kitchen", Thumbnail = "no-image.jpg"
            });
            Categories.Add(4, new Category {
                CategoryId = Guid.NewGuid(), Name = "Computers", Thumbnail = "no-image.jpg"
            });
            Categories.Add(5, new Category {
                CategoryId = Guid.NewGuid(), Name = "Electronics", Thumbnail = "no-image.jpg"
            });
            Categories.Add(6, new Category {
                CategoryId = Guid.NewGuid(), Name = "Toys & Games", Thumbnail = "no-image.jpg"
            });
            Categories.Add(7, new Category {
                CategoryId = Guid.NewGuid(), Name = "Video Games", Thumbnail = "no-image.jpg"
            });

            foreach (var category in Categories.Values)
            {
                _fuhoDbContext.Categories.Add(category);
            }

            await _fuhoDbContext.SaveChangesAsync(cancellationToken);
        }
        public async Task <CreateCategoryResponse> Handle(CreateCategoryCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var newCategory = new Category()
                {
                    Name      = request.Name,
                    Thumbnail = request.Thumbnail
                };

                _fuhoDbContext.Categories.Add(newCategory);

                await _fuhoDbContext.SaveChangesAsync(cancellationToken);

                return(new CreateCategoryResponse
                {
                    ErrorCode = ResponseStatus.Success.ToInt(),
                    ErrorMessage = null,
                    ErrorMessageCode = null,
                    CategoryDto = newCategory.ToCategoryEntity()
                });
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public async Task <Unit> Handle(UpdateCommentCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var comment = await _fuhoDbContext.Comments.FindAsync(request.CommentId);

                if (comment == null)
                {
                    throw new NullResult(nameof(Comment), nameof(request.CommentId));
                }

                if (request.UserId != comment.CreatedBy)
                {
                    throw new ForbiddenAction(nameof(UpdateCommentCommand), request.UserId);
                }

                comment.Content = request.Content;
                comment.IsEdit  = true;
                comment.Rating  = request.Rating;

                _fuhoDbContext.Comments.Update(comment);
                await _fuhoDbContext.SaveChangesAsync(cancellationToken);

                return(Unit.Value);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public async Task <CreateCartResponse> Handle(CreateCartCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var cart = new Cart()
                {
                };

                _fuhoDbContext.Carts.Add(cart);

                await _fuhoDbContext.SaveChangesAsync(cancellationToken);

                return(new CreateCartResponse
                {
                    ErrorCode = ResponseStatus.Success.ToInt(),
                    ErrorMessage = null,
                    ErrorMessageCode = null,
                    CartDto = cart.ToCartEntity()
                });
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public async Task <Unit> Handle(CreateProductCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var newProduct = new Product()
                {
                    ProductName    = request.ProductName,
                    BrandName      = request.BrandName,
                    Price          = request.Price,
                    Sku            = request.Sku,
                    Stock          = request.Stock,
                    ProductOptions = request.ProductOptions,
                    Images         = await _fileSystemService.SingleUpload(request.File),
                    CategoryId     = request.CategoryId
                };

                _fuhoDbContext.Products.Add(newProduct);

                await _fuhoDbContext.SaveChangesAsync(cancellationToken);

                return(Unit.Value);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public async Task <Unit> Handle(UpdateCategoryCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var category = await _fuhoDbContext.Categories.FindAsync(request.CategoryId);

                if (category != null)
                {
                    //Save category
                    category.Name      = request.Name;
                    category.Thumbnail = request.Thumbnail;

                    _fuhoDbContext.Categories.Update(category);
                    await _fuhoDbContext.SaveChangesAsync(cancellationToken);

                    return(Unit.Value);
                }
                else
                {
                    throw new NullResult(nameof(Category), nameof(request.CategoryId));
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Пример #7
0
        public async Task <CreateOrderResponse> Handle(CreateOrderCommand request, CancellationToken cancellationToken)
        {
            try
            {
                // add order
                var listOrderDetail = new List <OrderDetail>();

                var orderId  = Guid.NewGuid();
                var newOrder = new Order
                {
                    OrderId       = orderId,
                    ShipName      = request.ShipName,
                    ShipAddress   = request.ShipAddress,
                    ShipCity      = request.ShipCity,
                    ShipCountry   = request.ShipCountry,
                    IsCancelOrder = false
                };

                // add order detail
                foreach (var item in request.OrderDetails)
                {
                    var newOrderDetail = new OrderDetail
                    {
                        OrderDetailId = Guid.NewGuid(),
                        ProductId     = item.ProductId,
                        OrderId       = orderId,
                        UnitPrice     = item.UnitPrice,
                        Quantity      = item.Quantity,
                        Discount      = item.Discount
                    };
                    listOrderDetail.Add(newOrderDetail);
                }


                _fuhoDbContext.Orders.Add(newOrder);
                _fuhoDbContext.OrderDetails.AddRange(listOrderDetail);

                await _fuhoDbContext.SaveChangesAsync(cancellationToken);

                return(new CreateOrderResponse
                {
                    ErrorCode = ResponseStatus.Success.ToInt(),
                    ErrorMessage = null,
                    ErrorMessageCode = null,
                    OrderDto = newOrder.ToOrderEntity()
                });
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Пример #8
0
        public async Task <Unit> Handle(RemoveCategoryCommand request, CancellationToken cancellationToken)
        {
            var result = await _fuhoDbContext.Categories.FindAsync(request.CategoryId);

            if (result != null)
            {
                _fuhoDbContext.Categories.Remove(result);
                await _fuhoDbContext.SaveChangesAsync(cancellationToken);
            }
            else
            {
                throw new NullResult(nameof(Category), nameof(request.CategoryId));
            }

            return(Unit.Value);
        }
Пример #9
0
        public async Task <Unit> Handle(RemoveCommentCommand request, CancellationToken cancellationToken)
        {
            var result = await _fuhoDbContext.Comments.FindAsync(request.CommentId);

            if (result == null)
            {
                throw new NullResult(nameof(Comment), nameof(request.CommentId));
            }

            if (request.UserId != result.CreatedBy)
            {
                throw new ForbiddenAction(nameof(RemoveCommentCommand), request.UserId);
            }

            _fuhoDbContext.Comments.Remove(result);
            await _fuhoDbContext.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }
Пример #10
0
        public async Task <Unit> Handle(CreateCommentCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var newComment = new Comment()
                {
                    ProductId = request.ProductId,
                    Content   = request.Content,
                    Rating    = request.Rating
                };

                _fuhoDbContext.Comments.Add(newComment);
                await _fuhoDbContext.SaveChangesAsync(cancellationToken);

                return(Unit.Value);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public async Task <Unit> Handle(UpdateProductCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var product = await _fuhoDbContext.Products.AsNoTracking().FirstOrDefaultAsync(x => x.ProductId == request.ProductId);

                if (request.UserId != product.CreatedBy)
                {
                    throw new ForbiddenAction(nameof(UpdateProductHandler), request.UserId);
                }

                if (product == null)
                {
                    throw new NullResult(nameof(Product), nameof(request.ProductId));
                }

                //Save product
                product.ProductName    = request.ProductName;
                product.BrandName      = request.BrandName;
                product.Price          = request.Price;
                product.Sku            = request.Sku;
                product.Stock          = request.Stock;
                product.CategoryId     = request.CategoryId;
                product.ProductOptions = request.ProductOptions;
                product.Images         = await _fileSystemService.SingleUpdate(request.File, product.Images);

                _fuhoDbContext.Products.Update(product);
                await _fuhoDbContext.SaveChangesAsync(cancellationToken);

                return(Unit.Value);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }