public GroupVM DetachSubjectFromGroup(AttachDetachSubjectWithGroupDTO detachSubjectWithGroupDTO) { if (detachSubjectWithGroupDTO == null) { throw new ArgumentNullException($"DTO is null"); } var subjectGroup = _dbContext.SubjectGroups.FirstOrDefault(sg => sg.GroupId == detachSubjectWithGroupDTO.GroupId && sg.SubjectId == detachSubjectWithGroupDTO.SubjectId); if (subjectGroup == null) { throw new ArgumentNullException($"This subject is not attached with this group"); } _dbContext.SubjectGroups.Remove(subjectGroup); _dbContext.Remove(subjectGroup); _dbContext.SaveChanges(); var group = _dbContext.Groups.FirstOrDefault(x => x.Id == detachSubjectWithGroupDTO.GroupId); var groupVM = _mapper.Map <GroupVM>(group); return(groupVM); }
public IActionResult DetachSubjectFromGroup(AttachDetachSubjectWithGroupDTO attachDetachSubjectWithGroupDTO) { if (ModelState.IsValid) { try { var groupVM = _subjectGroupService.DetachSubjectFromGroup(attachDetachSubjectWithGroupDTO); if (groupVM != null) { return(RedirectToAction("Details", "Group", new { id = attachDetachSubjectWithGroupDTO.GroupId })); } } catch (Exception) { return(View("Error")); } } return(View()); }
public GroupVM AttachSubjectWithGroup(AttachDetachSubjectWithGroupDTO attachSubjectWithGroupDTO) { if (attachSubjectWithGroupDTO == null) { throw new ArgumentNullException($"DTO is null"); } var subjectGroup = _dbContext.SubjectGroups.FirstOrDefault(sg => sg.GroupId == attachSubjectWithGroupDTO.GroupId && sg.SubjectId == attachSubjectWithGroupDTO.SubjectId); if (subjectGroup != null) { throw new ArgumentNullException($"Subject is already attached to this group"); } subjectGroup = new SubjectGroup { GroupId = attachSubjectWithGroupDTO.GroupId, SubjectId = attachSubjectWithGroupDTO.SubjectId, }; _dbContext.SubjectGroups.Add(subjectGroup); _dbContext.SaveChanges(); var group = _dbContext.Groups.FirstOrDefault(x => x.Id == attachSubjectWithGroupDTO.GroupId); var groupVM = _mapper.Map <GroupVM>(group); return(groupVM); }