public GroupVm AttachSubjectToGroup(AttachDetachSubjectGroupDto attachSubjectGroup) { if (attachSubjectGroup == null) { throw new ArgumentNullException($"Dto of type is null"); } var subjectGroup = _dbContext.SubjectGroup.FirstOrDefault(sg => sg.GroupId == attachSubjectGroup.GroupId && sg.SubjectId == attachSubjectGroup.SubjectId); if (subjectGroup != null) { throw new ArgumentNullException($"There is such attachment already defined."); } subjectGroup = new SubjectGroup { GroupId = attachSubjectGroup.GroupId, SubjectId = attachSubjectGroup.SubjectId }; _dbContext.SubjectGroup.Add(subjectGroup); _dbContext.SaveChanges(); var group = _dbContext.Groups.FirstOrDefault(x => x.Id == attachSubjectGroup.GroupId); var groupVm = Mapper.Map <GroupVm>(group); return(groupVm); }
public IActionResult AttachSubjectToGroup(AttachDetachSubjectGroupDto attachDetachSubjectGroupDto) { try { if (!ModelState.IsValid) { return(View()); } _groupService.AttachSubjectToGroup(attachDetachSubjectGroupDto); return(RedirectToAction("Index", "Subject")); } catch (Exception ex) { ModelState.AddModelError(string.Empty, ex.Message); return(AttachDetachSubjectToGroupGetView()); } }
public GroupVm DetachSubjectFromGroup(AttachDetachSubjectGroupDto detachDetachSubject) { if (detachDetachSubject == null) { throw new ArgumentNullException($"Dto of type is null"); } var subjectGroup = _dbContext.SubjectGroup.FirstOrDefault(sg => sg.GroupId == detachDetachSubject.GroupId && sg.SubjectId == detachDetachSubject.SubjectId); if (subjectGroup == null) { throw new ArgumentNullException($"The is no such attachment between group and subject"); } _dbContext.SubjectGroup.Remove(subjectGroup); _dbContext.Remove(subjectGroup); _dbContext.SaveChanges(); var group = _dbContext.Groups.FirstOrDefault(x => x.Id == detachDetachSubject.GroupId); var groupVm = Mapper.Map <GroupVm>(group); return(groupVm); }