Skip to main content

headless_lms_models/
chatbot_configurations_models.rs

1use crate::prelude::*;
2use utoipa::ToSchema;
3
4#[derive(Clone, PartialEq, Deserialize, Serialize, ToSchema)]
5
6pub struct ChatbotConfigurationModel {
7    pub id: Uuid,
8    pub created_at: DateTime<Utc>,
9    pub updated_at: DateTime<Utc>,
10    pub deleted_at: Option<DateTime<Utc>>,
11    pub model: String,
12    pub thinking: bool,
13    pub default_model: bool,
14    pub deployment_name: String,
15    pub context_size: i32,
16}
17
18#[derive(Clone, PartialEq, Deserialize, Serialize)]
19pub struct NewChatbotConfigurationModel {
20    pub id: Uuid,
21    pub model: String,
22    pub thinking: bool,
23    pub default_model: bool,
24    pub deployment_name: String,
25    pub context_size: i32,
26}
27
28pub async fn get_by_id(
29    conn: &mut PgConnection,
30    id: Uuid,
31) -> ModelResult<ChatbotConfigurationModel> {
32    let res = sqlx::query_as!(
33        ChatbotConfigurationModel,
34        r#"
35SELECT * FROM chatbot_configurations_models
36WHERE id = $1
37AND deleted_at IS NULL
38        "#,
39        id
40    )
41    .fetch_one(conn)
42    .await?;
43    Ok(res)
44}
45
46pub async fn get_all(conn: &mut PgConnection) -> ModelResult<Vec<ChatbotConfigurationModel>> {
47    let res = sqlx::query_as!(
48        ChatbotConfigurationModel,
49        r#"
50SELECT * FROM chatbot_configurations_models
51WHERE deleted_at IS NULL
52        "#,
53    )
54    .fetch_all(conn)
55    .await?;
56    Ok(res)
57}
58
59pub async fn get_default(conn: &mut PgConnection) -> ModelResult<ChatbotConfigurationModel> {
60    let res = sqlx::query_as!(
61        ChatbotConfigurationModel,
62        r#"
63SELECT * FROM chatbot_configurations_models
64WHERE default_model = true
65AND deleted_at IS NULL
66        "#,
67    )
68    .fetch_one(conn)
69    .await?;
70    Ok(res)
71}
72
73pub async fn get_by_chatbot_configuration_id(
74    conn: &mut PgConnection,
75    chatbotconf_id: Uuid,
76) -> ModelResult<ChatbotConfigurationModel> {
77    let res = sqlx::query_as!(
78        ChatbotConfigurationModel,
79        r#"
80SELECT * FROM chatbot_configurations_models
81WHERE id = (
82    SELECT model_id FROM chatbot_configurations WHERE id = $1
83)
84AND deleted_at IS NULL
85        "#,
86        chatbotconf_id,
87    )
88    .fetch_one(conn)
89    .await?;
90    Ok(res)
91}
92
93pub async fn insert(
94    conn: &mut PgConnection,
95    input: NewChatbotConfigurationModel,
96) -> ModelResult<ChatbotConfigurationModel> {
97    let res = sqlx::query_as!(
98        ChatbotConfigurationModel,
99        r#"
100INSERT INTO chatbot_configurations_models (id, model, thinking, deployment_name, default_model, context_size) VALUES ($1, $2, $3, $4, $5, $6) RETURNING *
101        "#,
102        input.id,
103        input.model,
104        input.thinking,
105        input.deployment_name,
106        input.default_model,
107        input.context_size,
108    )
109    .fetch_one(conn)
110    .await?;
111    Ok(res)
112}