public IHttpActionResult EditPreferences([FromBody] FoodPreferencesDto foodPreferencesDto) { // Check if model is valid for the database if (!ModelState.IsValid) { // If model is not valid, return bad request return(BadRequest(ModelState)); } try { // Get token string from request header var tokenString = Request.Headers.Authorization.Parameter; // If model is valid, call manager to get preferences var manager = new FoodPreferencesManager(); var response = manager.EditFoodPreferences(tokenString, foodPreferencesDto); // If there is an error in the response, return a bad request if (response.Error != null) { return(BadRequest(response.Error)); } // Otherwise, return the preferences return(Ok(response)); } catch (Exception) { // If any other error occurs, return a bad request return(InternalServerError()); } }
public void Should_FailValidation_When_FoodPreferencesIsNull() { // Arrange var validator = new FoodPreferencesDtoValidator(); var foodPreferencesDto = new FoodPreferencesDto(); // Act var result = validator.Validate(foodPreferencesDto); // Assert result.Errors.Should().NotBeNull(); }
/// <summary> /// Method to edit user's food preferences /// </summary> /// <param name="foodPreferencesDto"></param> /// <returns>Response DTO with a boolean determining success of the transaction</returns> public ResponseDto <bool> EditFoodPreferences(string tokenString, FoodPreferencesDto foodPreferencesDto) { // Validate the food preference dto var editFoodPreferencesValidationStrategy = new EditFoodPreferencesValidationStrategy(foodPreferencesDto); var validationResult = editFoodPreferencesValidationStrategy.ExecuteStrategy(); // If the food preference dto fails validation, return an error if (validationResult.Error != null) { return(new ResponseDto <bool> { Data = false, Error = GeneralErrorMessages.MODEL_STATE_ERROR }); } // Extract username from token string var tokenService = new TokenService(); var username = tokenService.GetTokenUsername(tokenString); using (var gateway = new UserGateway()) { // Get list of current food preferences from database var currentFoodPreferences = gateway.GetFoodPreferencesByUsername(username).Data.FoodPreferences; // Get list of updated food preferences from dto var updatedFoodPreferences = foodPreferencesDto.FoodPreferences; // Call method to create lists of food preferences to be added and to be removed var preferencesToBeAdded = LeftOuterJoin(updatedFoodPreferences, currentFoodPreferences); var preferencesToBeRemoved = LeftOuterJoin(currentFoodPreferences, updatedFoodPreferences); // If no changes are to be made and bypass front-end validation, return true if (preferencesToBeAdded.Count == 0 && currentFoodPreferences.Count == 0) { return(new ResponseDto <bool> { Data = true }); } // Otherwise, call gateway to update user's food preferences var result = gateway.EditFoodPreferencesByUsername(username, preferencesToBeAdded, preferencesToBeRemoved); // Return boolean determining success of update return(result); } }
public void Should_PassValidation_When_AllRulesPass() { // Arrange var validator = new FoodPreferencesDtoValidator(); var preferences = new List <string>() { "Pescetarian", "Lacto-Vegetarian" }; var foodPreferencesDto = new FoodPreferencesDto(preferences); // Act var result = validator.Validate(foodPreferencesDto); // Assert result.IsValid.Should().BeTrue(); result.Errors.Should().BeEmpty(); }
// Constructor public EditFoodPreferencesValidationStrategy(FoodPreferencesDto foodPreferencesDto) { _foodPreferencesDto = foodPreferencesDto; _foodPreferencesDtoValidator = new FoodPreferencesDtoValidator(); }