use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use sqlx::FromRow;
#[derive(Debug, Clone, FromRow)]
pub struct User {
pub id: String,
pub username: String,
pub password_hash: String,
pub role: String,
pub display_name: Option<String>,
pub active: bool,
pub failed_login_count: i64,
pub locked_until: Option<DateTime<Utc>>,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
}
#[derive(Debug, Clone, Serialize)]
pub struct UserView {
pub id: String,
pub username: String,
pub role: String,
pub display_name: Option<String>,
pub active: bool,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
}
impl From<User> for UserView {
fn from(u: User) -> Self {
UserView {
id: u.id,
username: u.username,
role: u.role,
display_name: u.display_name,
active: u.active,
created_at: u.created_at,
updated_at: u.updated_at,
}
}
}
#[derive(Debug, Deserialize)]
pub struct UserCreate {
pub username: String,
pub password: String,
pub role: Option<String>,
pub display_name: Option<String>,
pub active: Option<bool>,
}
#[derive(Debug, Deserialize, Default)]
pub struct UserUpdate {
pub password: Option<String>,
pub role: Option<String>,
pub display_name: Option<String>,
pub active: Option<bool>,
}
#[derive(Debug, Deserialize)]
pub struct LoginRequest {
pub username: String,
pub password: String,
}
#[derive(Debug, Clone, FromRow)]
pub struct ApiKey {
pub id: String,
pub name: String,
pub key_hash: String,
pub key_prefix: String,
pub role: String,
pub active: bool,
pub last_used_at: Option<DateTime<Utc>>,
pub created_at: DateTime<Utc>,
}
#[derive(Debug, Clone, Serialize)]
pub struct ApiKeyView {
pub id: String,
pub name: String,
pub key_prefix: String,
pub role: String,
pub active: bool,
pub last_used_at: Option<DateTime<Utc>>,
pub created_at: DateTime<Utc>,
}
impl From<ApiKey> for ApiKeyView {
fn from(k: ApiKey) -> Self {
ApiKeyView {
id: k.id,
name: k.name,
key_prefix: k.key_prefix,
role: k.role,
active: k.active,
last_used_at: k.last_used_at,
created_at: k.created_at,
}
}
}
#[derive(Debug, Deserialize)]
pub struct ApiKeyCreate {
pub name: String,
pub role: Option<String>,
}