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);
                }
            }
        }
        public IEnumerable <object> GetAllUsers()
        {
            using (var context = new MainDBEntities())
            {
                if (this.GetType() == typeof(AdminAccess))
                {
                    List <User> users = context.Users.ToList();

                    //Map all schools to AdminSchoolViewModel objects
                    ApplicationSpecificMapper mapper = new ApplicationSpecificMapper();

                    //Convert objects from dom model to view model
                    IEnumerable <object> user_enum = mapper.MapAll(users,
                                                                   typeof(AdminUserViewModel)).Cast <object>().AsEnumerable();

                    //return enum of view models
                    return(user_enum);
                }
                else if (this.GetType() == typeof(AmbassadorAccess))
                {
                    List <User> users = context.Users.ToList();

                    //Map all schools to AdminSchoolViewModel objects
                    ApplicationSpecificMapper mapper = new ApplicationSpecificMapper();

                    //Convert objects from dom model to view model
                    IEnumerable <object> user_enum = mapper.MapAll(users,
                                                                   typeof(AmbassUserViewModel)).Cast <object>().AsEnumerable();

                    //return enum of view models
                    return(user_enum);
                }
                else
                {
                    //Gracefully deal with error ***
                    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 IEnumerable <object> GetAllActiveProjects()
        {
            using (var context = new MainDBEntities())
            {
                if (this.GetType() == typeof(AdminAccess))
                {
                    var temp = (from assignment in context.ProjectAssignments
                                join project in context.Projects on assignment.ProjectID equals project.ProjectID
                                into first from first_beg in first.DefaultIfEmpty()
                                join school in context.Schools on assignment.SchoolID equals school.SchoolID
                                into second from second_beg in second.DefaultIfEmpty()
                                join ambassador in context.Users on second_beg.Username equals ambassador.Username
                                into third from third_beg in third.DefaultIfEmpty()
                                select new {
                        ProjectAssignId = assignment.ProjectAssignID,
                        ProjectID = assignment.ProjectID,
                        SchoolID = assignment.SchoolID,
                        ProjectName = first_beg.ProjectName,
                        SchoolName = second_beg.SchoolName,
                        ProgressStatus = assignment.ProgressStatus,
                        Username = third_beg.Username,
                        Email = third_beg.Email
                    });

                    //Convert into necessary view model
                    ApplicationSpecificMapper mapper = new ApplicationSpecificMapper();

                    IEnumerable <object> assignment_list = (IEnumerable <object>)(mapper.MapAll(temp, typeof(AdminActiveProjectViewModel)));

                    //Return view models
                    return(assignment_list);
                }
                else if (this.GetType() == typeof(AmbassadorAccess))
                {
                    //Modify to make ambassador specific ***

                    var temp = (from assignment in context.ProjectAssignments
                                join project in context.Projects on assignment.ProjectID equals project.ProjectID
                                into first
                                from first_beg in first.DefaultIfEmpty()
                                join school in context.Schools on assignment.SchoolID equals school.SchoolID
                                into second
                                from second_beg in second.DefaultIfEmpty()
                                join ambassador in context.Users on second_beg.Username equals ambassador.Username
                                into third
                                from third_beg in third.DefaultIfEmpty()
                                select new
                    {
                        ProjectAssignId = assignment.ProjectAssignID,
                        ProjectID = assignment.ProjectID,
                        SchoolID = assignment.SchoolID,
                        ProjectName = first_beg.ProjectName,
                        SchoolName = second_beg.SchoolName,
                        ProgressStatus = assignment.ProgressStatus,
                        Username = third_beg.Username,
                        Email = third_beg.Email
                    });

                    //Convert into necessary view model
                    ApplicationSpecificMapper mapper = new ApplicationSpecificMapper();

                    IEnumerable <AmbassActiveProjectViewModel> vm_assignment_list = (IEnumerable <AmbassActiveProjectViewModel>)(mapper.MapAll(temp, typeof(AmbassActiveProjectViewModel))).Cast <AmbassActiveProjectViewModel>();

                    //Eliminate archived ideas for ambassador access
                    vm_assignment_list = vm_assignment_list.Where(x => x.ProgressStatus != ProjectStatus.ARCHIVED);

                    //Box items for passage into controller
                    IEnumerable <object> ob_assignment_list = (IEnumerable <object>)vm_assignment_list.Cast <object>();

                    //Return view models
                    return(ob_assignment_list);
                }
                else
                {
                    //Error -- Access type of 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;
            }
        }
        //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 get the current list of projects.
        //    Input: None.
        //   Output: An IEnumerable< T = appropriate view model >, or null
        public IEnumerable <object> GetProjectList()
        {
            using (var context = new MainDBEntities())
            {
                //Create mapper
                ApplicationSpecificMapper mapper = new ApplicationSpecificMapper();
                IEnumerable <object>      project_list;
                IEnumerable <object>      view_model_list;

                //Check the type of the calling object and vary reaction
                if (this.GetType() == typeof(AdminAccess))
                {
                    //If admin, use admin project view model

                    //Get project list
                    project_list = (IEnumerable <object>)context.Projects.ToList().Cast <object>();

                    try
                    {
                        //Map list to corresponding view model
                        view_model_list = mapper.MapAll(project_list, typeof(AdminProjectViewModel)).Cast <AdminProjectViewModel>();
                    }
                    catch
                    {
                        view_model_list = null;
                    }

                    if (view_model_list == null)
                    {
                        return(null);
                    }

                    //Return list of view models
                    return((IEnumerable <object>)view_model_list.Cast <AdminProjectViewModel>().GetEnumerator());
                }
                else if (this.GetType() == typeof(AmbassadorAccess))
                {
                    //If ambassador, use ambass project view model

                    //Get project list
                    project_list = (IEnumerable <object>)context.Projects.ToList().Cast <object>();
                    IEnumerable <AmbassProjectViewModel> temp;

                    try
                    {
                        //Map list to corresponding view model
                        temp = (IEnumerable <AmbassProjectViewModel>)mapper.MapAll(project_list,
                                                                                   typeof(AmbassProjectViewModel)).Cast <AmbassProjectViewModel>();
                    }
                    catch
                    {
                        temp = null;
                    }

                    if (temp == null)
                    {
                        return(null);
                    }

                    //Eliminate archived ideas for ambassador
                    view_model_list = temp.Where(x => x.IsArchived == false).Cast <object>();

                    //Return list of view models
                    return((IEnumerable <object>)view_model_list.Cast <AmbassProjectViewModel>().GetEnumerator());
                }
                else if (this.GetType() == typeof(ContributorAccess))
                {
                    //If contributor, use contributor project view model

                    //Get project list
                    project_list = (IEnumerable <object>)context.Projects.ToList().Cast <object>();
                    IEnumerable <ContributorProjectViewModel> temp;

                    try
                    {
                        //Map list to corresponding view model
                        temp = (IEnumerable <ContributorProjectViewModel>)mapper.MapAll(project_list,
                                                                                        typeof(ContributorProjectViewModel)).Cast <ContributorProjectViewModel>();
                    }
                    catch
                    {
                        temp = null;
                    }

                    if (temp == null)
                    {
                        return(null);
                    }

                    //Eliminate archived ideas for contributor
                    view_model_list = temp.Where(x => x.IsArchived == false).Cast <object>();

                    //Return list of view models
                    return((IEnumerable <object>)view_model_list.Cast <ContributorProjectViewModel>().GetEnumerator());
                }
                else if (this.GetType() == typeof(DefaultAccess))
                {
                    //If default, use default project view model

                    //Get project list
                    project_list = (IEnumerable <object>)context.Projects.ToList().Cast <object>();

                    //Map list to corresponding view model
                    IEnumerable <ProjectViewModel> temp;

                    try
                    {
                        temp = (IEnumerable <ProjectViewModel>)mapper.MapAll(project_list, typeof(ProjectViewModel)).Cast <ProjectViewModel>();
                    }
                    catch
                    {
                        temp = null;
                    }

                    if (temp == null)
                    {
                        return(null);
                    }

                    //Eliminate archived ideas for default
                    view_model_list = temp.Where(x => x.IsArchived == false).Cast <object>();

                    //Return list of view models
                    return((IEnumerable <object>)view_model_list.Cast <ProjectViewModel>().GetEnumerator());
                }
                else
                {
                    //Invalid access type
                    Debug.WriteLine("\n\n***** " +
                                    "Access object type wasn't recognized during GetProject(). " +
                                    "ERROR IN: CommerceIdeaBank.DatabaseInterface.BusinessLogic.ContributorAccess GetProject()" +
                                    "*****\n\n");

                    //Gracefully indicate error
                    return(null);
                }
            }
        }
        //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);
                }
            }
        }
Esempio n. 11
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);
                }
            }
        }