Add missing files. Add API for client apps.
[wsti_pai.git] / Projects / Controllers / api / ProjectsController.cs
diff --git a/Projects/Controllers/api/ProjectsController.cs b/Projects/Controllers/api/ProjectsController.cs
new file mode 100644 (file)
index 0000000..b1b3406
--- /dev/null
@@ -0,0 +1,122 @@
+using System;
+using System.Collections.Generic;
+using System.Data;
+using System.Data.Entity;
+using System.Data.Entity.Infrastructure;
+using System.Linq;
+using System.Net;
+using System.Net.Http;
+using System.Threading.Tasks;
+using System.Web.Http;
+using System.Web.Http.Description;
+using Projects.Models;
+
+namespace Projects.Controllers.api
+{
+    [AllowCrossSiteJson]
+    public class ProjectsController : ApiController
+    {
+        private ProjectsDBEntities db = new ProjectsDBEntities();
+
+        // GET: api/Projects
+        public IQueryable<Project> GetProjects()
+        {
+            db.Configuration.ProxyCreationEnabled = false;
+            db.Configuration.LazyLoadingEnabled = false;
+            return db.Projects;
+        }
+
+        // GET: api/Projects/5
+        [ResponseType(typeof(Project))]
+        public async Task<IHttpActionResult> GetProject(int id)
+        {
+            Project project = await db.Projects.FindAsync(id);
+            if (project == null)
+            {
+                return NotFound();
+            }
+
+            return Ok(project);
+        }
+
+        // PUT: api/Projects/5
+        [ResponseType(typeof(void))]
+        public async Task<IHttpActionResult> PutProject(int id, Project project)
+        {
+            if (!ModelState.IsValid)
+            {
+                return BadRequest(ModelState);
+            }
+
+            if (id != project.id)
+            {
+                return BadRequest();
+            }
+
+            db.Entry(project).State = EntityState.Modified;
+
+            try
+            {
+                await db.SaveChangesAsync();
+            }
+            catch (DbUpdateConcurrencyException)
+            {
+                if (!ProjectExists(id))
+                {
+                    return NotFound();
+                }
+                else
+                {
+                    throw;
+                }
+            }
+
+            return StatusCode(HttpStatusCode.NoContent);
+        }
+
+        // POST: api/Projects
+        [ResponseType(typeof(Project))]
+        public async Task<IHttpActionResult> PostProject(Project project)
+        {
+            if (!ModelState.IsValid)
+            {
+                return BadRequest(ModelState);
+            }
+
+            db.Projects.Add(project);
+            await db.SaveChangesAsync();
+
+            return CreatedAtRoute("DefaultApi", new { id = project.id }, project);
+        }
+
+        // DELETE: api/Projects/5
+        [ResponseType(typeof(Project))]
+        public async Task<IHttpActionResult> DeleteProject(int id)
+        {
+            Project project = await db.Projects.FindAsync(id);
+            if (project == null)
+            {
+                return NotFound();
+            }
+
+            db.Projects.Remove(project);
+            await db.SaveChangesAsync();
+
+            return Ok(project);
+        }
+
+        protected override void Dispose(bool disposing)
+        {
+            if (disposing)
+            {
+                db.Dispose();
+            }
+            base.Dispose(disposing);
+        }
+
+        private bool ProjectExists(int id)
+        {
+            return db.Projects.Count(e => e.id == id) > 0;
+        }
+    }
+}
\ No newline at end of file