public async Task <IResponse <ActionResult> > AnswerOnQuoteQuiz(UserAnsweredQuiz answer) { try { var validationResult = quizAnswerValidator.Validate(answer); if (!validationResult.IsValid) { return(Invalid <ActionResult>(validationResult.Errors.Select(x => x.ErrorMessage).ToArray())); } var serviceModel = new Data.ServiceModels.UserAnsweredQuoteQuiz { AnswerId = answer.AnswerId, BinaryAnswer = answer.BinaryAnswer, QuoteId = answer.QuoteId, UserId = answer.UserId }; await userService.AnswerOnQuoteQuiz(serviceModel); return(Success <ActionResult>(null)); } catch (FqqException fex) { return(Error <ActionResult>(fex)); } }
/// <summary> /// user answer quote quiz /// </summary> /// <param name="userQuiz"></param> /// <returns></returns> public async Task AnswerOnQuoteQuiz(Data.ServiceModels.UserAnsweredQuoteQuiz userQuiz) { try { if (userQuiz.AnswerId != null) { try { await quoteAnswer.SingleAsync(x => x.QuoteId == userQuiz.QuoteId && x.Id == userQuiz.AnswerId); } catch (InvalidOperationException iox) { throw new FqqException(FqqExceptionCode.WrongAnswer, false, iox); } } var dbModel = new UserAnsweredQuote { UserId = userQuiz.UserId, QuoteId = userQuiz.QuoteId, QuoteAnswerId = userQuiz.AnswerId, BinaryAnswer = userQuiz.BinaryAnswer }; await userAnsweredQoute.AddAsync(dbModel); await userAnsweredQoute.SaveChangesAsync(); } catch (Exception ex) { throw new FqqException(FqqExceptionCode.GeneralError, "Error when inserting user answer", true, ex); } }