public GroupVm DeleteSubjectToGroup(AddOrDeleteSubjectToGroupDto deleteSubjectToGroupDto) { if (deleteSubjectToGroupDto == null) { throw new ArgumentNullException($"Dto of type is null"); } var subjectGroup = _dbContext.SubjectGroup.FirstOrDefault(sg => sg.GroupId == deleteSubjectToGroupDto.GroupId && sg.SubjectId == deleteSubjectToGroupDto.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 == deleteSubjectToGroupDto.GroupId); var groupVm = Mapper.Map <GroupVm>(group); return(groupVm); }
public GroupVm AddSubjectToGroup(AddOrDeleteSubjectToGroupDto addSubjectToGroupDto) { if (addSubjectToGroupDto == null) { throw new ArgumentNullException($"Dto of type is null"); } var subjectGroup = _dbContext.SubjectGroup.FirstOrDefault(sg => sg.GroupId == addSubjectToGroupDto.GroupId && sg.SubjectId == addSubjectToGroupDto.SubjectId); if (subjectGroup != null) { throw new ArgumentNullException($"There is such attachment already defined."); } subjectGroup = new SubjectGroup { GroupId = addSubjectToGroupDto.GroupId, SubjectId = addSubjectToGroupDto.SubjectId }; _dbContext.SubjectGroup.Add(subjectGroup); _dbContext.SaveChanges(); var group = _dbContext.Groups.FirstOrDefault(x => x.Id == addSubjectToGroupDto.GroupId); var groupVm = Mapper.Map <GroupVm>(group); return(groupVm); }