Add missing files. Add API for client apps.
[wsti_pai.git] / Projects / Controllers / api / ProjectsController.cs
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.Threading.Tasks;
10 using System.Web.Http;
11 using System.Web.Http.Description;
12 using Projects.Models;
13
14 namespace Projects.Controllers.api
15 {
16     [AllowCrossSiteJson]
17     public class ProjectsController : ApiController
18     {
19         private ProjectsDBEntities db = new ProjectsDBEntities();
20
21         // GET: api/Projects
22         public IQueryable<Project> GetProjects()
23         {
24             db.Configuration.ProxyCreationEnabled = false;
25             db.Configuration.LazyLoadingEnabled = false;
26             return db.Projects;
27         }
28
29         // GET: api/Projects/5
30         [ResponseType(typeof(Project))]
31         public async Task<IHttpActionResult> GetProject(int id)
32         {
33             Project project = await db.Projects.FindAsync(id);
34             if (project == null)
35             {
36                 return NotFound();
37             }
38
39             return Ok(project);
40         }
41
42         // PUT: api/Projects/5
43         [ResponseType(typeof(void))]
44         public async Task<IHttpActionResult> PutProject(int id, Project project)
45         {
46             if (!ModelState.IsValid)
47             {
48                 return BadRequest(ModelState);
49             }
50
51             if (id != project.id)
52             {
53                 return BadRequest();
54             }
55
56             db.Entry(project).State = EntityState.Modified;
57
58             try
59             {
60                 await db.SaveChangesAsync();
61             }
62             catch (DbUpdateConcurrencyException)
63             {
64                 if (!ProjectExists(id))
65                 {
66                     return NotFound();
67                 }
68                 else
69                 {
70                     throw;
71                 }
72             }
73
74             return StatusCode(HttpStatusCode.NoContent);
75         }
76
77         // POST: api/Projects
78         [ResponseType(typeof(Project))]
79         public async Task<IHttpActionResult> PostProject(Project project)
80         {
81             if (!ModelState.IsValid)
82             {
83                 return BadRequest(ModelState);
84             }
85
86             db.Projects.Add(project);
87             await db.SaveChangesAsync();
88
89             return CreatedAtRoute("DefaultApi", new { id = project.id }, project);
90         }
91
92         // DELETE: api/Projects/5
93         [ResponseType(typeof(Project))]
94         public async Task<IHttpActionResult> DeleteProject(int id)
95         {
96             Project project = await db.Projects.FindAsync(id);
97             if (project == null)
98             {
99                 return NotFound();
100             }
101
102             db.Projects.Remove(project);
103             await db.SaveChangesAsync();
104
105             return Ok(project);
106         }
107
108         protected override void Dispose(bool disposing)
109         {
110             if (disposing)
111             {
112                 db.Dispose();
113             }
114             base.Dispose(disposing);
115         }
116
117         private bool ProjectExists(int id)
118         {
119             return db.Projects.Count(e => e.id == id) > 0;
120         }
121     }
122 }