backend signup
This commit is contained in:
+71
-2
@@ -1,7 +1,16 @@
|
||||
use chrono::NaiveDateTime;
|
||||
use diesel::deserialize::FromSql;
|
||||
use diesel::pg::{Pg, PgValue};
|
||||
use diesel::prelude::*;
|
||||
use diesel::serialize::{IsNull, Output, ToSql};
|
||||
use diesel::{deserialize, serialize, AsExpression, FromSqlRow};
|
||||
use serde_json::Value;
|
||||
use std::fmt;
|
||||
use std::io::Write;
|
||||
use std::str;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::schema::sql_types::TenantStatus as TenantStatusSql;
|
||||
use crate::schema::*;
|
||||
|
||||
#[derive(Debug, Clone, Queryable, Identifiable, Associations)]
|
||||
@@ -26,6 +35,65 @@ pub struct NewUserMembership {
|
||||
pub role: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, AsExpression, FromSqlRow)]
|
||||
#[diesel(sql_type = TenantStatusSql)]
|
||||
pub enum TenantStatus {
|
||||
Creating,
|
||||
Active,
|
||||
Suspended,
|
||||
Deleting,
|
||||
Error,
|
||||
}
|
||||
|
||||
impl TenantStatus {
|
||||
pub fn as_str(&self) -> &'static str {
|
||||
match self {
|
||||
TenantStatus::Creating => "creating",
|
||||
TenantStatus::Active => "active",
|
||||
TenantStatus::Suspended => "suspended",
|
||||
TenantStatus::Deleting => "deleting",
|
||||
TenantStatus::Error => "error",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_str(value: &str) -> Option<Self> {
|
||||
match value {
|
||||
"creating" => Some(TenantStatus::Creating),
|
||||
"active" => Some(TenantStatus::Active),
|
||||
"suspended" => Some(TenantStatus::Suspended),
|
||||
"deleting" => Some(TenantStatus::Deleting),
|
||||
"error" => Some(TenantStatus::Error),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for TenantStatus {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{}", self.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
impl ToSql<TenantStatusSql, Pg> for TenantStatus {
|
||||
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result {
|
||||
out.write_all(self.as_str().as_bytes())?;
|
||||
Ok(IsNull::No)
|
||||
}
|
||||
}
|
||||
|
||||
impl FromSql<TenantStatusSql, Pg> for TenantStatus {
|
||||
fn from_sql(bytes: PgValue<'_>) -> deserialize::Result<Self> {
|
||||
let value = str::from_utf8(bytes.as_bytes())
|
||||
.map_err(|err| Box::<dyn std::error::Error + Send + Sync>::from(err))?;
|
||||
TenantStatus::from_str(value).ok_or_else(|| {
|
||||
Box::<dyn std::error::Error + Send + Sync>::from(std::io::Error::new(
|
||||
std::io::ErrorKind::InvalidData,
|
||||
format!("invalid tenant status '{value}'"),
|
||||
))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Queryable, Identifiable)]
|
||||
#[diesel(table_name = tenants)]
|
||||
#[diesel(primary_key(id))]
|
||||
@@ -34,10 +102,11 @@ pub struct Tenant {
|
||||
pub slug: String,
|
||||
pub storage_root: Option<String>,
|
||||
pub quickwit_index: Option<String>,
|
||||
pub status: String,
|
||||
pub config: serde_json::Value,
|
||||
pub config: Value,
|
||||
pub created_at: NaiveDateTime,
|
||||
pub updated_at: NaiveDateTime,
|
||||
pub status: TenantStatus,
|
||||
pub created_by: Option<Uuid>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Queryable, Identifiable)]
|
||||
|
||||
Reference in New Issue
Block a user