Ejemplo n.º 1
0
        public IHttpActionResult AddCourse(Course newCourse)
        {
            // Checking if the object is not valid
            if (newCourse == null) throw new HttpResponseException(HttpStatusCode.PreconditionFailed);
            if (!ModelState.IsValid) throw new HttpResponseException(HttpStatusCode.PreconditionFailed);

            // Generating an ID for the object
            int id = _courses.Last().ID + 1;

            // Creating the course and adding it to the list
            Course course = new Course
            {
                ID = id,
                Name = newCourse.Name,
                TemplateID = newCourse.TemplateID,
                StartDate = newCourse.StartDate,
                EndDate = newCourse.EndDate
            };
            _courses.Add(course);

            // Getting the location and returning the create status code
            var location = Url.Link("GetCourse", new { id = course.ID });
            return Created(location, course);
        }
Ejemplo n.º 2
0
        public IHttpActionResult AddCourse(Course c)
        {
            //checking if the course being added is not of the right data type
            if (c == null)
            {
                throw new HttpResponseException(HttpStatusCode.PreconditionFailed);
            }

            //setting location url
            var location = Url.Link("GetCourse", new { id = c.ID });

            //adding course to list
            _courses.Add(c);

            return Created(location, c);
        }
Ejemplo n.º 3
0
        public IHttpActionResult UpdateCourse(int id, Course newCourse)
        {
            // Checking if the object is not valid
            if (newCourse == null) throw new HttpResponseException(HttpStatusCode.PreconditionFailed);
            if (!ModelState.IsValid) throw new HttpResponseException(HttpStatusCode.PreconditionFailed);

            // Finding the course
            var course = _courses.Find(x => newCourse.ID == x.ID);
            if (course == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            //Course was found, updating it
            course.Name = newCourse.Name;
            course.TemplateID = newCourse.TemplateID;
            course.StartDate = newCourse.StartDate;
            course.EndDate = newCourse.EndDate;
            return Ok();
        }
Ejemplo n.º 4
0
        public IHttpActionResult UpdateCourse(int id, Course course)
        {
            //checking if the course being added is not of the right data type
            if (course == null)
            {
                throw new HttpResponseException(HttpStatusCode.PreconditionFailed);
            }

            //update right course
            foreach (Course c in _courses)
            {
                if (c.ID == id)
                {
                    var temp = _courses.SingleOrDefault(x=>x.ID == course.ID);
                    temp.Name = course.Name;
                    temp.StartDate = course.StartDate;
                    temp.EndDate = course.EndDate;
                    temp.Students = course.Students;

                    //201 successfully created
                    var location = Url.Link("GetCourse", new { id = course.ID });
                    return Created(location, temp);
                }
            }

            //404 id not found
            return NotFound();
        }