Relax.Resource.Update behaviour
Include in your resource to respond to PUT /:id and PATCH /:id.
Typically brought into your resource via use Relax.Resource
.
Update defines two callback behaviours, one of which (update/2
) must be
implemented.
In addition this module includes the behaviours
Relax.Resource.FetchOne
- to find the model to update.Relax.Resource.Fetchable
- to find the model to update.Relax.Resource.PermittiedParams
- to whitelist the attributes for update.
Summary
halt_not_found(model, conn) |
Types
updateable :: %{}
Functions
Callbacks
Specs:
- update(Plug.Conn.t, updateable, %{}) :: updateable | Plug.Conn.t
Update (or attempt to update) an existing record.
Receives the conn, the model as found by fetch_one/2
and the whitelisted
and formatted params. See Relax.Resource.PermittedParams for more information
on whitelisting params.
Update often returns an Ecto.Changeset, which will be saved and formated as is appropriate.
def update(_conn, post, attributes) do
Models.Post.changeset(post, attributes)
end
Alternatively a tuple of either {:ok, model}
or {:error, errors}
can be
returned.
A conn may also be returned:
def update(conn), do: halt send_resp(conn, 401, "nope")
Specs:
- update_resource(Plug.Conn.t, id) :: Plug.Conn.t
This callback can be used to completely override the update action.
It accepts a Plug.Conn and must return a Plug.Conn.t