public ActionResult Index(CourseOfferingViewModel viewModel)
        {
            try
            {
                // Populate
                viewModel = new CourseOfferingViewModelFactory(viewModel, _courseService).GetViewModel();

                // Validate
                if (!ModelState.IsValid)
                    return View(viewModel);

                // Bind
                var courseOffering = new CourseOffering
                {
                    CourseId = viewModel.SelectedCourse,
                    Semester = viewModel.Semester,
                    Year = viewModel.SelectedYear
                };

                // Save
                _courseService.AddCourseOffering(courseOffering);

                // Respond
                Success("The Course Offering has been created", true);
            }
            catch (CourseOfferingExistsException)
            {
                Danger("Course Offering already exists.");
                return View(viewModel);
            }

            // redirect back to the page index
            return RedirectToAction("Index");
        }
        public async Task <CourseOffering> CreateCourseOfferingAsync(CourseOfferingViewModel model)
        {
            try
            {
                var courseOffering = mapper.Map <CourseOffering>(model);

                var created = await repository.CreateCourseOfferingAsync(courseOffering);

                return(await SaveAndReturn(created));
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        private Entities.CourseOffering CreateTestCourseOffering(CourseOfferingViewModel model)
        {
            var created = new Entities.CourseOffering
            {
                Id           = "2",
                CourseId     = model.CourseId,
                InstructorId = model.InstructorId,
                RoomId       = model.RoomId,
                TimeslotId   = model.TimeslotId
            };

            timeslots.Add(created);

            return(created);
        }
        public async Task <IActionResult> CreateCourse([FromBody] CourseOfferingViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                var created = await courseOfferingService.CreateCourseOfferingAsync(model);

                return(CreatedAtAction(nameof(GetCourseOfferingById), new { courseOfferingId = created.Id }, created));
            }
            catch (Exception e)
            {
                return(await HandleControllerException(e));
            }
        }
        public async Task CanAddALocation()
        {
            var toAdd = new CourseOfferingViewModel
            {
                CourseId     = "1",
                InstructorId = "1",
                RoomId       = "1",
                TimeslotId   = "1",
            };

            courseOfferingService.Setup(repo => repo.CreateCourseOfferingAsync(toAdd)).ReturnsAsync(CreateTestCourseOffering(toAdd));
            courseOfferingService.Setup(repo => repo.GetCourseOfferingsAsync()).ReturnsAsync(GetTestCourseOfferings());

            var result = await controller.GetCourseOfferings();

            // Assertions
            Assert.IsInstanceOf <ActionResult <IList <Entities.CourseOffering> > >(result);
            var resultObject = GetObjectResultContent(result);

            Assert.AreEqual(3, resultObject.Count);
        }
 /// <summary>
 ///     Initializes a new instance of the <see cref="CourseOfferingViewModelFactory" /> class.
 /// </summary>
 /// <param name="viewModel">The view model.</param>
 /// <param name="courseService">The course service.</param>
 public CourseOfferingViewModelFactory(CourseOfferingViewModel viewModel, ICourseService courseService)
 {
     _viewModel = viewModel;
     _courseService = courseService;
 }