Created
July 23, 2017 20:49
-
-
Save CodingTricksHub/f0e748595de378219b378b8112274c89 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.Web.Http; | |
using System.Web.Http.Description; | |
using PhoneBookWebApi.Models; | |
using System.Web.Http.Cors; | |
namespace PhoneBookWebApi.Controllers | |
{ | |
[EnableCors(origins: "*", headers: "*", methods: "*")] | |
public class DirectoriesController : ApiController | |
{ | |
private PhoneBookEntities db = new PhoneBookEntities(); | |
// GET: api/Directories | |
public IQueryable<Directory> GetDirectories() | |
{ | |
return db.Directories; | |
} | |
// GET: api/Directories/5 | |
[ResponseType(typeof(Directory))] | |
public IHttpActionResult GetDirectory(int id) | |
{ | |
Directory directory = db.Directories.Find(id); | |
if (directory == null) | |
{ | |
return NotFound(); | |
} | |
return Ok(directory); | |
} | |
// PUT: api/Directories/5 | |
[ResponseType(typeof(void))] | |
public IHttpActionResult PutDirectory(int id, Directory directory) | |
{ | |
if (!ModelState.IsValid) | |
{ | |
return BadRequest(ModelState); | |
} | |
if (id != directory.Id) | |
{ | |
return BadRequest(); | |
} | |
db.Entry(directory).State = EntityState.Modified; | |
try | |
{ | |
db.SaveChanges(); | |
} | |
catch (DbUpdateConcurrencyException) | |
{ | |
if (!DirectoryExists(id)) | |
{ | |
return NotFound(); | |
} | |
else | |
{ | |
throw; | |
} | |
} | |
return StatusCode(HttpStatusCode.NoContent); | |
} | |
// POST: api/Directories | |
[ResponseType(typeof(Directory))] | |
public IHttpActionResult PostDirectory(Directory directory) | |
{ | |
if (!ModelState.IsValid) | |
{ | |
return BadRequest(ModelState); | |
} | |
db.Directories.Add(directory); | |
db.SaveChanges(); | |
return CreatedAtRoute("DefaultApi", new { id = directory.Id }, directory); | |
} | |
// DELETE: api/Directories/5 | |
[ResponseType(typeof(Directory))] | |
public IHttpActionResult DeleteDirectory(int id) | |
{ | |
Directory directory = db.Directories.Find(id); | |
if (directory == null) | |
{ | |
return NotFound(); | |
} | |
db.Directories.Remove(directory); | |
db.SaveChanges(); | |
return Ok(directory); | |
} | |
protected override void Dispose(bool disposing) | |
{ | |
if (disposing) | |
{ | |
db.Dispose(); | |
} | |
base.Dispose(disposing); | |
} | |
private bool DirectoryExists(int id) | |
{ | |
return db.Directories.Count(e => e.Id == id) > 0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment