/* #TAG:CoreDocs */ public async Task <IEnumerable <SchoolDepartment> > GetSchoolDepartmentsAsync(string accountFromHeader, List <string> departmentsCodes = null, int size = 0) { var schoolDepartments = new List <SchoolDepartment>(); var filter = departmentsCodes != null && departmentsCodes.Count() > 0 ? Tools.GetCriteria(departmentsCodes) + "|''" : string.Empty; var departments = await GetDepartmentById(accountFromHeader, filter, size); var mainSchool = new SchoolDepartment(); foreach (var dep in departments) { if (dep.Primary_Key == null) { mainSchool.School = dep; continue; } if (!dep.Apply_School_Data_on_Reports) { var schoolDep = new SchoolDepartment(); schoolDep.School = dep; schoolDep.Departments.Add(dep); schoolDepartments.Add(schoolDep); } else { mainSchool.Departments.Add(dep); } } if (mainSchool.School != null && mainSchool.Departments.Any()) { schoolDepartments.Add(mainSchool); } return(schoolDepartments); }
public SchoolDepartment CreateSchoolDepartment(SchoolDepartment schoolDepartment) { return(_DbContext.SchoolDepartments.Add(schoolDepartment)); }
public void SchoolRegister(SchoolDTO schoolDto, IEnumerable <DepartmentDTO> departmentDTOs, IEnumerable <CourseDTO> courseDTOs, IEnumerable <TeacherDTO> teacherDTOs) { using (DatabaseContext context = new DatabaseContext()) { using (DbContextTransaction transaction = context.Database.BeginTransaction()) { try { schoolRegServ = new SchoolRegistrationService(context); var foundSchool = schoolRegServ.FindSchool(schoolDto.SchoolName); if (foundSchool != null) { throw new SchoolRegistrationException(); } if (foundSchool == null) { var newSchool = new School { Name = schoolDto.SchoolName, ContactEmail = schoolDto.SchoolContactEmail, EmailDomain = schoolDto.SchoolEmailDomain }; foundSchool = schoolRegServ.CreateSchool(newSchool); context.SaveChanges(); } foreach (DepartmentDTO d in departmentDTOs) { var foundDepartment = schoolRegServ.FindDepartment(d.DepartmentName); if (foundDepartment == null) { var newDepartment = new Department { Name = d.DepartmentName }; foundDepartment = schoolRegServ.CreateDepartment(newDepartment); context.SaveChanges(); } var foundSchoolDepartment = schoolRegServ.FindSchoolDepartment(foundSchool.Name, foundDepartment.Name); if (foundSchoolDepartment == null) { var newSchoolDepartment = new SchoolDepartment { School = foundSchool, Department = foundDepartment, }; foundSchoolDepartment = schoolRegServ.CreateSchoolDepartment(newSchoolDepartment); context.SaveChanges(); } foreach (TeacherDTO t in teacherDTOs) { var foundTeacher = schoolRegServ.FindTeacher(t.FirstName, t.LastName); if (t.DepartmentName.Equals(foundDepartment.Name)) { if (foundTeacher == null) { var newTeacher = new Teacher(t.FirstName, t.LastName); foundTeacher = schoolRegServ.CreateTeacher(newTeacher); context.SaveChanges(); } SchoolTeacher foundSchoolTeacher = schoolRegServ.FindSchoolTeacher(foundSchool.Name, foundDepartment.Name, foundTeacher.FirstName, foundTeacher.LastName); if (foundSchoolTeacher == null) { var newSchoolTeacher = new SchoolTeacher { Teacher = foundTeacher, SchoolDepartment = foundSchoolDepartment }; foundSchoolTeacher = schoolRegServ.CreateSchoolTeacher(newSchoolTeacher); context.SaveChanges(); } foreach (CourseDTO c in courseDTOs) { if (c.DepartmentName.Equals(foundDepartment.Name) && c.TeacherFirstName.Equals(foundTeacher.FirstName) && c.TeacherLastName.Equals(foundTeacher.LastName)) { var foundCourse = schoolRegServ.FindCourse(c.CourseName); if (foundCourse == null) { var newCourse = new Course { Name = c.CourseName, SchoolDepartment = foundSchoolDepartment }; foundCourse = schoolRegServ.CreateCourse(newCourse); context.SaveChanges(); } SchoolTeacherCourse foundSchoolTeacherCourse = schoolRegServ.FindSchoolTeacherCourse(foundSchool.Name, foundDepartment.Name, foundTeacher.FirstName, foundTeacher.LastName, foundCourse.Name); if (foundSchoolTeacherCourse == null) { var newSchoolTeacherCourse = new SchoolTeacherCourse { SchoolTeacher = foundSchoolTeacher, Course = foundCourse }; schoolRegServ.CreateSchoolTeacherCourse(newSchoolTeacherCourse); context.SaveChanges(); } } } } } } transaction.Commit(); } catch (SchoolRegistrationException schoolEX) { throw schoolEX; } catch (Exception ex) { transaction.Rollback(); } } } }