Advertisement
Guest User

Angular tute

a guest
Mar 15th, 2013
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.69 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.Entity;
  5. using System.Data.Entity.Infrastructure;
  6. using System.Linq;
  7. using System.Net;
  8. using System.Net.Http;
  9. using System.Web;
  10. using System.Web.Http;
  11. using AmazingTodo.Models;
  12.  
  13. namespace AmazingTodo.Controllers
  14. {
  15.     public class TodoController : ApiController
  16.     {
  17.         private readonly AmazingTodoContext db = new AmazingTodoContext();
  18.  
  19.         public IEnumerable<TodoItem> GetTodoItems(string q = null, string sort = null, bool desc = false,
  20.                                                                int? limit = null, int offset = 0) {
  21.             var list = ((IObjectContextAdapter) db).ObjectContext.CreateObjectSet<TodoItem>();
  22.  
  23.             IQueryable<TodoItem> items = string.IsNullOrEmpty(sort) ? list.OrderBy(o=>o.Priority)
  24.                 : list.OrderBy(String.Format("it.{0} {1}", sort, desc ? "DESC" : "ASC"));
  25.  
  26.             if (!string.IsNullOrEmpty(q) && q != "undefined") items = items.Where(t => t.Todo.Contains(q));
  27.  
  28.             if (offset > 0) items = items.Skip(offset);
  29.             if (limit.HasValue) items = items.Take(limit.Value);
  30.             return items;
  31.         }
  32.  
  33.         // GET api/Todo/5
  34.         public TodoItem GetTodoItem(int id)
  35.         {
  36.             TodoItem todoitem = db.TodoItems.Find(id);
  37.             if (todoitem == null)
  38.             {
  39.                 throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
  40.             }
  41.  
  42.             return todoitem;
  43.         }
  44.  
  45.         // PUT api/Todo/5
  46.         public HttpResponseMessage PutTodoItem(int id, TodoItem todoitem)
  47.         {
  48.             if (ModelState.IsValid && id == todoitem.TodoItemId)
  49.             {
  50.                 db.Entry(todoitem).State = EntityState.Modified;
  51.  
  52.                 try
  53.                 {
  54.                     db.SaveChanges();
  55.                 }
  56.                 catch (DbUpdateConcurrencyException)
  57.                 {
  58.                     return Request.CreateResponse(HttpStatusCode.NotFound);
  59.                 }
  60.  
  61.                 return Request.CreateResponse(HttpStatusCode.OK);
  62.             }
  63.             else
  64.             {
  65.                 return Request.CreateResponse(HttpStatusCode.BadRequest);
  66.             }
  67.         }
  68.  
  69.         // POST api/Todo
  70.         public HttpResponseMessage PostTodoItem(TodoItem todoitem)
  71.         {
  72.             if (ModelState.IsValid)
  73.             {
  74.                 db.TodoItems.Add(todoitem);
  75.                 db.SaveChanges();
  76.  
  77.                 HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, todoitem);
  78.                 response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = todoitem.TodoItemId }));
  79.                 return response;
  80.             }
  81.             else
  82.             {
  83.                 return Request.CreateResponse(HttpStatusCode.BadRequest);
  84.             }
  85.         }
  86.  
  87.         // DELETE api/Todo/5
  88.         public HttpResponseMessage DeleteTodoItem(int id)
  89.         {
  90.             TodoItem todoitem = db.TodoItems.Find(id);
  91.             if (todoitem == null)
  92.             {
  93.                 return Request.CreateResponse(HttpStatusCode.NotFound);
  94.             }
  95.  
  96.             db.TodoItems.Remove(todoitem);
  97.  
  98.             try
  99.             {
  100.                 db.SaveChanges();
  101.             }
  102.             catch (DbUpdateConcurrencyException)
  103.             {
  104.                 return Request.CreateResponse(HttpStatusCode.NotFound);
  105.             }
  106.  
  107.             return Request.CreateResponse(HttpStatusCode.OK, todoitem);
  108.         }
  109.  
  110.         protected override void Dispose(bool disposing)
  111.         {
  112.             db.Dispose();
  113.             base.Dispose(disposing);
  114.         }
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement