Exemplo n.º 1
0
        public override async Task <AddTaskResponse> Handle(AddTaskRequest request, CancellationToken cancellationToken)
        {
            var commandRepository = CommandFactory.RepositoryAsync <Project>();
            var queryRepository   = QueryFactory.QueryRepository <Project>();

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

            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());

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

            return(new AddTaskResponse {
                Result = project.ToDto()
            });
        }
Exemplo n.º 2
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()
            });
        }