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.
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
---
|
||||
name: crud_delete_handler
|
||||
kind: function
|
||||
lang: go
|
||||
domain: infra
|
||||
version: "1.0.0"
|
||||
purity: impure
|
||||
signature: "func CRUDDeleteHandler(res CRUDResource, db *sql.DB) http.HandlerFunc"
|
||||
description: "Genera un handler HTTP DELETE /{id} que borra un registro. Si el recurso es SoftDelete, hace UPDATE deleted_at en vez de DELETE. Responde 204 sin body, 404 si no existe."
|
||||
tags: [crud, delete, handler, http, sqlite, soft-delete, infra]
|
||||
uses_functions: [http_error_response_go_infra]
|
||||
uses_types: [CRUDResource_go_infra, HTTPError_go_infra]
|
||||
returns: []
|
||||
returns_optional: false
|
||||
error_type: "error_go_core"
|
||||
imports: [database/sql, fmt, net/http, time]
|
||||
params:
|
||||
- name: res
|
||||
desc: "definicion del recurso (SoftDelete determina el modo de borrado)"
|
||||
- name: db
|
||||
desc: "conexion *sql.DB a SQLite"
|
||||
output: "http.HandlerFunc que borra un registro"
|
||||
tested: true
|
||||
tests: ["hard delete fisico si soft_delete false", "soft delete via UPDATE deleted_at si soft_delete true", "retorna 404 si no existe", "retorna 204 sin body"]
|
||||
test_file_path: "functions/infra/crud_test.go"
|
||||
file_path: "functions/infra/crud_delete_handler.go"
|
||||
---
|
||||
|
||||
## Ejemplo
|
||||
|
||||
```go
|
||||
handler := CRUDDeleteHandler(res, db)
|
||||
mux.Handle("DELETE /api/projects/{id}", handler)
|
||||
// curl -X DELETE localhost:8080/api/projects/abc-123
|
||||
```
|
||||
|
||||
## Notas
|
||||
|
||||
Impura. Responde 204 No Content sin body en exito (convencion REST). Si el recurso es SoftDelete, actualiza deleted_at y updated_at con el timestamp actual, preservando el registro para auditoria. Un segundo DELETE sobre un recurso soft-deleted responde 404 (se considera que ya fue borrado).
|
||||
Reference in New Issue
Block a user