// POST api/TodoList public HttpResponseMessage PostTodoList(TodoListDto todoListDto) { if (!ModelState.IsValid) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } TodoList todoList = todoListDto.ToEntity(); db.TodoLists.Add(todoList); db.SaveChanges(); todoListDto.TodoListId = todoList.TodoListId; HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, todoListDto); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = todoListDto.TodoListId })); return response; }
// PUT api/TodoList/5 public HttpResponseMessage PutTodoList(int id, TodoListDto todoListDto) { if (!ModelState.IsValid) { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } if (id != todoListDto.TodoListId) { return Request.CreateResponse(HttpStatusCode.BadRequest); } var todoList = todoListDto.ToEntity(); db.Entry(todoList).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { return Request.CreateResponse(HttpStatusCode.InternalServerError); } return Request.CreateResponse(HttpStatusCode.OK); }