Exemplo n.º 1
0
        public override async Task <UpdateTaskResponse> Handle(UpdateTaskRequest request,
                                                               CancellationToken cancellationToken)
        {
            var commandRepository   = UnitOfWork.Repository <Project>();
            var queryRepository     = QueryRepositoryFactory.QueryEfRepository <Project>();
            var taskQueryRepository = QueryRepositoryFactory.QueryEfRepository <Domain.Task>();

            var project = await queryRepository.GetByIdAsync(request.ProjectId, q => q.Include(x => x.Tasks));

            if (project == null)
            {
                throw new Exception($"Could not find project#{request.ProjectId}.");
            }

            var task = await taskQueryRepository.GetByIdAsync(request.TaskId);

            task.ChangeTitle(request.Title)
            .ChangeOrder(request.Order ?? 1);

            if (request.Completed.HasValue && request.Completed.Value)
            {
                task.ChangeToCompleted();
            }

            project.UpdateTask(task);
            var updated = await commandRepository.UpdateAsync(project);

            await UnitOfWork.SaveChangesAsync(cancellationToken);

            return(new UpdateTaskResponse {
                Result = updated.ToDto()
            });
        }
Exemplo n.º 2
0
        public override async Task <ClearTasksResponse> Handle(ClearTasksRequest request,
                                                               CancellationToken cancellationToken)
        {
            var projectRepository = UnitOfWork.Repository <Project>();
            var taskRepository    = UnitOfWork.Repository <Task>();
            var queryRepository   = QueryRepositoryFactory.QueryEfRepository <Project>();

            var project = await queryRepository.GetByIdAsync(request.ProjectId, q => q.Include(x => x.Tasks));

            if (project == null)
            {
                throw new Exception($"Couldn't found the project#{request.ProjectId}.");
            }

            foreach (var projectTask in project.Tasks)
            {
                await taskRepository.DeleteAsync(projectTask);
            }
            await UnitOfWork.SaveChangesAsync(cancellationToken);

            project.ClearTasks();
            await projectRepository.UpdateAsync(project);

            await UnitOfWork.SaveChangesAsync(cancellationToken);

            return(new ClearTasksResponse());
        }
Exemplo n.º 3
0
        public override async Task <DeleteItemResponse> Handle(DeleteItemRequest request, CancellationToken cancellationToken)
        {
            var cartCommander     = UnitOfWork.Repository <Domain.Cart>();
            var cartItemCommander = UnitOfWork.Repository <CartItem>();
            var cartQuery         = QueryRepositoryFactory.QueryEfRepository <Domain.Cart>();

            var cart = await cartQuery
                       .GetFullCartAsync(request.CartId)
                       .ToObservable()
                       .SelectMany(c => c.InitCart(_catalogGateway));

            var cartItem = cart.CartItems.FirstOrDefault(x => x.Product.ProductId == request.ProductId);

            if (cartItem == null)
            {
                throw new Exception($"Could not find Product {request.ProductId}.");
            }

            await cartItemCommander.DeleteAsync(cartItem);

            await UnitOfWork.SaveChangesAsync(cancellationToken);

            cart = cart.RemoveCartItem(cartItem.Id);
            await cartCommander.UpdateAsync(cart);

            await UnitOfWork.SaveChangesAsync(cancellationToken);

            return(new DeleteItemResponse {
                ProductId = cartItem.Product.ProductId
            });
        }
        public override async Task <DeleteItemResponse> Handle(DeleteItemRequest request, CancellationToken cancellationToken)
        {
            var cartRepository     = UnitOfWork.Repository <Domain.Cart>();
            var cartItemRepository = UnitOfWork.Repository <CartItem>();

            var cart = await QueryRepositoryFactory
                       ?.QueryEfRepository <Domain.Cart>()
                       ?.GetFullCart(request.CartId)
                       ?.ToObservable()
                       ?.SelectMany(c => c.InitCart(_catalogGateway));

            var cartItem = cart.CartItems.FirstOrDefault(x => x.Product.ProductId == request.ProductId);

            if (cartItem == null)
            {
                throw new Exception($"Could not find CartItem {cartItem.Id}");
            }

            cart = cart.RemoveCartItem(cartItem.Id);
            var isSucceed = await cartRepository.UpdateAsync(cart) != null;

            await cartItemRepository.DeleteAsync(cartItem);

            return(new DeleteItemResponse {
                ProductId = cartItem.Product.ProductId
            });
        }
Exemplo n.º 5
0
        public override async Task <GetReviewsResponse> Handle(GetReviewsRequest request,
                                                               CancellationToken cancellationToken)
        {
            var reviewQueryRepo = QueryRepositoryFactory.QueryEfRepository <Domain.Review>();

            var reviews = await reviewQueryRepo
                          .ListAsync(
                queryable => queryable
                .Include(x => x.ReviewAuthor)
                .Include(x => x.ReviewProduct));

            var result = reviews.Where(x => x.ReviewProduct?.Id == request.ProductId);

            if (reviews == null)
            {
                throw new Exception($"Couldn't find a review with product#{request.ProductId}.");
            }

            return(new GetReviewsResponse
            {
                Reviews = result
                          .Select(x => x.ToDto())
                          .ToList()
            });
        }
Exemplo n.º 6
0
        public override async Task <DeleteTaskResponse> Handle(DeleteTaskRequest request,
                                                               CancellationToken cancellationToken)
        {
            var projectRepository = UnitOfWork.Repository <Project>();
            var taskRepository    = UnitOfWork.Repository <Task>();
            var queryRepository   = QueryRepositoryFactory.QueryEfRepository <Project>();

            var project = await queryRepository.GetByIdAsync(request.ProjectId, q => q.Include(x => x.Tasks));

            if (project == null)
            {
                throw new Exception($"Couldn't find the project#{request.ProjectId}.");
            }

            foreach (var projectTask in project.Tasks)
            {
                if (request.TaskId == projectTask.Id)
                {
                    await taskRepository.DeleteAsync(projectTask);
                }
            }

            project.RemoveTask(request.TaskId);
            var result = await projectRepository.UpdateAsync(project);

            await UnitOfWork.SaveChangesAsync(cancellationToken);

            return(new DeleteTaskResponse {
                Result = result.Id
            });
        }
Exemplo n.º 7
0
        public override async Task <AddTaskResponse> Handle(AddTaskRequest request, CancellationToken cancellationToken)
        {
            var projectRepository      = UnitOfWork.Repository <Project>();
            var taskRepository         = UnitOfWork.Repository <Task>();
            var projectQueryRepository = QueryRepositoryFactory.QueryEfRepository <Project>();

            var project = await projectQueryRepository.GetByIdAsync(request.ProjectId, q => q.Include(x => x.Tasks));

            if (project == null)
            {
                throw new Exception($"Couldn't found the project#{request.ProjectId}.");
            }

            var author = await _userGateway.GetAuthorAsync();

            if (author == null)
            {
                throw new Exception("Couldn't found the default author.");
            }

            var task = Task.Load(request.Title);

            task = task.SetAuthor(author.Id, author.GetFullName());

            task = await taskRepository.AddAsync(task);

            project.AddTask(task);
            project = await projectRepository.UpdateAsync(project);

            await UnitOfWork.SaveChangesAsync(cancellationToken);

            return(new AddTaskResponse {
                Result = project.ToDto()
            });
        }
        public override async Task <GetInventoryResponse> Handle(GetInventoryRequest request, CancellationToken cancellationToken)
        {
            var repo = QueryRepositoryFactory?.QueryEfRepository <Domain.Inventory>();
            var inv  = await repo.FindOneAsync(x => x.Id == request.InventoryId);

            return(new GetInventoryResponse {
                Result = inv.ToDto()
            });
        }
Exemplo n.º 9
0
        public override async Task <GetTodosResponse> Handle(GetTodosRequest request, CancellationToken cancellationToken)
        {
            var queryRepository = QueryRepositoryFactory.QueryEfRepository <Domain.Todo>();
            var result          = await queryRepository.ListAsync();

            return(new GetTodosResponse
            {
                Result = result.Select(x => x.ToDto()).ToList()
            });
        }
        public override async Task <GetTodoResponse> Handle(GetTodoRequest request, CancellationToken cancellationToken)
        {
            var queryRepository = QueryRepositoryFactory.QueryEfRepository <Domain.Todo>();
            var result          = await queryRepository.GetByIdAsync(request.Id);

            return(new GetTodoResponse
            {
                Result = result.ToDto()
            });
        }
        public override async Task <UpdateItemInCartResponse> Handle(UpdateItemInCartRequest request,
                                                                     CancellationToken cancellationToken)
        {
            var cartCommander     = UnitOfWork.Repository <Domain.Cart>();
            var cartItemCommander = UnitOfWork.Repository <CartItem>();
            var cartQuery         = QueryRepositoryFactory.QueryEfRepository <Domain.Cart>();

            var isNewItem = false;
            var cart      = await cartQuery
                            .GetFullCartAsync(request.CartId)
                            .ToObservable()
                            .SelectMany(c => c.InitCart(_catalogGateway, isPopulatePrice: true));

            var item = cart.CartItems.FirstOrDefault(x => x.Product.ProductId == request.ProductId);

            // if not exists then it should be a new item
            if (item == null)
            {
                isNewItem = true;
                item      = new CartItem()
                {
                    Quantity = request.Quantity
                };
                item.LinkProduct(new Product(request.ProductId));
                cart.CartItems.Add(item);
            }
            else
            {
                // otherwise is updating the current item in the cart
                item.Quantity += request.Quantity;
            }

            cart = _priceCalculator.Execute(cart);

            // Todo: refactor to unit of work later
            if (!isNewItem)
            {
                await cartItemCommander.UpdateAsync(item);
            }
            else
            {
                await cartItemCommander.AddAsync(item);
            }

            await cartCommander.UpdateAsync(cart);

            await UnitOfWork.SaveChangesAsync(cancellationToken);

            return(new UpdateItemInCartResponse {
                Result = cart.ToDto()
            });
        }
Exemplo n.º 12
0
        public override async Task <GetCartResponse> Handle(GetCartRequest request, CancellationToken cancellationToken)
        {
            var cart = await QueryRepositoryFactory
                       ?.QueryEfRepository <Domain.Cart>()
                       ?.GetFullCart(request.CartId)
                       ?.ToObservable()
                       ?.SelectMany(c => c.InitCart(_catalogGateway, isPopulatePrice: true))
                       ?.Select(c => _priceCalculator.Execute(c));

            return(new GetCartResponse {
                Result = cart.ToCartDto()
            });
        }
        public override async Task <GetCartResponse> Handle(GetCartRequest request, CancellationToken cancellationToken)
        {
            var cartRepo = QueryRepositoryFactory?.QueryEfRepository <Domain.Cart>();

            var cartInfo = await cartRepo.GetFullCart(request.CartId, false);

            cartInfo = await cartInfo.InitCart(_catalogGateway, isPopulatePrice : true);

            cartInfo = _priceCalculator.Execute(cartInfo);

            return(new GetCartResponse {
                Result = cartInfo.ToDto()
            });
        }
Exemplo n.º 14
0
        public override async Task <CheckoutResponse> Handle(CheckoutRequest request, CancellationToken cancellationToken)
        {
            var cartRepository = UnitOfWork.Repository <Domain.Cart>();

            var cart = await QueryRepositoryFactory
                       ?.QueryEfRepository <Domain.Cart>()
                       ?.GetFullCart(request.CartId);

            cart.IsCheckout = true;
            var checkoutCart = await cartRepository.UpdateAsync(cart);

            return(new CheckoutResponse
            {
                IsSucceed = checkoutCart != null
            });
        }
Exemplo n.º 15
0
        public override async Task <GetTasksResponse> Handle(GetTasksRequest request, CancellationToken cancellationToken)
        {
            var queryRepository = QueryRepositoryFactory.QueryEfRepository <Project>();

            var result = await queryRepository.GetByIdAsync(request.ProjectId, q => q.Include(x => x.Tasks));

            if (result == null)
            {
                throw new Exception($"Couldn't find project#{request.ProjectId}.");
            }

            return(new GetTasksResponse
            {
                Result = result.ToDto()
            });
        }
Exemplo n.º 16
0
        public override async Task <RemoveReviewResponse> TxHandle(RemoveReviewRequest request,
                                                                   CancellationToken cancellationToken)
        {
            var reviewQueryRepo = QueryRepositoryFactory.QueryEfRepository <Domain.Review>();
            var reviewRepo      = UnitOfWork.Repository <Domain.Review>();

            var review = await reviewQueryRepo.FindOneAsync(x => x.Id == request.ReviewId);

            if (review == null)
            {
                throw new Exception($"Could not find a review #{request.ReviewId}.");
            }

            var result = await reviewRepo.DeleteAsync(review);

            return(new RemoveReviewResponse {
                ReviewId = result.Id
            });
        }
        public override async Task <UpdateTodoResponse> TxHandle(UpdateTodoRequest request,
                                                                 CancellationToken cancellationToken)
        {
            var commandRepository = UnitOfWork.Repository <Domain.Todo>();
            var queryRepository   = QueryRepositoryFactory.QueryEfRepository <Domain.Todo>();

            var todo = await queryRepository.GetByIdAsync(request.Id);

            if (todo == null)
            {
                throw new Exception($"Could not find item #{request.Id}.");
            }

            var updated = await commandRepository.UpdateAsync(todo);

            return(new UpdateTodoResponse {
                Result = updated.ToDto()
            });
        }
Exemplo n.º 18
0
        public override async Task <ClearTodosResponse> TxHandle(ClearTodosRequest request,
                                                                 CancellationToken cancellationToken)
        {
            var commandRepository = UnitOfWork.Repository <Domain.Todo>();
            var queryRepository   = QueryRepositoryFactory.QueryEfRepository <Domain.Todo>();

            var todos = await queryRepository.ListAsync();

            if (todos == null || todos.Count <= 0)
            {
                return(new ClearTodosResponse());
            }

            //TODO: need to have a ClearAll method in CommandRepository
            foreach (var todo in todos)
            {
                await commandRepository.DeleteAsync(todo);
            }

            return(new ClearTodosResponse());
        }
Exemplo n.º 19
0
        public override async Task <UpdateReviewResponse> Handle(UpdateReviewRequest request,
                                                                 CancellationToken cancellationToken)
        {
            var reviewQueryRepo = QueryRepositoryFactory.QueryEfRepository <Domain.Review>();
            var reviewRepo      = UnitOfWork.Repository <Domain.Review>();

            var review = await reviewQueryRepo.FindOneAsync(x => x.Id == request.ReviewId);

            if (review == null)
            {
                throw new Exception($"Could not find a review #{request.ReviewId}.");
            }

            review.Content = request.Content;
            var result = await reviewRepo.UpdateAsync(review);

            await UnitOfWork.SaveChangesAsync(cancellationToken);

            return(new UpdateReviewResponse {
                Result = result.ToDto()
            });
        }