public async Task Adopt_WhenUserAndAnimalFound_ShouldAdoptTheAnimal() { var key = nameof(User); var userId = 2; var code = 4; var adopt = new UserCommands.Adopt { Code = code }; var response = await _client.PostAsJsonAsync(string.Format(AdoptAnimalEndpoint, userId), adopt); var users = _factory.FakeUsersCache.Get <List <User> >(key); var user = users.FirstOrDefault(u => u.Id == userId); Assert.Single(user.Animals); Assert.Contains(user.Animals, animal => animal.Code == 4); }
public async Task <IActionResult> Adopt(int userId, [FromBody] UserCommands.Adopt adopt) { var user = await _userRepository.GetByIdAsync(userId); if (user == null) { return(BadRequest($"User with id:{userId} doesn't exist.")); } var animal = _animalFactory.Create(adopt.Code); if (animal == null) { return(BadRequest($"{adopt.Code} doesn't correspond to any animal.")); } await _userRepository.Adopt(user, animal); _hub.Publish(new UserEvents.AnimalAdopted { Animal = animal }); return(CreatedAtRoute(RouteNames.GetUserAnimal, new { userId = user.Id, animalId = animal.Id }, animal)); }