public JsonResult Edit(Todo model) { var edit = _db.SingleOrDefault(x => string.Equals(x.Id, model.Id)); if (edit == null) throw new Exception("Todo não existe!"); edit.Done = model.Done; return Json(model); }
public TodoController() { // Se o db existe não faz nada if (_db != null) return; _db = new List<Todo>(); var todo1 = new Todo { CreatedIn = DateTime.Now, Done = true, Id = Guid.NewGuid().ToString(), Title = "Todo1" }; var todo2 = new Todo { CreatedIn = DateTime.Now, Done = false, Id = Guid.NewGuid().ToString(), Title = "Todo2" }; var todo3 = new Todo { CreatedIn = DateTime.Now, Done = true, Id = Guid.NewGuid().ToString(), Title = "Todo3" }; _db.Add(todo1); _db.Add(todo2); _db.Add(todo3); }
public JsonResult Add(string title) { var model = new Todo { Title = title, Id = Guid.NewGuid().ToString() }; _db.Add(model); return Json(model); }
// POST api/Todos public HttpResponseMessage PostTodo(Todo todo) { if (ModelState.IsValid) { db.Todos.Add(todo); db.SaveChanges(); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, todo); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = todo.Id })); return response; } else { return Request.CreateResponse(HttpStatusCode.BadRequest); } }
// PUT api/Todos/5 public HttpResponseMessage PutTodo(Guid id, Todo todo) { if (ModelState.IsValid && id == todo.Id) { db.Entry(todo).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { return Request.CreateResponse(HttpStatusCode.NotFound); } return Request.CreateResponse(HttpStatusCode.OK, todo); } else { return Request.CreateResponse(HttpStatusCode.BadRequest); } }