public static Hint CreateAnswerFromDto(this HintCreateOrUpdateDto hint) { return(new Hint { Content = hint.Content, }); }
public async Task <IActionResult> CreateHint(int number, HintCreateOrUpdateDto hint) { try { if (hint == null) { return(BadRequest("Hint is required")); } var question = await _repository.GetQuestionByNumberAsync(number, true); if (question == null) { return(NotFound($"Could not find any question number: {number}")); } var existing = await _repository.GetHintAsync(number); if (existing != null) { return(BadRequest("Hint in use")); } var hintEntity = hint.CreateAnswerFromDto(); question.Hint = hintEntity; _repository.Update(question); if (!await _unitOfWork.SaveChangeAsync()) { return(BadRequest()); } return(CreatedAtRoute(nameof(GetHint), new { number = question.Number }, hintEntity.MapToReadDto())); } catch (Exception) { return(StatusCode(StatusCodes.Status500InternalServerError, "Database Failure")); } }
public async Task <IActionResult> UpdateHint(int number, HintCreateOrUpdateDto hint) { try { if (hint == null) { return(BadRequest("Hint is required")); } var question = await _repository.GetQuestionByNumberAsync(number, true); if (question == null) { return(NotFound($"Could not find any question number: {number}")); } var existing = await _repository.GetHintAsync(number); if (existing == null) { return(BadRequest($"Could not find any hint of question number: {number}")); } existing.UpdateAnswerFromDto(hint); _repository.Update(question); if (!await _unitOfWork.SaveChangeAsync()) { return(BadRequest()); } return(NoContent()); } catch (Exception) { return(StatusCode(StatusCodes.Status500InternalServerError, "Database Failure")); } }
public static void UpdateAnswerFromDto(this Hint entity, HintCreateOrUpdateDto dto) { entity.Content = !string.IsNullOrWhiteSpace(dto.Content) ? dto.Content : entity.Content; }