public static async Task <Subject> AddAsync(Guid subjectId, Guid studentId) { using (var unitOfWork = RepoUnitOfWork.NewTracking <StudentSubjectCore>()) { var subjectRepository = unitOfWork.Repository <DataLayer.SubjectRepository>(); var studentRepository = unitOfWork.Repository <StudentRepository>(); var subject = await subjectRepository.GetAsync(subjectId, new[] { nameof(Subject.Students) }).ConfigureAwait(false); if (subject == null) { unitOfWork.RollbackTransaction(); return(null); } if (subject.Students.Any(stud => stud.Id == subjectId)) { return(subject.CopyTo <Subject>()); } var student = await studentRepository.GetAsync(studentId).ConfigureAwait(false); if (student == null) { unitOfWork.RollbackTransaction(); return(null); } subject.Students.Add(student); var updatedSubject = await subjectRepository.UpdateAsync(subject, true).ConfigureAwait(false); if (updatedSubject == null) { unitOfWork.RollbackTransaction(); return(null); } unitOfWork.CommitTransaction(); return(updatedSubject.CopyTo <Subject>()); } }
public static async Task <Group> AddAsync(Guid groupId, Guid studentId) { using (var unitOfWork = RepoUnitOfWork.NewTracking <StudentGroupCore>()) { var groupRepository = unitOfWork.Repository <DataLayer.GroupRepository>(); var studentRepository = unitOfWork.Repository <StudentRepository>(); var group = await groupRepository.GetAsync(groupId, new[] { nameof(Group.Students) }).ConfigureAwait(false); if (group == null) { unitOfWork.RollbackTransaction(); return(null); } if (group.Students.Any(stud => stud.Id == groupId)) { return(group.CopyTo <Group>()); } var student = await studentRepository.GetAsync(studentId).ConfigureAwait(false); if (student == null) { unitOfWork.RollbackTransaction(); return(null); } group.Students.Add(student); var updatedGroup = await groupRepository.UpdateAsync(group, true).ConfigureAwait(false); if (updatedGroup == null) { unitOfWork.RollbackTransaction(); return(null); } unitOfWork.CommitTransaction(); return(updatedGroup.CopyTo <Group>()); } }