Esempio n. 1
0
        public HttpResponseMessage Put(int id, [FromBody] CourseModel courseModel)
        {
            var updatedCourse = TheModelFactory.ParseFromCourseModel(courseModel);

            if (updatedCourse == null)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest, "Could not read posted values"));
            }

            var originalCourse = TheRepository.GetCourse(id, false);

            if (originalCourse == null || originalCourse.Id != id)
            {
                return(Request.CreateResponse(HttpStatusCode.NotModified, "Wrong id posted in body"));
            }

            else
            {
                updatedCourse.Id = id;
            }

            if (TheRepository.Update(originalCourse, updatedCourse) && TheRepository.SaveAll())
            {
                return(Request.CreateResponse(HttpStatusCode.OK, TheModelFactory.Create(updatedCourse)));
            }

            else
            {
                return(Request.CreateResponse(HttpStatusCode.NotModified));
            }
        }
Esempio n. 2
0
        public IHttpActionResult Post(CourseModel courseModel)
        {
            var entity = TheModelFactory.ParseFromCourseModel(courseModel);

            if (entity == null)
            {
                return(BadRequest("Please send valid data"));
            }

            if (TheRepository.Insert(entity) && TheRepository.SaveAll())
            {
                return(CreatedAtRoute("Courses", new { id = entity.Id }, TheModelFactory.Create(entity)));
            }

            else
            {
                return(BadRequest("Could not save to database"));
            }
        }