public async Task ShouldStrokePet() { // Arrange var userIdentifier = await Shared.CreateUser(Client); var petIdentifier = await Shared.CreatePet(Client, userIdentifier); var payload = new PetActionControllerStrokePatch { PetIdentifier = petIdentifier, HappinessImprovement = 2 }; // Act var createResponse = await Shared.StrokePet(Client, payload); // Get Pet to see if value has increased var petResponse = await Shared.GetPet(Client, new PetControllerRootGet { PetIdentifier = petIdentifier }); var pet = JsonConvert.DeserializeObject <Pet>(await petResponse.Content.ReadAsStringAsync()); // Assert Assert.Equal(HttpStatusCode.OK, createResponse.StatusCode); Assert.Equal(pet.Happiness, PetDefaults.DefaultHappiness + 2); }
public void StrokePet(PetActionControllerStrokePatch payload) { // Validate Payload DataValidator.ValidateGuid(payload.PetIdentifier); // Get Pet Information var pet = GetPet(new PetControllerRootGet { PetIdentifier = payload.PetIdentifier }); if (pet == null) { throw new NoNullAllowedException("PetService (StrokePet) - The requested pet does not exist."); } // Update Pet var now = DateTime.Now; UpdatePet(new Pet { PetIdentifier = payload.PetIdentifier, LastHappiness = GetUpdatedMetric(pet.Happiness, payload.HappinessImprovement), LastHappinessUpdate = now } ); }
public ActionResult StrokePet([FromBody] PetActionControllerStrokePatch payload) { // Check if payload is present if (payload == null) { return(BadRequest("PetActionController (PATCH, stroke) - Missing payload.")); } try { _petService.StrokePet(payload); return(Ok()); } catch (JsonSerializationException e) { // Return 422 if we can't parse the payload as JSON return(UnprocessableEntity(e.Message)); } catch (InvalidDataException e) { // Return 400 if the payload doesn't contain any user identifier return(BadRequest(e.Message)); } catch (NoNullAllowedException e) { // Return 400 if no updates were performed return(BadRequest(e.Message)); } catch (KeyNotFoundException e) { // Return 404 if we couldn't find the pet we wanted to update return(NotFound(e.Message)); } catch (InvalidOperationException e) { // Return 409 if more than one update was performed return(Conflict(e.Message)); } catch (Exception e) { // Return 500 if any other exception occurred return(Problem(e.Message, e.Source, 500, "UserController (PUT)", e.GetType().ToString())); } }