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()); }
// 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 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()); }
// 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) })); }