Skip to content

Commit

Permalink
add new db functions
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszKielar committed Sep 15, 2024
1 parent 333bec1 commit e804250
Showing 1 changed file with 45 additions and 5 deletions.
50 changes: 45 additions & 5 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,57 @@ where
Ok(items)
}

pub async fn create_conversation<'e, E>(executor: E, name: String) -> AppResult<Conversation>
where
E: Executor<'e, Database = Sqlite>,
{
let conversation = sqlx::query_as(
r#"
INSERT INTO conversations(name) VALUES (?1)
RETURNING *
"#,
)
.bind(name)
.persistent(false)
.fetch_one(executor)
.await?;

Ok(conversation)
}

pub async fn delete_conversation<'e, E>(
executor: E,
conversation_id: u32,
) -> AppResult<Conversation>
where
E: Executor<'e, Database = Sqlite>,
{
let conversation = sqlx::query_as(
r#"
DELETE FROM conversations
WHERE id = ?1
RETURNING *
"#,
)
.bind(conversation_id)
.persistent(false)
.fetch_one(executor)
.await?;

Ok(conversation)
}

pub async fn get_messages<'e, E>(executor: E, conversation_id: u32) -> AppResult<Vec<Message>>
where
E: Executor<'e, Database = Sqlite>,
{
let items = sqlx::query_as(
r#"
SELECT *
FROM messages
WHERE conversation_id = ?1
ORDER BY created_at ASC
"#,
SELECT *
FROM messages
WHERE conversation_id = ?1
ORDER BY created_at ASC
"#,
)
.bind(conversation_id)
.persistent(false)
Expand Down

0 comments on commit e804250

Please sign in to comment.