コード例 #1
0
        public IActionResult UploadCourseGroup(FileUploadVm vm)
        {
            if (!Authenticate())
            {
                return(RedirectToAction("Index", "Home"));
            }
            var user = GetCurrentlyLoggedInUser();

            if (!user.IsAdmin)
            {
                return(RedirectToAction("Index", "Planner"));
            }

            if (!ModelState.IsValid)
            {
                return(View(vm));
            }

            using (var context = new DegreePlannerContext())
                using (var stream = vm.File.OpenReadStream())
                    using (var reader = new StreamReader(stream)) {
                        string line;
                        while ((line = reader.ReadLine()) != null)
                        {
                            var data = line.Split(',');

                            var name        = data[0];
                            var courseGroup = new CourseGroup()
                            {
                                Name = name,
                                CourseCourseGroupLinks = new List <CourseCourseGroupLink>()
                            };
                            for (var i = 1; i < data.Length; i++)
                            {
                                var link       = new CourseCourseGroupLink();
                                var department = data[i].Substring(0, 4);
                                var number     = data[i].Substring(4);
                                var course     = CreateOrFetchCourse(context, department, number, out var gen);

                                link.Course = course;

                                courseGroup.CourseCourseGroupLinks.Add(link);
                            }

                            context.CourseGroups.Add(courseGroup);
                            context.SaveChanges();
                        }
                    }

            return(RedirectToAction("Index", "Planner"));
        }
コード例 #2
0
        public IActionResult UploadDegree(FileUploadVm vm)
        {
            if (!Authenticate())
            {
                return(RedirectToAction("Index", "Home"));
            }
            var user = GetCurrentlyLoggedInUser();

            if (!user.IsAdmin)
            {
                return(RedirectToAction("Index", "Planner"));
            }

            if (!ModelState.IsValid)
            {
                return(View(vm));
            }

            var degree = new Degree {
                Name         = vm.Name,
                Requirements = new List <DegreeElement>()
            };

            using (var context = new DegreePlannerContext())
                using (var stream = vm.File.OpenReadStream())
                    using (var reader = new StreamReader(stream)) {
                        context.Degrees.Add(degree);
                        context.SaveChanges();

                        string line;
                        while ((line = reader.ReadLine()) != null)
                        {
                            var data = line.Split(',');

                            var element = new DegreeElement();
                            element.Hours = int.Parse(data[0]);
                            var courseGroupName = data[1];
                            var courseGroup     = context.CourseGroups.FirstOrDefault(cg => cg.Name == courseGroupName);
                            if (data.Length == 2)
                            {
                                if (courseGroup == null)
                                {
                                    courseGroup = new CourseGroup()
                                    {
                                        Name = courseGroupName
                                    }
                                }
                                ;
                            }
                            else
                            {
                                if (courseGroup == null)
                                {
                                    courseGroup = new CourseGroup()
                                    {
                                        Name = courseGroupName,
                                        CourseCourseGroupLinks = new List <CourseCourseGroupLink>()
                                    };

                                    for (var i = 2; i < data.Length; i++)
                                    {
                                        var link       = new CourseCourseGroupLink();
                                        var department = data[i].Substring(0, 4);
                                        var number     = data[i].Substring(4);
                                        var course     = CreateOrFetchCourse(context, department, number, out var gen);

                                        link.Course = course;
                                        courseGroup.CourseCourseGroupLinks.Add(link);
                                    }
                                }
                            }
                            element.Members = courseGroup;
                            element.Degree  = degree;
                            context.DegreeElements.Add(element);
                            context.SaveChanges();
                        }
                    }

            return(RedirectToAction("Index", "Planner"));
        }