Пример #1
0
        public async Task <bool> AddUserOwnedQuestion(UserOwnedQuestionModel model)
        {
            using (var transaction = _context.Database.BeginTransaction())
            {
                try
                {
                    var id = model.Id ?? Guid.NewGuid().ToString();
                    _context.UserOwnedQuestions.Add(new UserOwnedQuestion()
                    {
                        Id               = id,
                        QuizzardUserId   = model.UserId,
                        QuestionText     = model.QuestionText,
                        QuestionTypeId   = model.QuestionTypeId,
                        UserOwnedAnswers = new List <UserOwnedAnswer>(model.Answers.Select(x => new UserOwnedAnswer()
                        {
                            Id                  = x.Id ?? Guid.NewGuid().ToString(),
                            AnswerText          = x.AnswerText,
                            Correctness         = x.Correctness,
                            UserOwnedQuestionId = id
                        }))
                    });
                    await _context.SaveChangesAsync();

                    transaction.Commit();
                    return(true);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    transaction.Rollback();
                    return(false);
                }
            }
        }
Пример #2
0
        public async Task <QuizModel> CreateQuiz(QuizModel model)
        {
            using (var transaction = _context.Database.BeginTransaction())
            {
                try
                {
                    var newId = Guid.NewGuid().ToString();
                    _context.Quizzes.Add(new Quiz
                    {
                        Id          = newId,
                        Title       = model.Title,
                        Description = model.Description,
                        SkillLevel  = model.SkillLevel,
                        Private     = model.Private,
                        DraftMode   = true,
                        CreatedOn   = DateTime.UtcNow
                    });
                    await _context.SaveChangesAsync();

                    transaction.Commit();
                    model.Id = newId;
                    return(model);
                }
                catch (Exception e)
                {
                    transaction.Rollback();
                    throw;
                }
            }
        }
Пример #3
0
        public async Task PlayQuiz(string userId, QuizSubmissionModel model)
        {
            var quiz = _context.Quizzes.FirstOrDefault(x => x.Id == model.QuizId);

            using (var transaction = _context.Database.BeginTransaction())
            {
                try
                {
                    var quizSubmissionId = model.Id ?? Guid.NewGuid().ToString();
                    var entity           = new QuizSubmission()
                    {
                        Id             = quizSubmissionId,
                        QuizzardUserId = userId,
                        QuizId         = model.QuizId,
                        SubmittedOn    = DateTime.Now,
                        Answers        = new List <QuizSubmissionAnswer>()
                    };

                    //entity.Answers = model.Answers.Select(x => new QuizSubmissionAnswer()
                    //{
                    //    Id = Guid.NewGuid().ToString(),
                    //    UserOwnedQuestionId = quiz.UserAttachedQuestions.FirstOrDefault(z => z.Id == x.QuestionId).Id,
                    //    QuestionId = quiz.Questions.FirstOrDefault(z => z.Id == x.QuestionId).Id,
                    //    SubmissionId = quizSubmissionId,
                    //    UserAnswer = x.UserAnswer
                    //}).ToList();


                    foreach (var x in model.Answers)
                    {
                        var userQuestionId = quiz.UserAttachedQuestions.FirstOrDefault(z => z.Id == x.QuestionId)?.Id;
                        var questionId     = quiz.Questions.FirstOrDefault(z => z.Id == x.QuestionId)?.Id;

                        var userAnswer = new QuizSubmissionAnswer()
                        {
                            Id = Guid.NewGuid().ToString(),
                            UserOwnedQuestionId = userQuestionId,
                            QuestionId          = questionId,
                            SubmissionId        = quizSubmissionId,
                            UserAnswer          = x.UserAnswer
                        };

                        entity.Answers.Add(userAnswer);
                    }



                    _context.QuizSubmissions.Add(entity);
                    await _context.SaveChangesAsync();

                    transaction.Commit();
                }
                catch (Exception e)
                {
                    transaction.Rollback();
                }
            }
        }
Пример #4
0
        public async Task <bool> LikeOrUnlikeQuiz(string quizId, string userId)
        {
            using (var transaction = _context.Database.BeginTransaction())
            {
                try
                {
                    _context.QuizReactions.Add(new QuizReaction()
                    {
                        QuizId = quizId, QuizzardUserId = userId
                    });
                    await _context.SaveChangesAsync();

                    transaction.Commit();
                    return(true);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    transaction.Rollback();
                    return(false);
                }
            }
        }