Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created May 25, 2017 23:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcomartin/176beb08e9cf81256a20df740430b25c to your computer and use it in GitHub Desktop.
Save dcomartin/176beb08e9cf81256a20df740430b25c to your computer and use it in GitHub Desktop.
public class TodoConverter : IStateConverter
{
public Task<object> ConvertAsync(StateConverterContext context)
{
var urlHelper = context.HttpContext.RequestServices.GetRequiredService<IUrlHelper>();
var todo = context.Object as TodoModel;
var actions = new List<Siren.Core.Action>();
actions.Add(new Siren.Core.Action
{
Name = "delete",
Title = "Delete Todo",
Method = "DELETE",
Href = urlHelper.Action("Delete", "Todo"),
});
if (todo.IsCompleted == false)
{
actions.Add(
new Siren.Core.Action
{
Name = "complete",
Title = "Complete Todo",
Method = "PUT",
Href = urlHelper.Action("Complete", "Todo"),
});
}
var document = new Document
{
Class = new Class { "todo" },
Properties = todo,
Href = urlHelper.Action("Get", "Todo"),
Actions = new Actions(actions)
};
return Task.FromResult<object>(document);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment