Created
January 2, 2015 07:30
-
-
Save wcadap/9225eb039b6bf17a7b44 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 Contacts.Helpers; | |
using Contacts.Models; | |
using System.Web.Mvc; | |
using System.Linq; | |
using System.Net; | |
namespace Contacts.Controllers | |
{ | |
public class ContactController : JsonController | |
{ | |
private ContactContext ContactsDB = new ContactContext(); | |
public ActionResult GetContacts() | |
{ | |
var Contacts = ContactsDB.Contacts; | |
return Json(Contacts, JsonRequestBehavior.AllowGet); | |
} | |
public ActionResult AddContact(Contact _Contact) | |
{ | |
try | |
{ | |
ContactsDB.Contacts.Add(_Contact); | |
ContactsDB.SaveChanges(); | |
return new HttpStatusCodeResult(HttpStatusCode.Created); // OK = 200 | |
} catch | |
{ | |
return new HttpStatusCodeResult(HttpStatusCode.BadRequest); | |
} | |
} | |
public ActionResult UpdateContact(Contact _Contact) | |
{ | |
try | |
{ | |
ContactsDB.Entry(_Contact).State = System.Data.Entity.EntityState.Modified; | |
ContactsDB.SaveChanges(); | |
return new HttpStatusCodeResult(HttpStatusCode.Accepted); | |
} catch | |
{ | |
return new HttpStatusCodeResult(HttpStatusCode.BadRequest); | |
} | |
} | |
public ActionResult DeleteContact(int id) | |
{ | |
var _Contact = ContactsDB.Contacts.First(r => r.Id == id); | |
try { | |
ContactsDB.Contacts.Remove(_Contact); | |
ContactsDB.SaveChanges(); | |
return new HttpStatusCodeResult(HttpStatusCode.OK); // OK = 200 | |
} | |
catch | |
{ | |
return new HttpStatusCodeResult(HttpStatusCode.BadRequest); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment