Пример #1
0
        public static void Initialize(CourseContext context)
        {
            context.Database.EnsureCreated();

            // Look for any students.
            if (context.Courses.Any())
            {
                return;   // DB has been seeded
            }

            var courses = new CourseDescription[]
            {
                new CourseDescription {
                    Description = "Coding class 1"
                },
                new CourseDescription {
                    Description = "Another coding class 2"
                },
                new CourseDescription {
                    Description = "Another coding class 3"
                },
                new CourseDescription {
                    Description = "Another coding class 4"
                },
                new CourseDescription {
                    Description = "Another coding class 5"
                }
            };

            foreach (CourseDescription c in courses)
            {
                context.Description.Add(c);
            }
            context.SaveChanges();

            var coursesList = new CourseInstance[]
            {
                new CourseInstance {
                    ID = 1, Dept = "Computer Science", Number = 1410, Semester = "Fall", Year = 2019, Description = courses[1]
                },
                new CourseInstance {
                    ID = 2, Dept = "Computer Science", Number = 2420, Semester = "Fall", Year = 2019, Description = courses[1]
                },
                new CourseInstance {
                    ID = 3, Dept = "Computer Science", Number = 2420, Semester = "Spring", Year = 2020, Description = courses[1]
                },
                new CourseInstance {
                    ID = 4, Dept = "Computer Science", Number = 3500, Semester = "Fall", Year = 2019, Description = courses[3]
                },
                new CourseInstance {
                    ID = 5, Dept = "Computer Science", Number = 3505, Semester = "Fall", Year = 2019, Description = courses[3]
                },
            };

            foreach (CourseInstance s in coursesList)
            {
                context.Courses.Add(s);
            }
            context.SaveChanges();
        }
Пример #2
0
        public async Task <IActionResult> Edit(int id, [Bind("DescriptionId,Description")] CourseDescription courseDescription)
        {
            if (id != courseDescription.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(courseDescription);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CourseDescriptionExists(courseDescription.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(courseDescription));
        }
Пример #3
0
        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);

            CourseDescription.OnModelCreating(builder);

            Request.OnModelCreating(builder);
        }
Пример #4
0
        public async Task <IActionResult> Create([Bind("DescriptionId,Description")] CourseDescription courseDescription)
        {
            if (ModelState.IsValid)
            {
                _context.Add(courseDescription);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(courseDescription));
        }
Пример #5
0
        public IEnumerable <ValidationResult> Validate(ValidationContext validationContext)
        {
            // Validation 1: Credits have to be between 1-4
            if (CourseCredits < 1 || CourseCredits > 4)
            {
                yield return(new ValidationResult("Credits must be between 1 and 4."));
            }

            // Validation 2:
            if (CourseDescription.Split(' ').Length > 100)
            {
                yield return(new ValidationResult("Your description is too verbose."));
            }
        }
Пример #6
0
        public IEnumerable <ValidationResult> Validate(ValidationContext validationContext)
        {
            // validation for credits 1-4
            if (CourseCredits < 1 || CourseCredits > 4)
            {
                yield return(new ValidationResult("Credits must be between 1 and 4"));
            }

            //validation for description being too long
            if (CourseDescription.Split(' ').Length > 100)
            {
                yield return(new ValidationResult("Your description is too verbose"));
            }
        }
Пример #7
0
        public IEnumerable <ValidationResult> Validate(ValidationContext validationContext)
        {
            int maxCourseCredits = 4;
            int maxWords         = 5;

            //validation 1 - Credits has to be 1-4

            if (int.Parse(CourseCredits) > maxCourseCredits)
            {
                //error
                yield return(new ValidationResult("Course Credits must be between 1 and 4", new[] { "Credits" }));
            }

            //validation 2 - Description cannot exceed 5 words

            if (CourseDescription.Split(' ').Length > maxWords)
            {
                //error
                yield return(new ValidationResult("Course Descritpion is too wordy!", new[] { "Description" }));
            }
        }