headless_lms_models/
chatbot_configurations_models.rs

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