public async Task <IActionResult> OnGetAsync(string BibleId, int Id)
        {
            IdentityUser user = await _userManager.GetUserAsync(User);

            PBEUser = await QuizUser.GetOrAddPBEUserAsync(_context, user.Email); // Static method not requiring an instance

            if (!PBEUser.IsQuizModerator())
            {
                return(RedirectToPage("/error", new { errorMessage = "Sorry! You do not have sufficient rights to edit a PBE BookList" }));
            }

            BookList = await _context.QuizBookLists.FindAsync(Id);

            if (BookList == null)
            {
                return(RedirectToPage("/error", new { errorMessage = "That's Odd! We weren't able to find this Book List" }));
            }

            this.BibleId = await Bible.GetValidPBEBibleIdAsync(_context, BibleId);

            //Initialize Books
            await _context.Entry(BookList).Collection(L => L.QuizBookListBookMaps).LoadAsync();

            BookList.PadBookListBookMapsForEdit();
            Books = BookList.QuizBookListBookMaps.ToList();
            Name  = BookList.BookListName;
            ViewData["BookSelectList"] = await BibleBook.GetBookSelectListAsync(_context, BibleId);

            return(Page());
        }
        public async Task <IActionResult> OnGetAsync(string BibleId, string Message)
        {
            IdentityUser user = await _userManager.GetUserAsync(User);

            PBEUser = await QuizUser.GetOrAddPBEUserAsync(_context, user.Email);

            if (!PBEUser.IsQuizModerator())
            {
                return(RedirectToPage("/error", new { errorMessage = "Sorry! You do not have sufficient rights to manage PBE Users" }));
            }

            var pbeUsers = from u in _context.QuizUsers
                           select u;

            if (!string.IsNullOrEmpty(SearchString))
            {
                pbeUsers = pbeUsers.Where(u => u.Email.Contains(SearchString)).OrderBy(u => u.Email).Take(20);
            }
            else
            {
                pbeUsers = pbeUsers.Where(u => u.IsModerator).OrderBy(u => u.Email).Take(20);
            }

            PBEUsers = await pbeUsers.ToListAsync();

            UserMessage = GetUserMessage(Message);
            return(Page());
        }
Example #3
0
        public async Task <IActionResult> OnGetAsync(int QuestionId, string Caller)
        {
            ReturnPath = Caller;
            IdentityUser user = await _userManager.GetUserAsync(User);

            PBEUser = await QuizUser.GetOrAddPBEUserAsync(_context, user.Email);

            if (!PBEUser.IsValidPBEQuestionBuilder())
            {
                return(RedirectToPage("/error", new { errorMessage = "Sorry! You do not have sufficient rights to edit a PBE question" }));
            }

            Question = await _context.QuizQuestions.FindAsync(QuestionId);

            if (Question == null)
            {
                return(RedirectToPage("/error", new { errorMessage = "That's Odd! We weren't able to find this Question" }));
            }

            // Setup our PBEBook Object
            Question.BibleId = await QuizQuestion.GetValidBibleIdAsync(_context, Question.BibleId);

            BibleBook PBEBook = await BibleBook.GetPBEBookAndChapterAsync(_context, Question.BibleId, Question.BookNumber, Question.Chapter);

            if (PBEBook == null)
            {
                return(RedirectToPage("/error", new { errorMessage = "That's Odd! We weren't able to find the PBE Book." }));
            }

            Question.PopulatePBEQuestionInfo(PBEBook);
            Question.Verses = await Question.GetBibleVersesAsync(_context, false);

            // We need an answer text, and while techincally we support multiple Answers
            // we are only going to allow operating on the first one in this basic edit experience.
            await _context.Entry(Question).Collection(Q => Q.QuizAnswers).LoadAsync();

            if (Question.QuizAnswers.Count > 0)
            {
                AnswerText = Question.QuizAnswers.OrderBy(A => A.Id).First().Answer;
            }
            else
            {
                AnswerText = "";
            }

            IsCommentary = (Question.Chapter == Bible.CommentaryChapter);
            if (IsCommentary == false)
            {
                ChapterQuestionCount = PBEBook.BibleChapters.Where(c => c.ChapterNumber == Question.Chapter).First().QuestionCount;
            }
            CommentaryQuestionCount = PBEBook.CommentaryQuestionCount;

            // and now we need a Verse Select List
            ViewData["VerseSelectList"]  = new SelectList(Question.Verses, "Verse", "Verse");
            ViewData["PointsSelectList"] = Question.GetPointsSelectList();
            return(Page());
        }
Example #4
0
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for
        // more details see https://aka.ms/RazorPagesCRUD.
        public async Task <IActionResult> OnPostAsync()
        {
            if (await QuizBookList.ListNameAlreadyExistsStaticAsync(_context, Name))
            {
                ModelState.AddModelError("Name", "Sorry, this Name is already in use.");
            }
            if (!ModelState.IsValid)
            {
                ViewData["BookSelectList"] = await BibleBook.GetBookSelectListAsync(_context, BibleId);

                return(Page());
            }

            // confirm our user is a valid PBE User.
            IdentityUser user = await _userManager.GetUserAsync(User);

            PBEUser = await QuizUser.GetOrAddPBEUserAsync(_context, user.Email);

            if (!PBEUser.IsQuizModerator())
            {
                return(RedirectToPage("/error", new { errorMessage = "Sorry! You do not have sufficient rights to add a PBE BookList" }));
            }

            // Now let's create an empty BookList
            var emptyBookList = new QuizBookList
            {
                Created  = DateTime.Now,
                Modified = DateTime.Now
            };

            emptyBookList.BookListName = Name;
            emptyBookList.IsDeleted    = false;
            _context.QuizBookLists.Add(emptyBookList);

            // now we need to add the Books
            foreach (int BookNum in Books)
            {
                if (BookNum > 0)
                {
                    QuizBookListBookMap BookMap = new QuizBookListBookMap();
                    BookMap.BookList   = emptyBookList;
                    BookMap.BookNumber = BookNum;
                    BookMap.IsDeleted  = false;
                    BookMap.Created    = DateTime.Now;
                    BookMap.Modified   = DateTime.Now;
                    _context.QuizBookListBookMaps.Add(BookMap);
                    await _context.SaveChangesAsync();
                }
            }
            await _context.SaveChangesAsync();

            return(RedirectToPage("BookLists", new { BibleId = this.BibleId, Message = String.Format("Book List {0} successfully created...", emptyBookList.BookListName) }));
        }
        public async Task <IActionResult> OnGetAsync(string Message)
        {
            IdentityUser user = await _userManager.GetUserAsync(User);

            PBEUser = await QuizUser.GetOrAddPBEUserAsync(_context, user.Email); // Static method not requiring an instance

            if (PBEUser.IsQuestionBuilderLocked)
            {
                return(RedirectToPage("/error", new { errorMessage = "Sorry! You do not have sufficient rights to create an API token" }));
            }

            TokenString = await PBEUser.GetQuestionAPITokenAsync(_context);

            UserMessage = GetUserMessage(Message);
            return(Page());
        }
Example #6
0
        public async Task <IActionResult> OnGetAsync(string BibleId)
        {
            IdentityUser user = await _userManager.GetUserAsync(User);

            PBEUser = await QuizUser.GetOrAddPBEUserAsync(_context, user.Email);

            if (!PBEUser.IsValidPBEQuestionBuilder())
            {
                return(RedirectToPage("/error", new { errorMessage = "Sorry! You do not have sufficient rights to add a PBE Quiz Template" }));
            }

            this.BibleId = await Bible.GetValidPBEBibleIdAsync(_context, BibleId);

            ViewData["BookSelectList"] = await BibleBook.GetBookAndBookListSelectListAsync(_context, BibleId);

            ViewData["CountSelectList"] = PredefinedQuiz.GetCountSelectList();
            return(Page());
        }
Example #7
0
        public async Task <IActionResult> OnGetAsync(string BibleId)
        {
            IdentityUser user = await _userManager.GetUserAsync(User);

            PBEUser = await QuizUser.GetOrAddPBEUserAsync(_context, user.Email); // Static method not requiring an instance

            if (!PBEUser.IsQuizModerator())
            {
                return(RedirectToPage("/error", new { errorMessage = "Sorry! You do not have sufficient rights to add a PBE BookList" }));
            }

            this.BibleId = await Bible.GetValidPBEBibleIdAsync(_context, BibleId);

            //Initialize Books
            Books = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            ViewData["BookSelectList"] = await BibleBook.GetBookSelectListAsync(_context, BibleId);

            return(Page());
        }
Example #8
0
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for
        // more details see https://aka.ms/RazorPagesCRUD.
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                ViewData["BookSelectList"] = await BibleBook.GetBookAndBookListSelectListAsync(_context, BibleId);

                return(Page());
            }

            // confirm our user is a valid PBE User.
            IdentityUser user = await _userManager.GetUserAsync(User);

            PBEUser = await QuizUser.GetOrAddPBEUserAsync(_context, user.Email);

            if (!PBEUser.IsValidPBEQuestionBuilder())
            {
                return(RedirectToPage("/error", new { errorMessage = "Sorry! You do not have sufficient rights to add a PBE Quiz Template" }));
            }

            // Now let's create an empty template
            var emptyTemplate = new PredefinedQuiz
            {
                Created  = DateTime.Now,
                Modified = DateTime.Now,
                QuizUser = PBEUser
            };

            if (await TryUpdateModelAsync <PredefinedQuiz>(
                    emptyTemplate,
                    "Template", // Prefix for form value.
                    t => t.QuizName, t => t.BookNumber, t => t.NumQuestions))
            {
                emptyTemplate.QuizName  = Name; // Name is handled seperately for remote validation to work.
                emptyTemplate.IsDeleted = false;
                _context.PredefinedQuizzes.Add(emptyTemplate);
                await _context.SaveChangesAsync();

                return(RedirectToPage("./ConfigureTemplate", new { Id = emptyTemplate.Id, BibleId = this.BibleId }));
            }

            return(RedirectToPage("./Templates", new { Message = String.Format("Quiz Template {0} successfully created...", emptyTemplate.QuizName) }));
        }
        public async Task <IActionResult> OnPostAsync()
        {
            // confirm our user is a valid PBE User.
            IdentityUser user = await _userManager.GetUserAsync(User);

            PBEUser = await QuizUser.GetOrAddPBEUserAsync(_context, user.Email); // Static method not requiring an instance

            if (PBEUser.IsQuestionBuilderLocked)
            {
                return(RedirectToPage("/error", new { errorMessage = "Sorry! You do not have sufficient rights to create an API token" }));
            }

            if (!ModelState.IsValid)
            {
                return(Page());
            }

            string NewToken = await PBEUser.CreateQuestionAPITokenAsync(_context);

            return(RedirectToPage("APIToken", new { Message = String.Format("Token successfully created: {0}", NewToken) }));
        }
        public async Task <IActionResult> OnGetAsync(string BibleId, int BookNumber, int Chapter, int?VerseNum, bool?BuildQuestion)
        {
            IdentityUser user = await _userManager.GetUserAsync(User);

            PBEUser = await QuizUser.GetOrAddPBEUserAsync(_context, user.Email); // Static method not requiring an instance

            if (!PBEUser.IsValidPBEQuestionBuilder())
            {
                return(RedirectToPage("/error", new { errorMessage = "Sorry! You do not have sufficient rights to add a PBE question" }));
            }

            bool generateQuestion = false;

            if (BuildQuestion.HasValue)
            {
                generateQuestion = (bool)BuildQuestion;
            }
            Question = new QuizQuestion();
            // Setup our PBEBook Object
            Question.BibleId = await QuizQuestion.GetValidBibleIdAsync(_context, BibleId);

            if (generateQuestion)
            {
                BibleVerse verse = await BibleVerse.GetVerseAsync(_context, Question.BibleId, BookNumber, Chapter, (int)VerseNum);

                Question = await Question.BuildQuestionForVerseAsync(_context, verse, 10, Question.BibleId);

                foreach (QuizAnswer Answer in Question.QuizAnswers)
                {
                    AnswerText += Answer.Answer;
                }
            }
            else
            {
                Question.BookNumber = BookNumber;
                Question.Chapter    = Chapter;
                Question.StartVerse = VerseNum ?? 1; // set to 1 if VersNum is Null.
                Question.EndVerse   = VerseNum ?? 1; // set to 1 if VersNum is Null.
                Question.Points     = 0;
            }

            BibleBook PBEBook = await BibleBook.GetPBEBookAndChapterAsync(_context, Question.BibleId, Question.BookNumber, Question.Chapter);

            if (PBEBook == null)
            {
                return(RedirectToPage("/error", new { errorMessage = "That's Odd! We weren't able to find the PBE Book." }));
            }

            Question.PopulatePBEQuestionInfo(PBEBook);
            Question.Verses = await Question.GetBibleVersesAsync(_context, false);

            IsCommentary = (Question.Chapter == Bible.CommentaryChapter);
            if (IsCommentary == false)
            {
                ChapterQuestionCount = PBEBook.BibleChapters.Where(c => c.ChapterNumber == Question.Chapter).First().QuestionCount;
            }
            CommentaryQuestionCount = PBEBook.CommentaryQuestionCount;


            // and now we need a Verse Select List
            ViewData["VerseSelectList"]  = new SelectList(Question.Verses, "Verse", "Verse");
            ViewData["PointsSelectList"] = Question.GetPointsSelectList();
            return(Page());
        }
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for
        // more details see https://aka.ms/RazorPagesCRUD.
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                // Setup our PBEBible Object
                Question.BibleId = await QuizQuestion.GetValidBibleIdAsync(_context, Question.BibleId);

                BibleBook PBEBook = await BibleBook.GetPBEBookAndChapterAsync(_context, Question.BibleId, Question.BookNumber, Question.Chapter);

                if (PBEBook == null)
                {
                    return(RedirectToPage("/error", new { errorMessage = "That's Odd! We weren't able to find the PBE Book." }));
                }

                Question.PopulatePBEQuestionInfo(PBEBook);
                Question.Verses = await Question.GetBibleVersesAsync(_context, false);


                IsCommentary = (Question.Chapter == Bible.CommentaryChapter);
                if (IsCommentary == false)
                {
                    ChapterQuestionCount = PBEBook.BibleChapters.Where(c => c.ChapterNumber == Question.Chapter).First().QuestionCount;
                }
                CommentaryQuestionCount = PBEBook.CommentaryQuestionCount;

                // and now we need a Verse and Points Select List
                ViewData["VerseSelectList"]  = new SelectList(Question.Verses, "Verse", "Verse");
                ViewData["PointsSelectList"] = Question.GetPointsSelectList();
                return(Page());
            }

            // confirm our user is a valid PBE User.
            IdentityUser user = await _userManager.GetUserAsync(User);

            if (User != null)
            {
                PBEUser = await QuizUser.GetOrAddPBEUserAsync(_context, user.Email);

                if (!PBEUser.IsValidPBEQuestionBuilder())
                {
                    return(RedirectToPage("/error", new { errorMessage = "Sorry! You do not have sufficient rights to add a PBE question" }));
                }
            }
            else
            {
                return(RedirectToPage("/error", new { errorMessage = "Oops! We were unable to get our User Object from the UserManager, this question cannot be added!" }));
            }

            // Now let's create an empty question and put only our validated properties onto it.
            var emptyQuestion = new QuizQuestion
            {
                Created  = DateTime.Now,
                Modified = DateTime.Now
            };

            if (await TryUpdateModelAsync <QuizQuestion>(
                    emptyQuestion,
                    "Question", // Prefix for form value.
                    Q => Q.BibleId, Q => Q.Points, Q => Q.BookNumber, Q => Q.Chapter, Q => Q.StartVerse, Q => Q.EndVerse, Q => Q.Question))
            {
                emptyQuestion.Owner  = PBEUser.Email;
                emptyQuestion.Source = "BiblePaths.Net";
                _context.QuizQuestions.Add(emptyQuestion);

                // now we need to add the Answer if there is one.
                if (AnswerText.Length > 0)
                {
                    QuizAnswer Answer = new QuizAnswer
                    {
                        Created   = DateTime.Now,
                        Modified  = DateTime.Now,
                        Question  = emptyQuestion,
                        Answer    = AnswerText,
                        IsPrimary = true
                    };
                    _context.QuizAnswers.Add(Answer);
                    // Register that this question has an answer.
                    emptyQuestion.IsAnswered = true;
                }
                await _context.SaveChangesAsync();

                return(RedirectToPage("AddQuestion", new { BibleId = emptyQuestion.BibleId, BookNumber = emptyQuestion.BookNumber, Chapter = emptyQuestion.Chapter, VerseNum = emptyQuestion.EndVerse }));
            }
            else
            {
                return(RedirectToPage("/error", new { errorMessage = "Oops! We failed to update the question model, this question cannot be added!" }));
            }
            // return RedirectToPage("Index");
        }
        public async Task <IActionResult> OnGetAsync(string BibleId, int QuizId, string Message)
        {
            IdentityUser user = await _userManager.GetUserAsync(User);

            PBEUser = await QuizUser.GetOrAddPBEUserAsync(_context, user.Email); // Static method not requiring an instance

            if (!PBEUser.IsValidPBEQuizHost())
            {
                return(RedirectToPage("/error", new { errorMessage = "Sorry! You do not have sufficient rights to host a PBE Quiz" }));
            }
            this.BibleId = await Bible.GetValidPBEBibleIdAsync(_context, BibleId);

            // Let's grab the Quiz Object
            Quiz = await _context.QuizGroupStats.FindAsync(QuizId);

            if (Quiz == null)
            {
                return(RedirectToPage("/error", new { errorMessage = "That's Odd... We were unable to find this Quiz" }));
            }
            if (Quiz.QuizUser != PBEUser)
            {
                return(RedirectToPage("/error", new { errorMessage = "Sorry! Only a Quiz Owner can run a Quiz" }));
            }
            _ = await Quiz.AddQuizPropertiesAsync(_context, BibleId);

            // Now for the Question Object... we're going to take 3 swings at this for the scenario where we don't have enough questions.
            int iterations = 0;

            do
            {
                Question = await Quiz.GetNextQuizQuestionAsync(_context, BibleId);

                iterations++;
            }while (iterations < 3 && Question.QuestionSelected == false);

            // This is the no questions found scenario
            if (Question.QuestionSelected == false)
            {
                return(RedirectToPage("/error", new { errorMessage = "Sorry! We failed to find a random question after three tries... please add more questions." }));
            }

            if (Question.BibleId == null)
            {
                Question.BibleId = BibleId;
            }

            BibleBook PBEBook = await BibleBook.GetPBEBookAndChapterAsync(_context, BibleId, Question.BookNumber, Question.Chapter);

            if (PBEBook == null)
            {
                return(RedirectToPage("/error", new { errorMessage = "That's Odd! We weren't able to find the PBE Book." }));
            }

            Question.PopulatePBEQuestionInfo(PBEBook);
            Question.Verses = await Question.GetBibleVersesAsync(_context, true);

            Question.LegalNote = Question.GetBibleLegalNote();

            // Build our Select List and set a default points value of -1 to require selection.
            ViewData["PointsSelectList"] = Question.GetQuestionPointsSelectList();
            Question.PointsAwarded       = -1;

            UserMessage = GetUserMessage(Message);
            return(Page());
        }
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for
        // more details see https://aka.ms/RazorPagesCRUD.
        public async Task <IActionResult> OnPostAsync(string BibleId, int QuizId)
        {
            if (!ModelState.IsValid)
            {
                // Something bad has happened let's go get a new quiz question.
                UserMessage = "Something has gone wrong! We were unable to save the results for that last question";
                return(RedirectToPage("Quiz", new { BibleId, QuizId, Message = UserMessage }));
            }

            IdentityUser user = await _userManager.GetUserAsync(User);

            PBEUser = await QuizUser.GetOrAddPBEUserAsync(_context, user.Email); // Static method not requiring an instance

            if (!PBEUser.IsValidPBEQuizHost())
            {
                return(RedirectToPage("/error", new { errorMessage = "Sorry! You do not have sufficient rights to host a PBE Quiz" }));
            }
            this.BibleId = await Bible.GetValidPBEBibleIdAsync(_context, BibleId);

            // Let's grab the Quiz Object in order to update it.
            Quiz = await _context.QuizGroupStats.FindAsync(QuizId);

            if (Quiz == null)
            {
                return(RedirectToPage("/error", new { errorMessage = "That's Odd... We were unable to find this Quiz" }));
            }
            if (Quiz.QuizUser != PBEUser)
            {
                return(RedirectToPage("/error", new { errorMessage = "Sorry! Only a Quiz Owner can award points during a Quiz" }));
            }

            // We need to upate the Question object as well so let's go grab it.
            QuizQuestion QuestionToUpdate = await _context.QuizQuestions.FindAsync(Question.Id);

            if (QuestionToUpdate == null)
            {
                return(RedirectToPage("/error", new { errorMessage = "That's Odd... We were unable to find this Question so we can't update it" }));
            }

            // Now we award the points... let's get this right:
            // Let's prevent posting an anomalous number of points.
            int QuestionPointsPossible = QuestionToUpdate.Points;

            if (Question.PointsAwarded > QuestionPointsPossible)
            {
                Question.PointsAwarded = QuestionPointsPossible;
            }
            if (Question.PointsAwarded < 0)
            {
                Question.PointsAwarded = 0;
            }

            // Update the Quiz Object:
            _context.Attach(Quiz);
            Quiz.PointsPossible += QuestionPointsPossible;
            Quiz.PointsAwarded  += Question.PointsAwarded;
            Quiz.QuestionsAsked += 1;
            Quiz.Modified        = DateTime.Now;

            // Update the Question Object
            _context.Attach(QuestionToUpdate);
            QuestionToUpdate.LastAsked = DateTime.Now;

            // We've had some challenges with users challenging many questions often with no comment.
            // We will do a user check and make sure our user isn't blocked, if they are we silently fail the challenge.
            // TODO: We should revisit the silent fail if it becomes a problem.
            if (Question.Challenged && !PBEUser.IsQuestionBuilderLocked)
            {
                QuestionToUpdate.Challenged       = true;
                QuestionToUpdate.ChallengeComment = Question.ChallengeComment;
                QuestionToUpdate.ChallengedBy     = PBEUser.Email;
            }

            // Save both of these changes.
            await _context.SaveChangesAsync();

            // And next let's make sure we log this event.
            // BUG: Note we've had a pretty significant data bug prior to 6/8/2019 where we were setting Points to the cumulative quizGroupStat.PointsAwarded vs. the non-cumulative PointsAwardedByJudge... so all data prior to this date is wrong.
            await QuestionToUpdate.RegisterEventAsync(_context, QuestionEventType.QuestionPointsAwarded, PBEUser.Id, null, Quiz.Id, Question.PointsAwarded);

            return(RedirectToPage("Quiz", new { BibleId, QuizId }));
        }
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for
        // more details see https://aka.ms/RazorPagesCRUD.
        public async Task <IActionResult> OnPostAsync(int Id)
        {
            // confirm our user is a valid PBE User.
            IdentityUser user = await _userManager.GetUserAsync(User);

            PBEUser = await QuizUser.GetOrAddPBEUserAsync(_context, user.Email);

            if (!PBEUser.IsQuizModerator())
            {
                return(RedirectToPage("/error", new { errorMessage = "Sorry! You do not have sufficient rights to edit a PBE BookList" }));
            }

            QuizBookList BookListToUpdate = await _context.QuizBookLists.FindAsync(Id);

            if (BookListToUpdate == null)
            {
                return(RedirectToPage("/error", new { errorMessage = "That's Odd! We weren't able to find this Book List" }));
            }
            await _context.Entry(BookListToUpdate).Collection(L => L.QuizBookListBookMaps).LoadAsync();

            // We need a copy of the BookListMap to compare to while the origonal is being updated.
            List <QuizBookListBookMap> CompareMap = BookListToUpdate.QuizBookListBookMaps.ToList();

            BibleId = await Bible.GetValidPBEBibleIdAsync(_context, BibleId);

            // Is this an attempted name change... for reals?
            if (Name.ToLower() != BookList.BookListName.ToLower())
            {
                if (await QuizBookList.ListNameAlreadyExistsStaticAsync(_context, Name))
                {
                    ModelState.AddModelError("Name", "Sorry, this Name is already in use.");
                }
                // Update the name since this is a rename attempt.
                BookListToUpdate.BookListName = Name;
            }

            if (!ModelState.IsValid)
            {
                BookList.PadBookListBookMapsForEdit();
                Books = BookList.QuizBookListBookMaps.ToList();
                Name  = BookList.BookListName;
                ViewData["BookSelectList"] = await BibleBook.GetBookSelectListAsync(_context, BibleId);

                return(Page());
            }

            _context.Attach(BookListToUpdate);
            BookListToUpdate.Modified = DateTime.Now;

            // now we need to update the books
            foreach (QuizBookListBookMap Book in Books)
            {
                // See if this is one of our existing BookMaps
                bool ExistingBookMap             = true;
                QuizBookListBookMap OriginalBook = new QuizBookListBookMap();
                try
                {
                    OriginalBook = CompareMap.Where(B => B.Id == Book.Id).Single();
                }
                catch
                {
                    ExistingBookMap = false;
                    // New Book Scenario let's add the book.
                    // But first we won't add it if the Book is already in the List.
                    if (BookListToUpdate.QuizBookListBookMaps.Where(B => B.BookNumber == Book.BookNumber).Any())
                    {
                        // Dupe scenario, don't add
                    }
                    else
                    {
                        if (Book.BookNumber > 0)
                        {
                            QuizBookListBookMap BookToAdd = new QuizBookListBookMap();
                            BookToAdd.BookList   = BookListToUpdate;
                            BookToAdd.IsDeleted  = false;
                            BookToAdd.Created    = DateTime.Now;
                            BookToAdd.Modified   = DateTime.Now;
                            BookToAdd.BookNumber = Book.BookNumber;
                            _context.QuizBookListBookMaps.Add(BookToAdd);
                            //await _context.SaveChangesAsync();
                        }
                    }
                }
                if (ExistingBookMap)
                {
                    // This is the update BookMap scenario
                    if (OriginalBook.BookNumber != Book.BookNumber)
                    {
                        if (Book.BookNumber != 0)
                        {
                            _context.Attach(OriginalBook);
                            OriginalBook.Modified   = DateTime.Now;
                            OriginalBook.BookNumber = Book.BookNumber;
                            // await _context.SaveChangesAsync();
                        }
                        else
                        {
                            // This is the delete BookMap scenario
                            _context.QuizBookListBookMaps.Remove(OriginalBook);
                            // await _context.SaveChangesAsync();
                        }
                    }
                }
            }
            await _context.SaveChangesAsync();

            return(RedirectToPage("BookLists", new { BibleId = this.BibleId, Message = String.Format("Book List {0} successfully updated...", BookListToUpdate.BookListName) }));
        }
Example #15
0
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for
        // more details see https://aka.ms/RazorPagesCRUD.
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                // Setup our PBEBible Object
                Question.BibleId = await QuizQuestion.GetValidBibleIdAsync(_context, Question.BibleId);

                BibleBook PBEBook = await BibleBook.GetPBEBookAndChapterAsync(_context, Question.BibleId, Question.BookNumber, Question.Chapter);

                if (PBEBook == null)
                {
                    return(RedirectToPage("/error", new { errorMessage = "That's Odd! We weren't able to find the PBE Book." }));
                }

                Question.PopulatePBEQuestionInfo(PBEBook);
                Question.Verses = await Question.GetBibleVersesAsync(_context, false);

                // We should still have AnswerText


                IsCommentary = (Question.Chapter == Bible.CommentaryChapter);
                if (IsCommentary == false)
                {
                    ChapterQuestionCount = PBEBook.BibleChapters.Where(c => c.ChapterNumber == Question.Chapter).First().QuestionCount;
                }
                CommentaryQuestionCount = PBEBook.CommentaryQuestionCount;

                // and now we need a Verse and Points Select List
                ViewData["VerseSelectList"]  = new SelectList(Question.Verses, "Verse", "Verse");
                ViewData["PointsSelectList"] = Question.GetPointsSelectList();
                return(Page());
            }

            // confirm our user is a valid PBE User.
            IdentityUser user = await _userManager.GetUserAsync(User);

            PBEUser = await QuizUser.GetOrAddPBEUserAsync(_context, user.Email);

            if (!PBEUser.IsValidPBEQuestionBuilder())
            {
                return(RedirectToPage("/error", new { errorMessage = "Sorry! You do not have sufficient rights to edit a PBE question" }));
            }

            // Now let's create an empty question and put only our validated properties onto it.
            QuizQuestion QuestionToUpdate = await _context.QuizQuestions.FindAsync(Question.Id);

            if (QuestionToUpdate == null)
            {
                return(RedirectToPage("/error", new { errorMessage = "That's Odd! We weren't able to find this Question" }));
            }

            if (await TryUpdateModelAsync <QuizQuestion>(
                    QuestionToUpdate,
                    "Question", // Prefix for form value.
                    Q => Q.BibleId, Q => Q.Points, Q => Q.StartVerse, Q => Q.EndVerse, Q => Q.Question, Q => Q.Challenged, Q => Q.ChallengeComment))
            {
                QuestionToUpdate.Modified = DateTime.Now;

                // now we need to add the Answer if there is one.
                if (AnswerText.Length > 0)
                {
                    // We need the Original Answer and while techincally we support multiple Answers
                    // we are only going to allow operating on the first one in this basic edit experience.
                    await _context.Entry(QuestionToUpdate).Collection(Q => Q.QuizAnswers).LoadAsync();

                    if (QuestionToUpdate.QuizAnswers.Count > 0)
                    {
                        QuizAnswer OriginalAnswer = QuestionToUpdate.QuizAnswers.OrderBy(A => A.Id).First();
                        if (OriginalAnswer.Answer != AnswerText)
                        {
                            _context.Attach(OriginalAnswer);
                            OriginalAnswer.Modified = DateTime.Now;
                            OriginalAnswer.Answer   = AnswerText;

                            QuestionToUpdate.IsAnswered = true;
                        }
                    }
                }
                await _context.SaveChangesAsync();

                switch (ReturnPath)
                {
                case "Questions":
                    return(RedirectToPage("Questions", new { BibleId = QuestionToUpdate.BibleId, BookNumber = QuestionToUpdate.BookNumber, Chapter = QuestionToUpdate.Chapter }));

                // break; not needed unreachable

                case "ChallengedQuestions":
                    return(RedirectToPage("ChallengedQuestions", new { BibleId = QuestionToUpdate.BibleId }));

                // break; not needed unreachable

                default:
                    return(RedirectToPage("AddQuestion", new { BibleId = QuestionToUpdate.BibleId, BookNumber = QuestionToUpdate.BookNumber, Chapter = QuestionToUpdate.Chapter, VerseNum = QuestionToUpdate.EndVerse }));
                    // break; not needed unreachable
                }
            }
            return(RedirectToPage("Index"));
        }