headless_lms_server/controllers/main_frontend/
chatbots.rs

1//! Controllers for requests starting with `/api/v0/main-frontend/chatbots/`.
2use crate::prelude::*;
3use utoipa::OpenApi;
4
5use models::chatbot_configurations::{ChatbotConfiguration, NewChatbotConf};
6
7#[derive(OpenApi)]
8#[openapi(paths(get_chatbot, edit_chatbot, delete_chatbot))]
9pub(crate) struct MainFrontendChatbotsApiDoc;
10
11/// GET `/api/v0/main-frontend/chatbots/{chatbot_configuration_id}`
12#[utoipa::path(
13    get,
14    path = "/{chatbot_configuration_id}",
15    operation_id = "getChatbotConfiguration",
16    tag = "chatbots",
17    params(
18        ("chatbot_configuration_id" = Uuid, Path, description = "Chatbot configuration id")
19    ),
20    responses(
21        (status = 200, description = "Chatbot configuration", body = ChatbotConfiguration)
22    )
23)]
24#[instrument(skip(pool))]
25async fn get_chatbot(
26    chatbot_configuration_id: web::Path<Uuid>,
27    pool: web::Data<PgPool>,
28    user: AuthUser,
29) -> ControllerResult<web::Json<ChatbotConfiguration>> {
30    let mut conn = pool.acquire().await?;
31    let configuration =
32        models::chatbot_configurations::get_by_id(&mut conn, *chatbot_configuration_id).await?;
33    let token = authorize(
34        &mut conn,
35        Act::Edit,
36        Some(user.id),
37        Res::Course(configuration.course_id),
38    )
39    .await?;
40
41    token.authorized_ok(web::Json(configuration))
42}
43
44/// POST `/api/v0/main-frontend/chatbots/{chatbot_configuration_id}`
45#[utoipa::path(
46    post,
47    path = "/{chatbot_configuration_id}",
48    operation_id = "configureChatbot",
49    tag = "chatbots",
50    params(
51        ("chatbot_configuration_id" = Uuid, Path, description = "Chatbot configuration id")
52    ),
53    request_body = NewChatbotConf,
54    responses(
55        (status = 200, description = "Updated chatbot configuration", body = ChatbotConfiguration)
56    )
57)]
58#[instrument(skip(pool, payload))]
59async fn edit_chatbot(
60    chatbot_configuration_id: web::Path<Uuid>,
61    payload: web::Json<NewChatbotConf>,
62    pool: web::Data<PgPool>,
63    user: AuthUser,
64) -> ControllerResult<web::Json<ChatbotConfiguration>> {
65    let mut conn = pool.acquire().await?;
66    let chatbot =
67        models::chatbot_configurations::get_by_id(&mut conn, *chatbot_configuration_id).await?;
68    let token = authorize(
69        &mut conn,
70        Act::Edit,
71        Some(user.id),
72        Res::Course(chatbot.course_id),
73    )
74    .await?;
75
76    let configuration: ChatbotConfiguration = models::chatbot_configurations::edit(
77        &mut conn,
78        payload.into_inner(),
79        *chatbot_configuration_id,
80    )
81    .await?;
82    token.authorized_ok(web::Json(configuration))
83}
84
85/// DELETE `/api/v0/main-frontend/chatbots/{chatbot_configuration_id}`
86#[utoipa::path(
87    delete,
88    path = "/{chatbot_configuration_id}",
89    operation_id = "deleteChatbotConfiguration",
90    tag = "chatbots",
91    params(
92        ("chatbot_configuration_id" = Uuid, Path, description = "Chatbot configuration id")
93    ),
94    responses(
95        (status = 200, description = "Deleted chatbot configuration")
96    )
97)]
98#[instrument(skip(pool))]
99async fn delete_chatbot(
100    chatbot_configuration_id: web::Path<Uuid>,
101    pool: web::Data<PgPool>,
102    user: AuthUser,
103) -> ControllerResult<web::Json<()>> {
104    let mut conn = pool.acquire().await?;
105    let chatbot =
106        models::chatbot_configurations::get_by_id(&mut conn, *chatbot_configuration_id).await?;
107    let token = authorize(
108        &mut conn,
109        Act::Edit,
110        Some(user.id),
111        Res::Course(chatbot.course_id),
112    )
113    .await?;
114    models::chatbot_configurations::delete(&mut conn, *chatbot_configuration_id).await?;
115
116    token.authorized_ok(web::Json(()))
117}
118
119pub fn _add_routes(cfg: &mut web::ServiceConfig) {
120    cfg.route("/{chatbot_configuration_id}", web::get().to(get_chatbot))
121        .route("/{chatbot_configuration_id}", web::post().to(edit_chatbot))
122        .route(
123            "/{chatbot_configuration_id}",
124            web::delete().to(delete_chatbot),
125        );
126}