Files
fn_registry/functions/infra/crud_create_handler.md
T
egutierrez 28599436e5 feat(crud): handlers HTTP y registro de rutas para recursos CRUD
Anade los 5 handlers CRUD genericos (list, get, create, update, delete) a
partir de un CRUDResource y *sql.DB, la factory crud_generate_handlers que
compone los 5 en un mapa, y crud_register_routes que registra todas las
rutas REST en un http.ServeMux con la sintaxis METHOD /path de Go 1.22+.

Caracteristicas:
- List con paginacion (page, per_page), orden (sort_by, sort_dir) y
  filtros exactos (filter_<campo>), validando nombres de columna contra
  la definicion del recurso para evitar SQL injection.
- Create valida required y validaciones (min/max, min_length/max_length,
  pattern, enum) antes de insertar; mapea UNIQUE violations a 409.
- Update hace partial update — solo los campos presentes en el JSON.
- Delete hace hard delete o soft delete segun CRUDResource.SoftDelete.
- UUIDs generados via github.com/google/uuid; timestamps en RFC3339Nano UTC.

Los handlers usan las funciones HTTP del registry (http_json_response,
http_error_response, http_parse_body) y se pueden componer con el mux
via http_router.
2026-04-18 17:15:33 +02:00

1.8 KiB

name, kind, lang, domain, version, purity, signature, description, tags, uses_functions, uses_types, returns, returns_optional, error_type, imports, params, output, tested, tests, test_file_path, file_path
name kind lang domain version purity signature description tags uses_functions uses_types returns returns_optional error_type imports params output tested tests test_file_path file_path
crud_create_handler function go infra 1.0.0 impure func CRUDCreateHandler(res CRUDResource, db *sql.DB) http.HandlerFunc Genera un handler HTTP POST que parsea JSON, valida campos contra la definicion del recurso, genera UUID y timestamps, inserta en SQLite y responde 201 con el registro. 400 en errores de validacion, 409 en violaciones UNIQUE.
crud
create
handler
http
sqlite
uuid
validation
infra
http_json_response_go_infra
http_error_response_go_infra
http_parse_body_go_infra
CRUDResource_go_infra
HTTPError_go_infra
false error_go_core
database/sql
fmt
net/http
strings
time
github.com/google/uuid
name desc
res definicion del recurso con campos y validaciones
name desc
db conexion *sql.DB a SQLite
http.HandlerFunc que crea un registro y responde 201 true
crea un registro valido y retorna 201
valida campos required y retorna 400 si faltan
valida min_length y max_length
valida enum de texto
valida min y max numericos
retorna 409 si se viola UNIQUE
functions/infra/crud_test.go functions/infra/crud_create_handler.go

Ejemplo

handler := CRUDCreateHandler(res, db)
mux.Handle("POST /api/projects", handler)
// curl -X POST localhost:8080/api/projects -H 'Content-Type: application/json' -d '{"name":"mi-proyecto"}'

Notas

Impura. Limita el body a 1 MiB. Los ids se generan con github.com/google/uuid (string). Los timestamps created_at y updated_at se escriben en formato RFC3339 UTC con nanosegundos. Los errores de validacion devuelven 400 con code "validation_error" y mensaje descriptivo. Errores UNIQUE de SQLite se mapean a 409.