Created
February 22, 2012 21:35
-
-
Save motowilliams/1887536 to your computer and use it in GitHub Desktop.
This file contains hidden or 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.Net; | |
using System.Net.Http; | |
using System.Web.Http; | |
using ContactManager.Models; | |
namespace ContactManager.Controllers | |
{ | |
public class ContactController : ApiController | |
{ | |
private readonly IContactRepository repository; | |
public ContactController(IContactRepository repository) | |
{ | |
this.repository = repository; | |
} | |
public HttpResponseMessage<Contact> Get(int id) | |
{ | |
var contact = this.repository.Get(id); | |
if (contact == null) | |
{ | |
var response = new HttpResponseMessage(); | |
response.StatusCode = HttpStatusCode.NotFound; | |
response.Content = new StringContent("Contact not found"); | |
throw new HttpResponseException(response); | |
} | |
var contactResponse = new HttpResponseMessage<Contact>(contact); | |
//set it to expire in 5 minutes | |
contactResponse.Content.Headers.Expires = new DateTimeOffset(DateTime.Now.AddSeconds(30)); | |
return contactResponse; | |
} | |
public Contact Put(int id, Contact contact) | |
{ | |
this.repository.Get(id); | |
this.repository.Update(contact); | |
return contact; | |
} | |
public Contact Delete(int id) | |
{ | |
var deleted = this.repository.Get(id); | |
this.repository.Delete(id); | |
return deleted; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment