Skip to content

Instantly share code, notes, and snippets.

@motowilliams
Created February 22, 2012 21:35
Show Gist options
  • Save motowilliams/1887536 to your computer and use it in GitHub Desktop.
Save motowilliams/1887536 to your computer and use it in GitHub Desktop.
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