public async Task ShouldUpdateGoal() { Guid profileId = Guid.NewGuid(); List <Goal> goals = new List <Goal> { new Goal(profileId, "Novo para teste", "Testando") }; UpdateGoalCommand command = new UpdateGoalCommand { GoalId = goals.First().Id, Title = "Testes de Goal", Details = "Registrando um goal para testes unitários", WritePost = false }; GoalFakeRepository goalFakeRepository = new GoalFakeRepository(goals); GoalCommandHandler handler = new GoalCommandHandler( goalFakeRepository, GetIdentityService(profileId), GetMediator(), GetUnitOfWork(), GetLogger() ); Assert.AreEqual("Novo para teste", goals.First().Title); CommandResult commandResult = await handler.Handle(command, default(CancellationToken)); Assert.AreEqual("Testes de Goal", goals.First().Title); Assert.IsTrue(commandResult.Success); }
public async Task ShouldFailToUpdateGoalUserNotOwner() { Guid profileId = Guid.NewGuid(); List <Goal> goals = new List <Goal> { new Goal(profileId, "Novo para teste", "Testando") }; UpdateGoalCommand command = new UpdateGoalCommand { GoalId = goals.First().Id, Title = "Testes de Goal", Details = "Registrando um goal para testes unitários", WritePost = false }; GoalFakeRepository goalFakeRepository = new GoalFakeRepository(goals); GoalCommandHandler handler = new GoalCommandHandler( goalFakeRepository, GetIdentityService(null), GetMediator(), GetUnitOfWork(), GetLogger() ); CommandResult commandResult = await handler.Handle(command, default(CancellationToken)); Assert.IsFalse(commandResult.Success); Assert.AreEqual("Falha ao buscar Objetivo no banco de dados.", commandResult.Notifications.FirstOrDefault().Description); }
public async Task <IActionResult> UpdateGoalAsync(Guid id, UpdateGoalCommand updateGoalCommand) { if (!Guid.TryParse(HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier)?.Value, out Guid userId)) { return(Unauthorized()); } updateGoalCommand.Id = id; var hasBeenUpdated = await _mediator.Send(updateGoalCommand); return(NoContent()); }
public GoalsController(AllGoalsQuery allGoalsQuery, UserGoalsQuery userGoalsQuery, GoalQuery goalQuery, CreateGoalCommand createGoalCommand, UpdateGoalCommand updateGoalCommand, DeleteGoalCommand deleteGoalCommand, CompleteGoalCommand completeGoalCommand) { this.allGoalsQuery = allGoalsQuery; this.userGoalsQuery = userGoalsQuery; this.goalQuery = goalQuery; this.createGoalCommand = createGoalCommand; this.updateGoalCommand = updateGoalCommand; this.deleteGoalCommand = deleteGoalCommand; this.completeGoalCommand = completeGoalCommand; }
public async Task <CommandResult> Handle(UpdateGoalCommand request, CancellationToken cancellationToken) { Goal goal = await _goalRepository.GetByIdAsync(request.GoalId); if (!FoundValidGoal(goal)) { return(FailureDueToGoalNotFound()); } goal.Update( request.Title, request.Details ); await _goalRepository.UpdateAsync(goal); return(await CommitAndPublishDefaultAsync()); }
public async Task <bool> Handle(UpdateGoalCommand message, CancellationToken cancellationToken) { if (!message.IsValid()) { NotifyValidationErrors(message); return(false); } var goal = new Goal(message.Description, message.Reward, message.StarsToAchieve, message.StarsEarned, message.StartDate, message.EndDate); var existingGoal = _goalRepository.GetById(goal.Id); if (existingGoal != null && existingGoal.Id != goal.Id) { if (!existingGoal.Equals(goal)) { await Bus.RaiseEvent(new DomainNotification(message.MessageType, "The Goal e-mail has already been taken.")); return(false); } } _goalRepository.Update(goal); if (await Commit()) { await Bus.RaiseEvent(new GoalUpdatedEvent(goal.Id, goal.Description, goal.Reward, goal.StarsToAchieve, goal.StarsEarned, goal.StartDate, goal.EndDate)); } return(true); }
public async Task <IActionResult> UpdateAsync(Guid id, [FromBody] UpdateGoalCommand command) { command.GoalId = id; return(await CreateCommandResponse(command)); }