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);
            }
        }
        //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;
            }
        }
        //Func Desc: Used to return a project from its id.
        //    Input: Int representing id of project to locate.
        //   Output: An instance of the project that has the specified id, or null
        public bool EditProject(object vm_project)
        {
            //Input checks
            if (vm_project == null) { return false; }
            if ( (vm_project.GetType() != typeof(AdminProjectViewModel)) &&
                 (vm_project.GetType() != typeof(AmbassProjectViewModel)) &&
                 (vm_project.GetType() != typeof(ContributorProjectViewModel)))
            {
                //Invalid view model
                Debug.WriteLine("\n\n***** " +
                    "View model input into EditProject of ContributorAccess is invalid in type. " +
                    "ERROR IN: CommerceIdeaBank.DatabaseInterface.BusinessLogic.ContributorAccess EditProject()" +
                    "*****\n\n" );

                //Indicate failure in status
                return false;
            }

            using (var context = new MainDBEntities())
            {
                ApplicationSpecificMapper mapper = new ApplicationSpecificMapper();

                if (this.GetType() == typeof(AdminAccess))
                {
                    Project proj = (Project)mapper.Map((AdminProjectViewModel)vm_project, typeof(Project));

                    //Create the project edit and populate necessary attributes
                    ProjectEdit edit = new ProjectEdit();
                    edit.Username = proj.Username;
                    edit.ProjectID = proj.ProjectID;
                    edit.EditDate = DateTime.Now;
                    context.ProjectEdits.Add(edit);

                    //Indicate modification and make changes persistent
                    context.Entry(proj).State = EntityState.Modified;
                    context.SaveChanges();

                    //Return true indicating successful project edit
                    return true;

                }
                else if (this.GetType() == typeof(AmbassadorAccess))
                {
                    Project proj = (Project)mapper.Map((AmbassProjectViewModel)vm_project, typeof(Project));

                    //Create the project edit and populate necessary attributes
                    ProjectEdit edit = new ProjectEdit();
                    edit.Username = proj.Username;
                    edit.ProjectID = proj.ProjectID;
                    edit.EditDate = DateTime.Now;
                    context.ProjectEdits.Add(edit);

                    //Indicate modification and make changes persistent
                    context.Entry(proj).State = EntityState.Modified;
                    context.SaveChanges();

                    //Return true indicating successful project edit
                    return true;
                }
                else if (this.GetType() == typeof(ContributorAccess))
                {
                    Project proj = (Project)mapper.Map((ContributorProjectViewModel)vm_project, typeof(Project));

                    //Create the project edit and populate necessary attributes
                    ProjectEdit edit = new ProjectEdit();
                    edit.Username = proj.Username;
                    edit.ProjectID = proj.ProjectID;
                    edit.EditDate = DateTime.Now;
                    context.ProjectEdits.Add(edit);

                    //Indicate modification and make changes persistent
                    context.Entry(proj).State = EntityState.Modified;
                    context.SaveChanges();

                    //Return true indicating successful project edit
                    return true;
                }
                else
                {
                    //Access type not recognized

                    Debug.WriteLine("\n\n***** " +
                        "Access object type wasn't recognized during EditProject(). " +
                        "ERROR IN: CommerceIdeaBank.DatabaseInterface.BusinessLogic.ContributorAccess EditProject()" +
                        "*****\n\n");

                    //Indicate error status
                    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);
            }
        }
        //Func Desc: Used to return a project from its id.
        //    Input: Int representing id of project to locate.
        //   Output: An instance of the project that has the specified id, or null
        public object GetProject(int?project_id)  //Should this be nullable? ***
        {
            //Input checks
            if (project_id == null)
            {
                return(null);
            }
            if (project_id < 0)
            {
                return(null);
            }

            using (var context = new MainDBEntities())
            {
                ApplicationSpecificMapper mapper = new ApplicationSpecificMapper();

                if (this.GetType() == typeof(AdminAccess))
                {
                    //Map located project to project view model
                    AdminProjectViewModel vm_project = (AdminProjectViewModel)mapper.Map(context.Projects.Find(project_id), typeof(AdminProjectViewModel));

                    //Return project with given id or null
                    return(vm_project);
                }
                else if (this.GetType() == typeof(AmbassadorAccess))
                {
                    //Map located project to project view model
                    AmbassProjectViewModel vm_project = (AmbassProjectViewModel)mapper.Map(context.Projects.Find(project_id), typeof(AmbassProjectViewModel));

                    //Make sure project isn't archived
                    if (vm_project.IsArchived == true)
                    {
                        return(null);
                    }

                    //Return project with given id or null
                    return(vm_project);
                }
                else if (this.GetType() == typeof(ContributorAccess))
                {
                    //Map located project to project view model
                    ContributorProjectViewModel vm_project = (ContributorProjectViewModel)mapper.Map(context.Projects.Find(project_id), typeof(ContributorProjectViewModel));

                    //Make sure project isn't archived
                    if (vm_project.IsArchived == true)
                    {
                        return(null);
                    }

                    //Return project with given id or null
                    return(vm_project);
                }
                else if (this.GetType() == typeof(DefaultAccess))
                {
                    //Map located project to project view model
                    ProjectViewModel vm_project = (ProjectViewModel)mapper.Map(context.Projects.Find(project_id), typeof(ProjectViewModel));

                    //Make sure project isn't archived
                    if (vm_project.IsArchived == true)
                    {
                        return(null);
                    }

                    //Return project with given id or null
                    return(vm_project);
                }
                else
                {
                    //Invalid access object
                    Debug.WriteLine("\n\n***** " +
                                    "Access object type wasn't recognized during GetProject(). " +
                                    "ERROR IN: CommerceIdeaBank.DatabaseInterface.BusinessLogic.ContributorAccess GetProject()" +
                                    "*****\n\n");

                    return(null);
                }
            }
        }
Exemple #8
0
        //Func Desc: Used to return a project from its id.
        //    Input: Int representing id of project to locate.
        //   Output: An instance of the project that has the specified id, or null
        public bool EditProject(object vm_project)
        {
            //Input checks
            if (vm_project == null)
            {
                return(false);
            }
            if ((vm_project.GetType() != typeof(AdminProjectViewModel)) &&
                (vm_project.GetType() != typeof(AmbassProjectViewModel)) &&
                (vm_project.GetType() != typeof(ContributorProjectViewModel)))
            {
                //Invalid view model
                Debug.WriteLine("\n\n***** " +
                                "View model input into EditProject of ContributorAccess is invalid in type. " +
                                "ERROR IN: CommerceIdeaBank.DatabaseInterface.BusinessLogic.ContributorAccess EditProject()" +
                                "*****\n\n");

                //Indicate failure in status
                return(false);
            }

            using (var context = new MainDBEntities())
            {
                ApplicationSpecificMapper mapper = new ApplicationSpecificMapper();

                if (this.GetType() == typeof(AdminAccess))
                {
                    Project proj = (Project)mapper.Map((AdminProjectViewModel)vm_project, typeof(Project));

                    //Create the project edit and populate necessary attributes
                    ProjectEdit edit = new ProjectEdit();
                    edit.Username  = proj.Username;
                    edit.ProjectID = proj.ProjectID;
                    edit.EditDate  = DateTime.Now;
                    context.ProjectEdits.Add(edit);

                    //Indicate modification and make changes persistent
                    context.Entry(proj).State = EntityState.Modified;
                    context.SaveChanges();

                    //Return true indicating successful project edit
                    return(true);
                }
                else if (this.GetType() == typeof(AmbassadorAccess))
                {
                    Project proj = (Project)mapper.Map((AmbassProjectViewModel)vm_project, typeof(Project));

                    //Create the project edit and populate necessary attributes
                    ProjectEdit edit = new ProjectEdit();
                    edit.Username  = proj.Username;
                    edit.ProjectID = proj.ProjectID;
                    edit.EditDate  = DateTime.Now;
                    context.ProjectEdits.Add(edit);

                    //Indicate modification and make changes persistent
                    context.Entry(proj).State = EntityState.Modified;
                    context.SaveChanges();

                    //Return true indicating successful project edit
                    return(true);
                }
                else if (this.GetType() == typeof(ContributorAccess))
                {
                    Project proj = (Project)mapper.Map((ContributorProjectViewModel)vm_project, typeof(Project));

                    //Create the project edit and populate necessary attributes
                    ProjectEdit edit = new ProjectEdit();
                    edit.Username  = proj.Username;
                    edit.ProjectID = proj.ProjectID;
                    edit.EditDate  = DateTime.Now;
                    context.ProjectEdits.Add(edit);

                    //Indicate modification and make changes persistent
                    context.Entry(proj).State = EntityState.Modified;
                    context.SaveChanges();

                    //Return true indicating successful project edit
                    return(true);
                }
                else
                {
                    //Access type not recognized

                    Debug.WriteLine("\n\n***** " +
                                    "Access object type wasn't recognized during EditProject(). " +
                                    "ERROR IN: CommerceIdeaBank.DatabaseInterface.BusinessLogic.ContributorAccess EditProject()" +
                                    "*****\n\n");

                    //Indicate error status
                    return(false);
                }
            }
        }