예제 #1
0
 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);
 }
예제 #2
0
 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);
 }
예제 #3
0
 public JsonResult Add(string title)
 {
     var model = new Todo
     {
         Title = title,
         Id = Guid.NewGuid().ToString()
     };
     _db.Add(model);
     return Json(model);
 }
예제 #4
0
        // 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);
            }
        }
예제 #5
0
        // 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);
            }
        }