feat: introduce content library management for reusable content blocks with dedicated API endpoints and database schema.
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
-- Content Libraries: Repositorio reutilizable de bloques y lecciones
|
||||
-- Permite a instructores guardar y reutilizar componentes de contenido
|
||||
|
||||
-- Bloques reutilizables guardados en la biblioteca
|
||||
CREATE TABLE library_blocks (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
organization_id UUID NOT NULL,
|
||||
created_by UUID NOT NULL, -- El instructor que lo guardó
|
||||
name TEXT NOT NULL, -- Nombre descriptivo dado por el instructor
|
||||
description TEXT, -- Descripción opcional
|
||||
block_type TEXT NOT NULL, -- 'quiz', 'peer-review', 'hotspot', etc.
|
||||
block_data JSONB NOT NULL, -- El bloque completo (mismo formato que metadata.blocks)
|
||||
tags TEXT[], -- Array de etiquetas para búsqueda
|
||||
usage_count INTEGER DEFAULT 0, -- Contador de cuántas veces se ha usado
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
||||
);
|
||||
|
||||
-- Índices para búsqueda eficiente
|
||||
CREATE INDEX idx_library_blocks_org ON library_blocks(organization_id);
|
||||
CREATE INDEX idx_library_blocks_type ON library_blocks(block_type);
|
||||
CREATE INDEX idx_library_blocks_tags ON library_blocks USING GIN(tags);
|
||||
CREATE INDEX idx_library_blocks_created_by ON library_blocks(created_by);
|
||||
|
||||
-- Trigger para actualizar updated_at
|
||||
CREATE TRIGGER update_library_blocks_updated_at
|
||||
BEFORE UPDATE ON library_blocks
|
||||
FOR EACH ROW
|
||||
EXECUTE PROCEDURE update_updated_at_column();
|
||||
|
||||
-- Plantillas de lecciones completas (para futuras expansiones)
|
||||
CREATE TABLE library_templates (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
organization_id UUID NOT NULL,
|
||||
created_by UUID NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
description TEXT,
|
||||
lesson_data JSONB NOT NULL, -- Incluye metadata.blocks y configuración
|
||||
tags TEXT[],
|
||||
usage_count INTEGER DEFAULT 0,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
||||
);
|
||||
|
||||
CREATE INDEX idx_library_templates_org ON library_templates(organization_id);
|
||||
|
||||
-- Trigger para actualizar updated_at
|
||||
CREATE TRIGGER update_library_templates_updated_at
|
||||
BEFORE UPDATE ON library_templates
|
||||
FOR EACH ROW
|
||||
EXECUTE PROCEDURE update_updated_at_column();
|
||||
@@ -0,0 +1,238 @@
|
||||
use axum::{
|
||||
Json,
|
||||
extract::{Path, Query, State},
|
||||
http::StatusCode,
|
||||
};
|
||||
use common::models::{CreateLibraryBlockPayload, LibraryBlock, UpdateLibraryBlockPayload};
|
||||
use common::{auth::Claims, middleware::Org};
|
||||
use serde::Deserialize;
|
||||
use sqlx::PgPool;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct LibraryBlockFilters {
|
||||
#[serde(rename = "type")]
|
||||
pub block_type: Option<String>,
|
||||
pub tags: Option<String>, // Comma-separated list
|
||||
pub search: Option<String>,
|
||||
}
|
||||
|
||||
/// POST /api/library/blocks - Guardar un bloque en la biblioteca
|
||||
pub async fn create_library_block(
|
||||
Org(org_ctx): Org,
|
||||
claims: Claims,
|
||||
State(pool): State<PgPool>,
|
||||
Json(payload): Json<CreateLibraryBlockPayload>,
|
||||
) -> Result<Json<LibraryBlock>, (StatusCode, String)> {
|
||||
let block = sqlx::query_as!(
|
||||
LibraryBlock,
|
||||
r#"
|
||||
INSERT INTO library_blocks (organization_id, created_by, name, description, block_type, block_data, tags)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
RETURNING id, organization_id, created_by, name, description, block_type, block_data, tags, usage_count as "usage_count!", created_at, updated_at
|
||||
"#,
|
||||
org_ctx.id,
|
||||
claims.sub,
|
||||
payload.name,
|
||||
payload.description,
|
||||
payload.block_type,
|
||||
payload.block_data,
|
||||
payload.tags.as_deref()
|
||||
)
|
||||
.fetch_one(&pool)
|
||||
.await
|
||||
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
|
||||
|
||||
Ok(Json(block))
|
||||
}
|
||||
|
||||
/// GET /api/library/blocks - Listar bloques de la biblioteca
|
||||
pub async fn list_library_blocks(
|
||||
Org(org_ctx): Org,
|
||||
State(pool): State<PgPool>,
|
||||
Query(filters): Query<LibraryBlockFilters>,
|
||||
) -> Result<Json<Vec<LibraryBlock>>, (StatusCode, String)> {
|
||||
// Base query
|
||||
let mut query = String::from("SELECT * FROM library_blocks WHERE organization_id = $1");
|
||||
let mut param_count = 1;
|
||||
|
||||
// Filtro por tipo
|
||||
if filters.block_type.is_some() {
|
||||
param_count += 1;
|
||||
query.push_str(&format!(" AND block_type = ${}", param_count));
|
||||
}
|
||||
|
||||
// Filtro por tags (busca si algún tag coincide)
|
||||
if filters.tags.is_some() {
|
||||
param_count += 1;
|
||||
query.push_str(&format!(" AND tags && ${}", param_count));
|
||||
}
|
||||
|
||||
// Búsqueda en nombre y descripción
|
||||
if filters.search.is_some() {
|
||||
param_count += 1;
|
||||
query.push_str(&format!(
|
||||
" AND (name ILIKE ${0} OR description ILIKE ${0})",
|
||||
param_count
|
||||
));
|
||||
}
|
||||
|
||||
query.push_str(" ORDER BY created_at DESC");
|
||||
|
||||
// Build query con bind dinámico
|
||||
let mut sql_query = sqlx::query_as::<_, LibraryBlock>(&query).bind(org_ctx.id);
|
||||
|
||||
if let Some(block_type) = &filters.block_type {
|
||||
sql_query = sql_query.bind(block_type);
|
||||
}
|
||||
|
||||
if let Some(tags_str) = &filters.tags {
|
||||
let tags: Vec<String> = tags_str.split(',').map(|s| s.trim().to_string()).collect();
|
||||
sql_query = sql_query.bind(tags);
|
||||
}
|
||||
|
||||
let search_pattern = filters.search.as_ref().map(|s| format!("%{}%", s));
|
||||
if let Some(ref pattern) = search_pattern {
|
||||
sql_query = sql_query.bind(pattern);
|
||||
}
|
||||
|
||||
let blocks = sql_query
|
||||
.fetch_all(&pool)
|
||||
.await
|
||||
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
|
||||
|
||||
Ok(Json(blocks))
|
||||
}
|
||||
|
||||
/// GET /api/library/blocks/:id - Obtener un bloque específico
|
||||
pub async fn get_library_block(
|
||||
Org(org_ctx): Org,
|
||||
State(pool): State<PgPool>,
|
||||
Path(block_id): Path<Uuid>,
|
||||
) -> Result<Json<LibraryBlock>, (StatusCode, String)> {
|
||||
let block = sqlx::query_as!(
|
||||
LibraryBlock,
|
||||
r#"SELECT id, organization_id, created_by, name, description, block_type, block_data, tags, usage_count as "usage_count!", created_at, updated_at FROM library_blocks WHERE id = $1 AND organization_id = $2"#,
|
||||
block_id,
|
||||
org_ctx.id
|
||||
)
|
||||
.fetch_optional(&pool)
|
||||
.await
|
||||
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
|
||||
|
||||
match block {
|
||||
Some(b) => Ok(Json(b)),
|
||||
None => Err((StatusCode::NOT_FOUND, "Block not found".to_string())),
|
||||
}
|
||||
}
|
||||
|
||||
/// PUT /api/library/blocks/:id - Actualizar bloque (nombre, descripción, tags)
|
||||
pub async fn update_library_block(
|
||||
Org(org_ctx): Org,
|
||||
State(pool): State<PgPool>,
|
||||
Path(block_id): Path<Uuid>,
|
||||
Json(payload): Json<UpdateLibraryBlockPayload>,
|
||||
) -> Result<Json<LibraryBlock>, (StatusCode, String)> {
|
||||
// Verificar que el bloque existe y pertenece a la org
|
||||
let existing = sqlx::query!(
|
||||
"SELECT id FROM library_blocks WHERE id = $1 AND organization_id = $2",
|
||||
block_id,
|
||||
org_ctx.id
|
||||
)
|
||||
.fetch_optional(&pool)
|
||||
.await
|
||||
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
|
||||
|
||||
if existing.is_none() {
|
||||
return Err((StatusCode::NOT_FOUND, "Block not found".to_string()));
|
||||
}
|
||||
|
||||
// Update dinámico basado en campos provistos
|
||||
let updated = if let Some(name) = &payload.name {
|
||||
sqlx::query_as!(
|
||||
LibraryBlock,
|
||||
r#"
|
||||
UPDATE library_blocks
|
||||
SET name = COALESCE($1, name),
|
||||
description = COALESCE($2, description),
|
||||
tags = COALESCE($3, tags),
|
||||
updated_at = NOW()
|
||||
WHERE id = $4 AND organization_id = $5
|
||||
RETURNING id, organization_id, created_by, name, description, block_type, block_data, tags, usage_count as "usage_count!", created_at, updated_at
|
||||
"#,
|
||||
Some(name),
|
||||
payload.description,
|
||||
payload.tags.as_deref(),
|
||||
block_id,
|
||||
org_ctx.id
|
||||
)
|
||||
.fetch_one(&pool)
|
||||
.await
|
||||
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
|
||||
} else {
|
||||
sqlx::query_as!(
|
||||
LibraryBlock,
|
||||
r#"
|
||||
UPDATE library_blocks
|
||||
SET description = COALESCE($1, description),
|
||||
tags = COALESCE($2, tags),
|
||||
updated_at = NOW()
|
||||
WHERE id = $3 AND organization_id = $4
|
||||
RETURNING id, organization_id, created_by, name, description, block_type, block_data, tags, usage_count as "usage_count!", created_at, updated_at
|
||||
"#,
|
||||
payload.description,
|
||||
payload.tags.as_deref(),
|
||||
block_id,
|
||||
org_ctx.id
|
||||
)
|
||||
.fetch_one(&pool)
|
||||
.await
|
||||
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?
|
||||
};
|
||||
|
||||
Ok(Json(updated))
|
||||
}
|
||||
|
||||
/// DELETE /api/library/blocks/:id - Eliminar bloque
|
||||
pub async fn delete_library_block(
|
||||
Org(org_ctx): Org,
|
||||
State(pool): State<PgPool>,
|
||||
Path(block_id): Path<Uuid>,
|
||||
) -> Result<StatusCode, (StatusCode, String)> {
|
||||
let result = sqlx::query!(
|
||||
"DELETE FROM library_blocks WHERE id = $1 AND organization_id = $2",
|
||||
block_id,
|
||||
org_ctx.id
|
||||
)
|
||||
.execute(&pool)
|
||||
.await
|
||||
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
|
||||
|
||||
if result.rows_affected() == 0 {
|
||||
return Err((StatusCode::NOT_FOUND, "Block not found".to_string()));
|
||||
}
|
||||
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
}
|
||||
|
||||
/// POST /api/library/blocks/:id/increment-usage - Incrementar contador de uso
|
||||
pub async fn increment_block_usage(
|
||||
Org(org_ctx): Org,
|
||||
State(pool): State<PgPool>,
|
||||
Path(block_id): Path<Uuid>,
|
||||
) -> Result<StatusCode, (StatusCode, String)> {
|
||||
let result = sqlx::query!(
|
||||
"UPDATE library_blocks SET usage_count = usage_count + 1 WHERE id = $1 AND organization_id = $2",
|
||||
block_id,
|
||||
org_ctx.id
|
||||
)
|
||||
.execute(&pool)
|
||||
.await
|
||||
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, e.to_string()))?;
|
||||
|
||||
if result.rows_affected() == 0 {
|
||||
return Err((StatusCode::NOT_FOUND, "Block not found".to_string()));
|
||||
}
|
||||
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
@@ -1,9 +1,10 @@
|
||||
mod db_util;
|
||||
pub mod exporter;
|
||||
mod external_handlers;
|
||||
mod handlers;
|
||||
mod handlers_branding;
|
||||
mod handlers_library;
|
||||
mod webhooks;
|
||||
mod external_handlers;
|
||||
|
||||
use axum::{
|
||||
Router,
|
||||
@@ -139,7 +140,10 @@ async fn main() {
|
||||
get(handlers::get_grading_categories),
|
||||
)
|
||||
.route("/auth/me", get(handlers::get_me))
|
||||
.route("/users", get(handlers::get_all_users).post(handlers::admin_create_user))
|
||||
.route(
|
||||
"/users",
|
||||
get(handlers::get_all_users).post(handlers::admin_create_user),
|
||||
)
|
||||
.route("/users/{id}", axum::routing::put(handlers::update_user))
|
||||
.route("/audit-logs", get(handlers::get_audit_logs))
|
||||
.route("/api/ai/review-text", post(handlers::review_text))
|
||||
@@ -177,14 +181,38 @@ async fn main() {
|
||||
"/organizations/{id}/branding",
|
||||
axum::routing::put(handlers_branding::update_organization_branding),
|
||||
)
|
||||
// Content Libraries routes
|
||||
.route(
|
||||
"/library/blocks",
|
||||
get(handlers_library::list_library_blocks).post(handlers_library::create_library_block),
|
||||
)
|
||||
.route(
|
||||
"/library/blocks/{id}",
|
||||
get(handlers_library::get_library_block)
|
||||
.put(handlers_library::update_library_block)
|
||||
.delete(handlers_library::delete_library_block),
|
||||
)
|
||||
.route(
|
||||
"/library/blocks/{id}/increment-usage",
|
||||
post(handlers_library::increment_block_usage),
|
||||
)
|
||||
.route_layer(middleware::from_fn(
|
||||
common::middleware::org_extractor_middleware,
|
||||
));
|
||||
|
||||
let api_routes = Router::new()
|
||||
.route("/v1/courses", post(external_handlers::create_course_external))
|
||||
.route("/v1/courses/{id}", get(external_handlers::get_course_external))
|
||||
.route("/v1/lessons/{id}/transcribe", post(external_handlers::trigger_transcription_external));
|
||||
.route(
|
||||
"/v1/courses",
|
||||
post(external_handlers::create_course_external),
|
||||
)
|
||||
.route(
|
||||
"/v1/courses/{id}",
|
||||
get(external_handlers::get_course_external),
|
||||
)
|
||||
.route(
|
||||
"/v1/lessons/{id}/transcribe",
|
||||
post(external_handlers::trigger_transcription_external),
|
||||
);
|
||||
|
||||
// Rutas públicas que no requieren autenticación
|
||||
let public_routes = Router::new()
|
||||
|
||||
@@ -178,7 +178,7 @@ pub async fn submit_peer_review(
|
||||
}
|
||||
|
||||
pub async fn get_my_submission_feedback(
|
||||
Org(org_ctx): Org,
|
||||
Org(_org_ctx): Org,
|
||||
claims: Claims,
|
||||
State(pool): State<PgPool>,
|
||||
Path((_course_id, lesson_id)): Path<(Uuid, Uuid)>,
|
||||
|
||||
Reference in New Issue
Block a user