Skip to main content

headless_lms_server/controllers/main_frontend/
chatbot_models.rs

1//! Controllers for requests starting with `/api/v0/main-frontend/chatbot-models/`.
2use crate::prelude::*;
3use utoipa::OpenApi;
4
5use models::chatbot_configurations_models::ChatbotConfigurationModel;
6
7#[derive(OpenApi)]
8#[openapi(paths(get_model, get_all_models))]
9pub(crate) struct MainFrontendChatbotModelsApiDoc;
10
11#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
12
13pub struct CourseInfo {
14    course_id: Option<Uuid>,
15}
16
17/// GET `/api/v0/main-frontend/chatbot-models/{chatbot_configuration_id}`
18#[utoipa::path(
19    get,
20    path = "/{chatbot_model_id}",
21    operation_id = "getChatbotModel",
22    tag = "chatbot-models",
23    params(
24        ("chatbot_model_id" = Uuid, Path, description = "Chatbot model id")
25    ),
26    request_body(
27        content = Uuid,
28        content_type = "application/json"
29    ),
30    responses(
31        (status = 200, description = "Chatbot model", body = ChatbotConfigurationModel)
32    )
33)]
34#[instrument(skip(pool))]
35async fn get_model(
36    chatbot_model_id: web::Path<Uuid>,
37    payload: web::Json<Uuid>,
38    pool: web::Data<PgPool>,
39    user: AuthUser,
40) -> ControllerResult<web::Json<ChatbotConfigurationModel>> {
41    let mut conn = pool.acquire().await?;
42    let model =
43        models::chatbot_configurations_models::get_by_id(&mut conn, *chatbot_model_id).await?;
44    let token = authorize(&mut conn, Act::Edit, Some(user.id), Res::Course(*payload)).await?;
45
46    token.authorized_ok(web::Json(model))
47}
48
49/// GET `/api/v0/main-frontend/chatbot-models?course_id={course_id}`
50#[utoipa::path(
51    get,
52    path = "/",
53    operation_id = "getChatbotModels",
54    tag = "chatbot-models",
55    params(
56        ("course_id" = Option<Uuid>, Query, description = "Course id")
57    ),
58    responses(
59        (status = 200, description = "Chatbot models", body = Vec<ChatbotConfigurationModel>)
60    )
61)]
62#[instrument(skip(pool))]
63async fn get_all_models(
64    course_info: web::Query<CourseInfo>,
65    pool: web::Data<PgPool>,
66    user: AuthUser,
67) -> ControllerResult<web::Json<Vec<ChatbotConfigurationModel>>> {
68    let mut conn = pool.acquire().await?;
69    let models = models::chatbot_configurations_models::get_all(&mut conn).await?;
70    let token = if let Some(course_id) = course_info.course_id {
71        authorize(&mut conn, Act::Edit, Some(user.id), Res::Course(course_id)).await?
72    } else {
73        authorize(&mut conn, Act::Edit, Some(user.id), Res::GlobalPermissions).await?
74    };
75
76    token.authorized_ok(web::Json(models))
77}
78
79pub fn _add_routes(cfg: &mut web::ServiceConfig) {
80    cfg.route("/{chatbot_model_id}", web::get().to(get_model))
81        .route("/", web::get().to(get_all_models));
82}