headless_lms_server/controllers/main_frontend/
chatbots.rs1use 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, get_all_chatbots))]
9pub(crate) struct MainFrontendChatbotsApiDoc;
10
11#[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
34 let token = if let Some(course_id) = configuration.course_id {
35 authorize(&mut conn, Act::Edit, Some(user.id), Res::Course(course_id)).await?
36 } else {
37 authorize(&mut conn, Act::Edit, Some(user.id), Res::GlobalPermissions).await?
38 };
39
40 token.authorized_ok(web::Json(configuration))
41}
42
43#[utoipa::path(
45 post,
46 path = "/{chatbot_configuration_id}",
47 operation_id = "configureChatbot",
48 tag = "chatbots",
49 params(
50 ("chatbot_configuration_id" = Uuid, Path, description = "Chatbot configuration id")
51 ),
52 request_body = NewChatbotConf,
53 responses(
54 (status = 200, description = "Updated chatbot configuration", body = ChatbotConfiguration)
55 )
56)]
57#[instrument(skip(pool, payload))]
58async fn edit_chatbot(
59 chatbot_configuration_id: web::Path<Uuid>,
60 payload: web::Json<NewChatbotConf>,
61 pool: web::Data<PgPool>,
62 user: AuthUser,
63) -> ControllerResult<web::Json<ChatbotConfiguration>> {
64 let mut conn = pool.acquire().await?;
65 let chatbot =
66 models::chatbot_configurations::get_by_id(&mut conn, *chatbot_configuration_id).await?;
67 let token = if let Some(course_id) = chatbot.course_id {
68 authorize(&mut conn, Act::Edit, Some(user.id), Res::Course(course_id)).await?
69 } else {
70 authorize(&mut conn, Act::Edit, Some(user.id), Res::GlobalPermissions).await?
71 };
72
73 let configuration: ChatbotConfiguration = models::chatbot_configurations::edit(
74 &mut conn,
75 payload.into_inner(),
76 *chatbot_configuration_id,
77 )
78 .await?;
79 token.authorized_ok(web::Json(configuration))
80}
81
82#[utoipa::path(
84 delete,
85 path = "/{chatbot_configuration_id}",
86 operation_id = "deleteChatbotConfiguration",
87 tag = "chatbots",
88 params(
89 ("chatbot_configuration_id" = Uuid, Path, description = "Chatbot configuration id")
90 ),
91 responses(
92 (status = 200, description = "Deleted chatbot configuration")
93 )
94)]
95#[instrument(skip(pool))]
96async fn delete_chatbot(
97 chatbot_configuration_id: web::Path<Uuid>,
98 pool: web::Data<PgPool>,
99 user: AuthUser,
100) -> ControllerResult<web::Json<()>> {
101 let mut conn = pool.acquire().await?;
102 let chatbot =
103 models::chatbot_configurations::get_by_id(&mut conn, *chatbot_configuration_id).await?;
104 let token = if let Some(course_id) = chatbot.course_id {
105 authorize(&mut conn, Act::Edit, Some(user.id), Res::Course(course_id)).await?
106 } else {
107 authorize(&mut conn, Act::Edit, Some(user.id), Res::GlobalPermissions).await?
108 };
109 models::chatbot_configurations::delete(&mut conn, *chatbot_configuration_id).await?;
110
111 token.authorized_ok(web::Json(()))
112}
113
114#[utoipa::path(
116 get,
117 path = "/",
118 operation_id = "getAllChatbots",
119 tag = "chatbots",
120 responses(
121 (status = 200, description = "All chatbots", body = Vec<ChatbotConfiguration>)
122 )
123)]
124#[instrument(skip(pool))]
125async fn get_all_chatbots(
126 pool: web::Data<PgPool>,
127 user: AuthUser,
128) -> ControllerResult<web::Json<Vec<ChatbotConfiguration>>> {
129 let mut conn = pool.acquire().await?;
130 let all_chatbots = models::chatbot_configurations::get_all_chatbots(&mut conn).await?;
131 let token = authorize(&mut conn, Act::View, Some(user.id), Res::GlobalPermissions).await?;
132 token.authorized_ok(web::Json(all_chatbots))
133}
134
135pub fn _add_routes(cfg: &mut web::ServiceConfig) {
136 cfg.route("/{chatbot_configuration_id}", web::get().to(get_chatbot))
137 .route("/{chatbot_configuration_id}", web::post().to(edit_chatbot))
138 .route(
139 "/{chatbot_configuration_id}",
140 web::delete().to(delete_chatbot),
141 )
142 .route("/", web::get().to(get_all_chatbots));
143}