Esempio n. 1
0
        public IActionResult Create(CourseInputViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(this.View());
            }

            var course = new Courses
            {
                Title       = model.Title,
                Description = model.Description,
                Credits     = model.Credits
            };

            dbContext.Add(course);
            try
            {
                dbContext.SaveChanges();
                return(this.RedirectToAction("Details", new { Id = course.Id }));
            }
            catch (Exception e)
            {
                return(this.BadRequest(e.Message));
            }
        }
Esempio n. 2
0
        public IActionResult Edit(CourseInputViewModel model, int id)
        {
            if (!ModelState.IsValid)
            {
                return(this.View(model));
            }

            var course = dbContext.Courses.FirstOrDefault(c => c.Id == id);

            if (course == null)
            {
                return(this.BadRequest("Invalid product ID."));
            }

            course.Title       = model.Title;
            course.Credits     = model.Credits;
            course.Description = model.Description;

            try
            {
                this.dbContext.SaveChanges();
                return(this.RedirectToAction("Details", new { Id = id }));
            }
            catch (Exception e)
            {
                return(this.BadRequest(e.Message));
            }
        }
        public IActionResult Create()
        {
            var teachers = this.teachersService.GetAll <TeachersDropDownViewModel>();

            CourseInputViewModel input = new CourseInputViewModel
            {
                Teachers = teachers,
            };

            return(this.View(input));
        }
        public IActionResult Edit(int id = 0)
        {
            using (this.db)
            {
                var courseToEdit = this.db.Courses.FirstOrDefault(c => c.Id == id);
                var teachers     = this.teachersService.GetAll <TeachersDropDownViewModel>();

                CourseInputViewModel input = new CourseInputViewModel
                {
                    Id          = id,
                    Name        = courseToEdit.Name,
                    Description = courseToEdit.Description,
                    Teachers    = teachers,
                };

                return(this.View(input));
            }
        }
Esempio n. 5
0
        public IActionResult Delete(int id)
        {
            var course = dbContext.Courses.FirstOrDefault(c => c.Id == id);

            if (course == null)
            {
                return(this.BadRequest("Invalid product ID."));
            }

            var viewModel = new CourseInputViewModel
            {
                Title       = course.Title,
                Description = course.Description,
                Credits     = course.Credits,
            };

            return(this.View(viewModel));
        }
        public IActionResult Create(CourseInputViewModel input)
        {
            if (!this.ModelState.IsValid)
            {
                var teachers = this.teachersService.GetAll <TeachersDropDownViewModel>();
                input.Teachers = teachers;
                return(this.View(input));
            }

            var teacher = this.teachersService.GetTeacherById(input.TeacherId);

            Course course = new Course(input.Name, input.Description, teacher);

            using (this.db)
            {
                this.db.Courses.Add(course);
                this.db.SaveChanges();
            }

            return(this.RedirectToAction("Index", "Home"));
        }
        public IActionResult Edit(CourseInputViewModel input)
        {
            if (!this.ModelState.IsValid)
            {
                var teachers = this.teachersService.GetAll <TeachersDropDownViewModel>();
                input.Teachers = teachers;

                return(this.View(input));
            }

            var courseToEdit = this.coursesService.GetById(input.Id);

            using (this.db)
            {
                courseToEdit.Name        = input.Name;
                courseToEdit.Description = input.Description;
                courseToEdit.TeacherId   = input.TeacherId;
                courseToEdit.Teacher     = this.teachersService.GetTeacherById(input.TeacherId);

                this.db.SaveChanges();
            }

            return(this.RedirectToAction("ByName", new { name = input.Name }));
        }