public async Task <ApplicationCore.Models.Question> AddQuestionAsync(ApplicationCore.Models.Question question) { if (question is null) { throw new ArgumentNullException(nameof(question)); } Entities.Question entity = Mapper.Map(question); await _context.Question.AddAsync(entity); await _context.SaveChangesAsync(); //add questionchoices to db foreach (var choice in question.QuestionChoices) { var coreChoice = new ApplicationCore.Models.Choice { QuestionId = entity.QuestionId, Correct = (bool)choice.Correct, ChoiceString = choice.ChoiceString }; await AddChoiceAsync(coreChoice); } await _context.SaveChangesAsync(); return(Mapper.Map(entity)); }
public static async Task <bool> AnswerQuestion(AnswerQuestionReq answerQuestionReq) { try { using TriviaGameDBContext _dbContext = DBContext; Game _game = await TriviaGameRestEFAPIDAL.GetGameById(_dbContext, answerQuestionReq.GameId); if (_game is null) { throw new Exception("Invalid Game Id."); } Question _question = await TriviaGameRestEFAPIDAL.GetQuestionById(_dbContext, answerQuestionReq.QuestionId); if (_question is null) { throw new Exception("Invalid Question Id."); } if (await TriviaGameRestEFAPIDAL.IsQuestionAnswered(_dbContext, answerQuestionReq.GameId, answerQuestionReq.QuestionId)) { throw new Exception("Question already answered."); } Choice _choice = null; if (answerQuestionReq.ChoiceId != null) { _choice = await TriviaGameRestEFAPIDAL.GetChoiceById(_dbContext, (Guid)answerQuestionReq.ChoiceId); if (_choice is null) { throw new Exception("Invalid Choice Id."); } } answerQuestionReq.AnswerDuration = answerQuestionReq.AnswerDuration > _question.QuestionDuration ? _question.QuestionDuration : answerQuestionReq.AnswerDuration; Answer _answer = new Answer() { Game = _game, Question = _question, Choice = _choice, AnswerDuration = answerQuestionReq.ChoiceId != null ? answerQuestionReq.AnswerDuration : _question.QuestionDuration }; await _dbContext.Answer.AddAsync(_answer); await _dbContext.SaveChangesAsync(); return(!(_choice is null) && _choice.IsCorrect); } catch (Exception) { throw; } }
public static async Task <GameResp> CreateGame(string genreName) { try { using TriviaGameDBContext _dbContext = DBContext; Genre _genre = await TriviaGameRestEFAPIDAL.GetGenreByGenreName(_dbContext, genreName); if (_genre is null) { throw new Exception("Invalid Genre Name."); } Game _game = new Game() { Genre = _genre, CreationDate = DateTime.Now, }; await _dbContext.Game.AddAsync(_game); await _dbContext.SaveChangesAsync(); return(FillGameResp(_game)); } catch (Exception) { throw; } }
public async Task <ApplicationCore.Models.User> AddUserAsync(ApplicationCore.Models.User user) { if (user is null) { throw new ArgumentNullException(nameof(user)); } Entities.User entity = Mapper.Map(user); await _context.User.AddAsync(entity); await _context.SaveChangesAsync(); return(Mapper.Map(entity)); }
public async Task <ApplicationCore.Models.Category> AddCategoryAsync(ApplicationCore.Models.Category category) { if (category is null) { throw new ArgumentNullException(nameof(category)); } Entities.Category entity = Mapper.Map(category); await _context.Category.AddAsync(entity); await _context.SaveChangesAsync(); return(Mapper.Map(entity)); }
public static async Task UpdateGenre(Guid genreId, string genreName) { try { using TriviaGameDBContext _dbContext = DBContext; Genre _genre = await TriviaGameRestEFAPIDAL.GetGenreById(_dbContext, genreId); _genre.GenreName = genreName; EntityEntry _entityEntry = _dbContext.Update(_genre); await _dbContext.SaveChangesAsync(); } catch (Exception) { throw; } }