public object GetSchool(int?id) { //Input checks if (id == null) { return(null); } if (id < 0) { return(null); } using (var context = new MainDBEntities()) { //Read in school with id = id object school = context.Schools.Find(id); if (school == null) { return(null); } //Instantiate mapper object ApplicationSpecificMapper mapper = new ApplicationSpecificMapper(); //Create application specific mapper and map to view model if (this.GetType() == typeof(AdminAccess)) { AdminSchoolViewModel vm_school = (AdminSchoolViewModel)mapper.Map((School)school, typeof(AdminSchoolViewModel)); return((object)vm_school); } else if (this.GetType() == typeof(AmbassadorAccess)) { AmbassSchoolViewModel vm_school = (AmbassSchoolViewModel)mapper.Map((School)school, typeof(AmbassSchoolViewModel)); return((object)vm_school); } else { //Error : Access object not recognized return(null); } } }
//Func Desc: Used to submit school to database // Input: A SchoolViewModel object instance // Output: A bool indicating whether submission succeeded. T = success, F = failure public bool SubmitSchool(AdminSchoolViewModel new_school) { //Input checks if (new_school == null) { return(false); } try { using (var context = new MainDBEntities()) { ApplicationSpecificMapper mapper = new ApplicationSpecificMapper(); //Create new project instance School school = new School(); //Map the view model to the domain model school = (School)mapper.Map(new_school, typeof(School)); //Submit the project to the db context.Schools.Add(school); //Save changes context.SaveChanges(); //Indicate successful submission return(true); } } catch { //Return false indicating failure to submit project return(false); } }
public bool EditSchool(AdminSchoolViewModel vm_school) { if (vm_school == null) { return(false); } try { using (var context = new MainDBEntities()) { ApplicationSpecificMapper mapper = new ApplicationSpecificMapper(); //Validate input if (mapper == null) { return(false); } School school = (School)mapper.Map((AdminSchoolViewModel)vm_school, typeof(School)); //Validate input if (school == null) { return(false); } //Create the project edit and populate necessary attributes SchoolEdit edit = new SchoolEdit(); edit.SchoolID = school.SchoolID; edit.Username = school.Username; edit.SchoolName = school.SchoolName; edit.Phone = (int)school.Phone; edit.Email = school.Email; edit.ContactEmail = school.ContactEmail; edit.ContactName = school.ContactName; edit.ContactPhone = school.ContactPhone; edit.Department = school.Department; edit.Class = school.Class; edit.StreetNumber = school.StreetNumber; edit.StreetName = school.StreetName; edit.ZipCode = school.ZipCode; edit.City = school.City; edit.State = school.State; edit.EditDate = DateTime.Now; context.SchoolEdits.Add(edit); //Indicate modification and make changes persistent context.Entry(school).State = EntityState.Modified; context.SaveChanges(); //Return true indicating successful project edit return(true); } } catch { //There was an error during school edit return(false); } }