1
0
Fork 0
forked from 2sys/shoutdotdev
shoutdotdev/src/api_keys.rs

36 lines
937 B
Rust
Raw Normal View History

2025-02-26 13:10:48 -08:00
use deadpool_diesel::postgres::Connection;
2025-02-26 13:10:50 -08:00
use diesel::prelude::*;
use uuid::Uuid;
2025-02-26 13:10:48 -08:00
use crate::{app_error::AppError, schema::api_keys::dsl::*, teams::Team};
2025-02-26 13:10:50 -08:00
2025-02-26 13:10:48 -08:00
#[derive(Associations, Clone, Debug, Identifiable, Insertable, Queryable, Selectable)]
#[diesel(table_name = crate::schema::api_keys)]
2025-02-26 13:10:50 -08:00
#[diesel(belongs_to(Team))]
pub struct ApiKey {
pub id: Uuid,
pub team_id: Uuid,
}
impl ApiKey {
2025-02-26 13:10:48 -08:00
pub async fn generate_for_team(
db_conn: &Connection,
key_team_id: Uuid,
) -> Result<Self, AppError> {
let api_key = Self {
id: Uuid::new_v4(),
team_id: key_team_id,
};
let api_key_copy = api_key.clone();
db_conn
2025-02-26 13:10:50 -08:00
.interact(move |conn| {
2025-02-26 13:10:48 -08:00
diesel::insert_into(api_keys)
.values(api_key_copy)
.execute(conn)
2025-02-26 13:10:50 -08:00
})
.await
.unwrap()?;
Ok(api_key)
}
}