public IActionResult UpdateSuggestion([FromRoute] int id, [FromBody] Suggestion suggestion, [FromRoute] string userId) { Logger.Info(this, $"Suggestion with id {id} is being updated"); if (suggestion == null)//suggestion null of Id null: null coalescing operator { return(BadRequest("suggestion is required")); } var sugg = _suggestionRepository.Get(suggestion.Id); if (sugg == null) { return(BadRequest($"A suggestion with id {suggestion.Id} does not yet exist")); } if (suggestion.UserId != userId || suggestion.Id != id) { return(BadRequest("Ids don't match")); } if (string.IsNullOrWhiteSpace(suggestion.Name) || string.IsNullOrWhiteSpace(suggestion.Milestone)) { return(BadRequest("suggestion name and/or milestone is required")); } if (suggestion.NumberOfHours <= 0) { return(BadRequest("suggestion hours need to be greater than 0")); } _suggestionRepository.Update(id, suggestion); return(Ok()); }
public async Task <SuggestionResponse> UpdateASync(int id, Suggestion suggestion) { var existingSuggestion = await _suggestionRepository.FindById(id); if (existingSuggestion == null) { return(new SuggestionResponse("Suggestion not found")); } existingSuggestion.Message = suggestion.Message; try { _suggestionRepository.Update(existingSuggestion); await _unitOfWork.CompleteAsync(); return(new SuggestionResponse(existingSuggestion)); } catch (Exception ex) { return(new SuggestionResponse($"An error ocurred while updating the Suggestion: {ex.Message}")); } }