28599436e5
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.
1.5 KiB
1.5 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_delete_handler | function | go | infra | 1.0.0 | impure | func CRUDDeleteHandler(res CRUDResource, db *sql.DB) http.HandlerFunc | 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. |
|
|
|
false | error_go_core |
|
|
http.HandlerFunc que borra un registro | true |
|
functions/infra/crud_test.go | functions/infra/crud_delete_handler.go |
Ejemplo
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).