Update Singleton Resource

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.

update todo resource

Goal

  • Update a resource with an edit-form link relation
  • Create cachable edit form (designed to be put back onto the resource)

Anatomy of update

Methodlink relassociated routenotes
GET/todo/{id}add edit-form as a link rel to singleton
GETedit-form/todo/form/editfollow the edit-form link to get the form
PUTself/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.

Anatomy of code

Return todo (with edit-form link)

Below, you see on overview of the code, then you see the implementation.

Edit form

Below, you see on overview of the code, then you see the implementation.

Create the resource upon submission

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

...todo-hypermedia/api/Api/Controllers/TodoController.cs
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>
Unlock the next chapter
Updating a singleton is going to require responding to which verb(s)?
POST
PUT
PUT & PATCH
POST, PUT & PATCH