You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.4 KiB
Rust

use chrono::prelude::*;
use serde::{Deserialize, Serialize};
use sqlx::FromRow;
use crate::core::*;
use crate::util::crypto::PubKey;
use crate::util::validate::{ResultBuilder, Validate};
pub type AccountId = Id<Account>;
#[derive(Clone, Deserialize, Serialize, FromRow)]
pub struct Account {
pub id: AccountId,
pub iri: Option<String>,
pub name: String,
pub domain: String,
pub display_name: Option<String>,
pub public_key: Option<PubKey>,
pub created_at: NaiveDateTime,
pub updated_at: NaiveDateTime,
}
pub struct NewAccount {
pub iri: Option<String>,
pub name: String,
pub domain: String,
pub display_name: Option<String>,
pub public_key: Option<PubKey>,
}
impl From<Account> for AccountId {
fn from(account: Account) -> AccountId {
account.id
}
}
impl Validate for NewAccount {
fn validate(&self, builder: ResultBuilder) -> ResultBuilder {
builder
.field(&self.name, "name", |f| {
f.check("Must not be empty", |v| !v.is_empty())
.check("Must be at most 16 characters long", |v| v.len() <= 16)
})
.field(&self.display_name, "display_name", |f| {
f.check("Must be at most 64 characters long", |v| {
v.as_ref().map(|v| v.len()).unwrap_or(0) <= 64
})
})
}
}