public async Task <IActionResult> CreateSpellingGroup(SpellingGroupViewModel SpellingGroupVM) { if (ModelState.IsValid) { SpellingGroup newList = new SpellingGroup() { Name = SpellingGroupVM.Name, Description = SpellingGroupVM.Description, TeacherUsername = User.Identity.Name }; try { await _context.SpellingGroups.AddAsync(newList); await _context.SaveChangesAsync(); ViewData["message"] = $"You have successfully created the Spelling Group named {newList.Name}"; } catch (Exception ex) // Make a log to enter this Exception ex { ViewData["message"] = "There was a problem updating the database. Please try again."; } } return(View()); }
// GET: Student/ViewLists public async Task <IActionResult> ViewLists(int StudentId, int?SpellingListId) { ViewListsViewModel viewListsVM = new ViewListsViewModel(); viewListsVM.Student = await _context.Students.SingleOrDefaultAsync(s => s.StudentId == StudentId); // Get all group allocations for the student and add the group to a list List <StudentGroupAllocation> groupAllocations = await _context.StudentGroupAllocations.Where(a => a.StudentId == viewListsVM.Student.StudentId).ToListAsync(); List <SpellingGroup> studentGroups = new List <SpellingGroup>(); foreach (var allocation in groupAllocations) { SpellingGroup group = await _context.SpellingGroups.SingleOrDefaultAsync(g => g.SpellingGroupId == allocation.SpellingGroupId); studentGroups.Add(group); } viewListsVM.SpellingLists = new List <SpellingList>(); // Assign all spelling lists for each group the student is in to the View Model List of Spelling Lists foreach (var group in studentGroups) { List <ListGroupAllocation> listAllocations = await _context.ListGroupAllocations.Where(a => a.SpellingGroupId == group.SpellingGroupId).ToListAsync(); foreach (var a in listAllocations) { SpellingList list = await _context.SpellingLists.SingleOrDefaultAsync(l => l.SpellingListId == a.SpellingListId); viewListsVM.SpellingLists.Add(list); } } // Assing a spelling list to the active list if (SpellingListId == null) { viewListsVM.ActiveSpellingList = viewListsVM.SpellingLists.First(); } else { viewListsVM.ActiveSpellingList = viewListsVM.SpellingLists.SingleOrDefault(l => l.SpellingListId == SpellingListId); } // Get all the words associated with the Spelling list List <WordListAllocation> wordAllocations = await _context.WordListAllocations.Where(a => a.SpellingListId == viewListsVM.ActiveSpellingList.SpellingListId).ToListAsync(); viewListsVM.SpellingWords = new List <SpellingWord>(); foreach (var a in wordAllocations) { SpellingWord word = await _context.SpellingWords.SingleOrDefaultAsync(w => w.Id == a.SpellingWordId); viewListsVM.SpellingWords.Add(word); } return(View(viewListsVM)); }
public async Task <IActionResult> RemoveFromGroup(int Id, int GroupId) { Student student = await _context.Students.SingleOrDefaultAsync(s => s.StudentId == Id); SpellingGroup group = await _context.SpellingGroups.SingleOrDefaultAsync(g => g.SpellingGroupId == GroupId); var allocation = await _context.StudentGroupAllocations.SingleOrDefaultAsync(a => a.Student == student && a.SpellingGroup == group); _context.StudentGroupAllocations.Remove(allocation); await _context.SaveChangesAsync(); return(RedirectToAction("AssignStudents", new { SpellingGroupId = group.SpellingGroupId })); }
public async Task <IActionResult> AddStudentToGroup(int Id, int GroupId) { Student student = await _context.Students.SingleOrDefaultAsync(s => s.StudentId == Id); SpellingGroup group = await _context.SpellingGroups.SingleOrDefaultAsync(g => g.SpellingGroupId == GroupId); StudentGroupAllocation allocation = new StudentGroupAllocation() { Student = student, SpellingGroup = group }; await _context.StudentGroupAllocations.AddAsync(allocation); await _context.SaveChangesAsync(); return(RedirectToAction("AssignStudents", new { SpellingGroupId = group.SpellingGroupId })); }