repository

command
v14.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 12, 2026 License: BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Overview

mvc.UseRepository: the six resource routes over a repository, with no controller in between.

When a resource's HTTP half adds nothing to its persistence half there is no type left to write, so UseRepository generates the routes straight from a mvc.Repository[T, ID, Filter]:

POST   /notes              -> 201 {"id": ...}
GET    /notes/{note_id}    -> 200 the model
PUT    /notes/{note_id}    -> 204, or 304 when nothing changed
PATCH  /notes/{note_id}    -> the same, honouring ?columns=a&columns=b
DELETE /notes/{note_id}    -> 204, or 304 when nothing was deleted
POST   /notes/list         -> 200 a page of models

The path parameter's name comes from the model (Note becomes note_id) and its macro from the ID type, so a uuid.UUID id would register {note_id:uuid} and validate before any handler runs; a string id stays {note_id:string}.

The four options below are the ones worth knowing:

mvc.Guard        middleware for the write routes only, so reads stay public
mvc.Pagination   the list route's sort policy, checked before the repository
mvc.BeforeWrite  a hook over the decoded body, for values the client must
                 not choose (here the owner, taken from the API key)
mvc.Without      drops a generated route entirely (not used here)

The update routes read their columns through ctx.BindUpdate, so a column that is not a member of Note answers 400 naming it and the members it could have been, instead of updating nothing and reporting success. The names that reach the repository are still the client's: a repository building SQL from them maps them to its own columns and never interpolates them, which is what the switch in Update does.

Learn: serve a resource straight from an mvc.Repository with mvc.UseRepository, guard the writes, bound the sort columns and stamp server-owned fields before they are stored. Before: ../mvc-generics

Run:

go run main.go

Try:

K='X-API-Key: editor-key'

curl -i -X POST localhost:8080/notes -H "$K" -d '{"title":"read the mvc chapter","body":"start at UseController"}'
                                                       # 201 {"id":"note-1"}
curl -i -X POST localhost:8080/notes -d '{"title":"anonymous"}'
                                                       # 401 UNAUTHENTICATED, writes are guarded
curl -i localhost:8080/notes/note-1                    # 200, owner "editor"
curl -i localhost:8080/notes/note-9                    # 404 NOT_FOUND
curl -i -X PUT 'localhost:8080/notes/note-1?columns=title' -H "$K" -d '{"title":"renamed"}'
                                                       # 204 No Content
curl -i -X PUT 'localhost:8080/notes/note-1?columns=title' -H "$K" -d '{"title":"renamed"}'
                                                       # 304 Not Modified, the same value again
curl -i -X PUT 'localhost:8080/notes/note-1?columns=titel' -H "$K" -d '{"title":"typo"}'
                                                       # 400 INVALID_ARGUMENT, unknown column
curl -i -X POST localhost:8080/notes/list -d '{"sort":"title"}'
                                                       # 200, the Page envelope
curl -i -X POST localhost:8080/notes/list -d '{"sort":"body"}'
                                                       # 400 INVALID_ARGUMENT, sort not allowed
curl -i -X DELETE localhost:8080/notes/note-1 -H "$K"  # 204 No Content
curl -i -X DELETE localhost:8080/notes/note-1 -H "$K"  # 304 Not Modified, already gone

Next: ../../04-request/query-and-forms

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL