Skip to content

Instantly share code, notes, and snippets.

@benfoster
Created September 3, 2012 16:51
Show Gist options
  • Save benfoster/3610765 to your computer and use it in GitHub Desktop.
Save benfoster/3610765 to your computer and use it in GitHub Desktop.
A production Web Api Controller
/// <summary>
/// Page Resource API.
/// </summary>
public class PagesController : ApiControllerBase
{
/// <summary>
/// Get pages from the specified site.
/// </summary>
/// <param name="siteId">The site identifier.</param>
/// <param name="command">A command containing result paging parameters.</param>
/// <example>
/// GET api/sites/1/pages
/// </example>
public IPagedResult<Page> Get(int siteId, PagingCommand command)
{
RavenQueryStatistics stats;
var pages = Session.Query<Domain.Page>()
.Statistics(out stats)
.NotDeleted()
.FromSite(Session.GetSiteId(siteId))
.GetPage(command.PageIndex, command.PageSize)
.ToList();
return Mapper.MapPaged<Page>(pages, command.PageIndex, command.PageSize, stats.TotalResults)
.WithPageLinks(Request.RequestUri)
.ApplyToResults(p => p.WithSelfAndEditLinks(GetPageUri(siteId, p.Id)));
}
/// <summary>
/// Get a single page from the specified site.
/// </summary>
/// <param name="siteId">The site identifier.</param>
/// <param name="id">The page identifier.</param>
/// <example>
/// GET api/sites/1/pages/5
/// </example>
public Page Get(int siteId, int id)
{
var page = Session.TryLoadFromSite<Domain.Page>(siteId, id);
return Mapper.Map<Page>(page)
.WithSelfAndEditLinks(GetPageUri(siteId, id));
}
/// <summary>
/// Add a new page to the specified site.
/// </summary>
/// <param name="siteId">The site identifier.</param>
/// <param name="command">A command containing details of the page to add.</param>
/// <example>
/// POST api/sites/1/pages
/// </example>
public HttpResponseMessage Post(int siteId, AddPageCommand command)
{
var page = new Domain.Page(
Session.GetSiteId(siteId),
command.Title,
command.Slug,
command.Summary,
command.ContentType,
command.Content,
command.Tags,
command.Published
);
Session.Store(page);
return Created(new Uri(GetPageUri(siteId, page.Id.ToIntId())));
}
// PUT api/pages/1
/// <summary>
/// Update a page from the specified site.
/// </summary>
/// <param name="siteId">The site identifier.</param>
/// <param name="id">The page identifier.</param>
/// <param name="command">A command containing the updated page details.</param>
/// <example>
/// PUT api/sites/1/pages/5
/// </example>
public void Put(int siteId, int id, UpdatePageCommand command)
{
var page = Session.TryLoadFromSite<Domain.Page>(siteId, id);
page.Update(
command.Title,
command.Slug,
command.Summary,
command.ContentType,
command.Content,
command.Tags,
command.Published
);
}
/// <summary>
/// Removes a page from the specified site.
/// </summary>
/// <param name="siteId">The site identifier.</param>
/// <param name="id">The page identifier.</param>
/// <example>
/// DELETE api/sites/1/pages/5
/// </example>
public void Delete(int siteId, int id)
{
var page = Session.TryLoadFromSite<Domain.Page>(siteId, id);
page.Delete();
}
private string GetPageUri(int siteId, int pageId)
{
return Url.Link("SiteApi", new { controller = "pages", siteId = siteId, id = pageId });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment