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

36 lines
1,015 B
Rust
Raw Normal View History

2024-11-01 00:07:33 -07:00
use anyhow::Result;
use deadpool_diesel::postgres::Pool;
use diesel::prelude::*;
use uuid::Uuid;
use crate::{app_error::AppError, models::Team, schema};
#[derive(Associations, Clone, Debug, Identifiable, Queryable, Selectable)]
#[diesel(table_name = schema::api_keys)]
#[diesel(belongs_to(Team))]
pub struct ApiKey {
pub id: Uuid,
pub team_id: Uuid,
}
impl ApiKey {
pub async fn generate_for_team(db_pool: &Pool, team_id: Uuid) -> Result<Self, AppError> {
let id = Uuid::new_v4();
let api_key = db_pool
.get()
.await?
.interact(move |conn| {
diesel::insert_into(schema::api_keys::table)
.values((
schema::api_keys::id.eq(id),
schema::api_keys::team_id.eq(team_id),
))
.returning(ApiKey::as_select())
.get_result(conn)
})
.await
.unwrap()?;
Ok(api_key)
}
}