/// <summary> /// Method that adds a new course to the database. /// The attributes needed to add a new course are given with a view model class. /// </summary> /// <param name="model">Add course view model (ViewModel class)</param> /// <returns>The newly added course (DTO class)</returns> public CourseDTO CreateCourse(AddCourseViewModel model) { var courseTemplate = _db.CourseTemplates.SingleOrDefault(x => x.TemplateID == model.TemplateID); if (courseTemplate == null) { throw new AppInternalServerException(); } var course = new Course { TemplateID = model.TemplateID, StartDate = model.StartDate, EndDate = model.EndDate, Semester = model.Semester, MaxStudents = model.MaxStudents }; _db.Courses.Add(course); _db.SaveChanges(); var result = new CourseDTO { ID = course.ID, Name = course.TemplateID, StartDate = course.StartDate, StudentCount = 0 }; return result; }
public IHttpActionResult CreateCourse(AddCourseViewModel model) { if (ModelState.IsValid) { try { var result = _service.CreateCourse(model); var location = Url.Link("GetCourseById", new { id = result.ID }); return Created(location, result); } catch (AppInternalServerException) { return StatusCode(HttpStatusCode.InternalServerError); } } return StatusCode(HttpStatusCode.PreconditionFailed); }