public School GetSchool(int id) { Domain.School school = _context.Schools.Include(s => s.Principal).FirstOrDefault(s => s.Id == id); // alternative solution: /*Domain.Principal principal = _context.Principals.FirstOrDefault(p => p.SchoolId == school.Id); * * school.Principal = principal;*/ return(_mapper.Map <School>(school)); }
public void UpdateSchool(School school, SchoolDTO schoolDTO) { Domain.Principal updatePrincipal = _context.Principals.FirstOrDefault(s => s.Id == school.Principal.Id); updatePrincipal.Name = schoolDTO.Principal.Name; updatePrincipal.Photo = schoolDTO.Principal.Photo; updatePrincipal.BirthDate = schoolDTO.Principal.BirthDate; _context.SaveChanges(); Domain.School updateSchool = _context.Schools.FirstOrDefault(s => s.Id == school.Id); updateSchool.Name = schoolDTO.Name; updateSchool.Photo = schoolDTO.Photo; _context.SaveChanges(); }
public School AddSchool(School school) { Domain.Principal newPrincipal = _mapper.Map <Domain.Principal>(school.Principal); Domain.School newSchool = new Domain.School() { Name = school.Name, Photo = school.Photo }; _context.Schools.Add(newSchool); _context.SaveChanges(); newPrincipal.SchoolId = newSchool.Id; _context.Principals.Add(newPrincipal); _context.SaveChanges(); school.Id = newSchool.Id; return(school); }
public void DeleteSchool(School school) { // delete principal Domain.Principal deletePrincipal = _context.Principals.FirstOrDefault(s => s.Id == school.Principal.Id); _context.Remove(deletePrincipal); _context.SaveChanges(); // select catalogues List <Domain.Catalogue> deleteCatalogues = _context.Catalogues .Include(c => c.CourseCatalogues).Include(c => c.MentorCatalogues) .Where(c => c.SchoolId == school.Id).ToList(); //A. delete catalogues grades List <Domain.Grade> deleteGrades = new List <Domain.Grade>(); foreach (Domain.Catalogue catalogue in deleteCatalogues) { List <Domain.Grade> oneCatalogueGrades = _context.Grades .Where(g => g.CatalogueId == catalogue.Id).ToList(); deleteGrades.AddRange(oneCatalogueGrades); } _context.Grades.RemoveRange(deleteGrades); _context.SaveChanges(); // select courses List <Domain.Course> deleteCourses = _context.Courses.Where(c => c.SchoolId == school.Id).ToList(); //B. delete courses documents List <Domain.Document> deleteDocuments = new List <Domain.Document>(); foreach (Domain.Course course in deleteCourses) { List <Domain.Document> oneCourseDocuments = _context.Documents .Where(d => d.CourseId == course.Id).ToList(); deleteDocuments.AddRange(oneCourseDocuments); } _context.Documents.RemoveRange(deleteDocuments); _context.SaveChanges(); //C. delete courses _context.Courses.RemoveRange(deleteCourses); _context.SaveChanges(); //D. delete mentors List <Domain.Mentor> deleteMentors = _context.Mentors.Where(m => m.SchoolId == school.Id).ToList(); _context.Mentors.RemoveRange(deleteMentors); _context.SaveChanges(); //E. delete students List <Domain.Student> deleteStudents = _context.Students.Where(s => s.SchoolId == school.Id).ToList(); _context.Students.RemoveRange(deleteStudents); _context.SaveChanges(); //F. delete catalogues _context.Catalogues.RemoveRange(deleteCatalogues); _context.SaveChanges(); //G. delete subjects List <Domain.Subject> deleteSubjects = _context.Subjects.Where(s => s.SchoolId == school.Id).ToList(); _context.Subjects.RemoveRange(deleteSubjects); _context.SaveChanges(); //delete school Domain.School deleteSchool = _context.Schools.FirstOrDefault(s => s.Id == school.Id); _context.Schools.Remove(deleteSchool); _context.SaveChanges(); }