コード例 #1
0
        public async Task <int> CreateQuestion(Library.Models.Question question)
        {
            if (question is null)
            {
                throw new ArgumentNullException(nameof(question));
            }
            _logger.LogInformation($"Adding question");

            Entities.Question entity = Mapper.Map(question);
            _dbContext.Add(entity);
            await _dbContext.SaveChangesAsync();

            return(question.QuestionId);
        }
コード例 #2
0
        /// <summary>
        /// Adds a new User into the database and updates the log
        /// </summary>
        /// <param name="user">The Library model of the User to be added</param>
        public async Task <int> AddUser(Library.Models.User user)
        {
            if (user is null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            _logger.LogInformation($"Adding user {user.UserName}");

            DataAccess.Entities.User entity = Mapper.Map(user);
            _dbContext.Add(entity);
            await _dbContext.SaveChangesAsync();

            return(user.UserId);
        }
コード例 #3
0
        public async Task <int> CreateChoice(Library.Models.Choice choice)
        {
            if (choice is null)
            {
                throw new ArgumentNullException(nameof(choice));
            }

            _logger.LogInformation($"Adding choice");

            Entities.Choice entity = Mapper.Map(choice);
            _dbContext.Add(entity);
            await _dbContext.SaveChangesAsync();

            return(choice.ChoiceId);
        }
コード例 #4
0
        /// <summary>
        /// Add a quiz, including any associated objects. Updates log.
        /// </summary>
        /// <param name="quiz">The quiz</param>
        public async Task <int> CreateQuiz(Library.Models.Quiz quiz)
        {
            if (quiz is null)
            {
                throw new ArgumentNullException(nameof(quiz));
            }

            _logger.LogInformation($"Adding quiz");

            Entities.Quiz entity = Mapper.Map(quiz);
            entity.Time = DateTime.Now;
            _dbContext.Add(entity);
            await _dbContext.SaveChangesAsync();

            //update user completed quizzes
            Entities.User userEntity = _dbContext.User.Where(u => u.UserId == entity.UserId).First();
            if (userEntity != null)
            {
                userEntity.CompletedQuizzes += 1;
                await _dbContext.SaveChangesAsync();
            }

            return(quiz.QuizId);
        }