69dcfec4eb
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.
40 lines
1.5 KiB
Markdown
40 lines
1.5 KiB
Markdown
---
|
|
name: crud_generate_handlers
|
|
kind: function
|
|
lang: go
|
|
domain: infra
|
|
version: "1.0.0"
|
|
purity: pure
|
|
signature: "func CRUDGenerateHandlers(res CRUDResource, db *sql.DB) map[string]http.HandlerFunc"
|
|
description: "Compone los 5 handlers CRUD (list, get, create, update, delete) en un mapa con claves estandar. La funcion factory es pura — solo construye closures a partir de la definicion y la conexion de bd. Los handlers resultantes son impuros al invocarse."
|
|
tags: [crud, factory, handlers, compose, http, infra]
|
|
uses_functions: [crud_list_handler_go_infra, crud_get_handler_go_infra, crud_create_handler_go_infra, crud_update_handler_go_infra, crud_delete_handler_go_infra]
|
|
uses_types: [CRUDResource_go_infra]
|
|
returns: []
|
|
returns_optional: false
|
|
error_type: ""
|
|
imports: [database/sql, net/http]
|
|
params:
|
|
- name: res
|
|
desc: "definicion CRUDResource del recurso"
|
|
- name: db
|
|
desc: "conexion *sql.DB a SQLite"
|
|
output: "map con keys 'list', 'get', 'create', 'update', 'delete' -> http.HandlerFunc"
|
|
tested: true
|
|
tests: ["retorna las 5 keys esperadas", "cada handler funciona end-to-end"]
|
|
test_file_path: "functions/infra/crud_test.go"
|
|
file_path: "functions/infra/crud_generate_handlers.go"
|
|
---
|
|
|
|
## Ejemplo
|
|
|
|
```go
|
|
handlers := CRUDGenerateHandlers(res, db)
|
|
mux.Handle("GET /api/projects", handlers["list"])
|
|
mux.Handle("POST /api/projects", handlers["create"])
|
|
```
|
|
|
|
## Notas
|
|
|
|
Funcion pura — solo ensambla closures. Para registrar todas las rutas en un paso, ver CRUDRegisterRoutes. El mapa incluye exactamente las claves list, get, create, update, delete (en minuscula).
|