2 using System.Collections.Generic;
4 using System.Data.Entity;
5 using System.Data.Entity.Infrastructure;
9 using System.Threading.Tasks;
10 using System.Web.Http;
11 using System.Web.Http.Description;
12 using Projects.Models;
14 namespace Projects.Controllers.api
17 public class ProjectsController : ApiController
19 private ProjectsDBEntities db = new ProjectsDBEntities();
22 public IQueryable<Project> GetProjects()
24 db.Configuration.ProxyCreationEnabled = false;
25 db.Configuration.LazyLoadingEnabled = false;
29 // GET: api/Projects/5
30 [ResponseType(typeof(Project))]
31 public async Task<IHttpActionResult> GetProject(int id)
33 Project project = await db.Projects.FindAsync(id);
42 // PUT: api/Projects/5
43 [ResponseType(typeof(void))]
44 public async Task<IHttpActionResult> PutProject(int id, Project project)
46 if (!ModelState.IsValid)
48 return BadRequest(ModelState);
56 db.Entry(project).State = EntityState.Modified;
60 await db.SaveChangesAsync();
62 catch (DbUpdateConcurrencyException)
64 if (!ProjectExists(id))
74 return StatusCode(HttpStatusCode.NoContent);
78 [ResponseType(typeof(Project))]
79 public async Task<IHttpActionResult> PostProject(Project project)
81 if (!ModelState.IsValid)
83 return BadRequest(ModelState);
86 db.Projects.Add(project);
87 await db.SaveChangesAsync();
89 return CreatedAtRoute("DefaultApi", new { id = project.id }, project);
92 // DELETE: api/Projects/5
93 [ResponseType(typeof(Project))]
94 public async Task<IHttpActionResult> DeleteProject(int id)
96 Project project = await db.Projects.FindAsync(id);
102 db.Projects.Remove(project);
103 await db.SaveChangesAsync();
108 protected override void Dispose(bool disposing)
114 base.Dispose(disposing);
117 private bool ProjectExists(int id)
119 return db.Projects.Count(e => e.id == id) > 0;