This example constructs an edit form for use to update a resource on a singleton resource. This example updates a todo resource (wither a list or item). Navigate to this resource from the home via the ‘me’ > ‘todos’ > pick a todo list.
edit-form
link relationMethod | link rel | associated route | notes |
---|---|---|---|
GET | /todo/{id} | add
edit-form
as a link rel to singleton |
|
GET | edit-form |
/todo/form/edit | follow the
edit-form
link to get the form |
PUT | self |
/todo/{id} | fill in the form and send back to singleton. Response returns
204 No Content |
GET | /todo/{id} | optionally get the resource to populate caches |
Note: by convention a form can be submitted semantically. Here, because there is no
submit
link rel on the form, the forms is send back to the ‘self’ rel on the originating resource. Note: this design requires the client to know that an ‘edit-form’ is an update. Alternatively, the submit link could have only ‘type=PUT’ which would further decouple the client.
Below, you see on overview of the code, then you see the implementation.
Below, you see on overview of the code, then you see the implementation.
Below, you see on overview of the code, then you see the implementation.
Create a route in the controller that accepts the the create representation and returns a 201 Created
with Location
header to the canonical resource. Also included is the route for that canonical resource
using System.Collections.Generic;
using System.Threading.Tasks;
using Api.Authorisation;
using Api.RepresentationExtensions;
using Api.UriFactory;
using Api.Web;
using Domain.Models;
using Domain.Persistence;
using Domain.Representation;
using Marvin.Cache.Headers;
using Microsoft.AspNetCore.JsonPatch;
using Microsoft.AspNetCore.Mvc;
using SemanticLink;
using SemanticLink.AspNetCore;
using SemanticLink.Form;
using Toolkit;
namespace Api.Controllers
{
/// <see cref="UserController.GetUserTodos"/> for the todo collection as they are parented on a user
[Route("todo")]
public class TodoController : Controller
{
/// <summary>
/// Update a todo list or item
/// </summary>
[HttpPut("{id}", Name = TodoUriFactory.TodoRouteName)]
[AuthoriseTodo(Permission.Put)]
public async Task<NoContentResult> Update(string id, [FromBody] TodoRepresentation item)
{
await _todoStore.Update(id,
todo =>
{
todo.Name = item.Name
.ThrowInvalidDataExceptionIfNullOrWhiteSpace("A todo must have a name");
todo.State = item.State;
todo.Due = item.Due;
});
return NoContent();
}
}
}```
</Instruction>