예제 #1
0
        public async Task <int> Add(Lecturer lecturer)
        {
            var l = new Lecturer
            {
                Name = lecturer.Name
            };

            _context.Lecturers.Add(l);
            await SaveChangesAsync();

            var newStudents      = lecturer.LecturerStudents.Select(ls => ls.Student).Where(s => s.Id == 0).ToList();
            var existingStudents = lecturer.LecturerStudents.Select(ls => ls.Student).Where(s => s.Id != 0).ToList();
            var newStudentIds    = await _studentService.AddMany(newStudents);

            var existingStudentIds = await _studentService.UpdateMany(existingStudents);

            foreach (var studentId in newStudentIds.Union(existingStudentIds))
            {
                _context.LecturerStudents.Add(new LecturerStudent
                {
                    LecturerId = l.Id,
                    StudentId  = studentId
                });
            }
            await SaveChangesAsync();

            return(l.Id);
        }