public StudentVM DetachStudentFromGroup(AttachDetachStudentWithGroupDTO detachStudentWithGroupDTO) { if (detachStudentWithGroupDTO == null) { throw new ArgumentNullException($"DTO is null"); } var student = _dbContext.Users.OfType <Student>().FirstOrDefault(t => t.Id == detachStudentWithGroupDTO.StudentId); if (student == null || !_userManager.IsInRoleAsync(student, "Student").Result) { throw new ArgumentNullException($"Student is null or user is not a student"); } var group = _dbContext.Groups.FirstOrDefault(x => x.Id == detachStudentWithGroupDTO.GroupId); if (group == null) { throw new ArgumentNullException($"Group is null"); } student.GroupId = null; student.Group = null; group.Students.Remove(student); _dbContext.SaveChanges(); var studentVm = _mapper.Map <StudentVM>(student); return(studentVm); }
public IActionResult DetachStudentFromGroup(AttachDetachStudentWithGroupDTO attachDetachStudentWithGroupDTO) { if (ModelState.IsValid) { try { var studentVM = _studentGroupService.DetachStudentFromGroup(attachDetachStudentWithGroupDTO); if (studentVM != null) { return(RedirectToAction("Details", "Group", new { id = attachDetachStudentWithGroupDTO.GroupId })); } } catch (Exception) { return(View("Error")); } } return(View()); }