添加用户角色变量

This commit is contained in:
李运家 2024-05-30 15:41:38 +08:00
parent 837a296d21
commit bc437629f1

View File

@ -2,6 +2,54 @@ use chrono::{DateTime, Utc};
use sqlx::{Error, PgPool};
use sqlx::types::{chrono, JsonValue};
#[derive(Debug, PartialEq, Clone)]
pub enum Role {
Admin,
User,
}
impl Default for Role {
fn default() -> Self {
Role::User
}
}
// impl sqlx::Type<sqlx::Postgres> for Role {
// fn type_info() -> sqlx::postgres::PgTypeInfo {
// sqlx::postgres::PgTypeInfo::with_name("CHAR(1)")
// }
// }
// impl<'r> sqlx::Decode<'r, sqlx::Postgres> for Role {
// fn decode(value: sqlx::postgres::PgValueRef<'r>) -> Result<Self, sqlx::error::BoxDynError> {
// match value.as_str().map_err(|_| sqlx::Error::Decode("Invalid Role".into()))? {
// "admin" => Ok(Role::Admin),
// "user" => Ok(Role::User),
// _ => Err(sqlx::Error::Decode("Unknown Role".into()))?,
// }
// }
// }
// impl sqlx::Encode<'_, sqlx::Postgres> for Role {
// fn encode_by_ref(&self, buf: &mut sqlx::postgres::PgArgumentBuffer) -> sqlx::encode::IsNull {
// match self {
// Role::Admin => buf.extend_from_slice(b"admin"),
// Role::User => buf.extend_from_slice(b"user"),
// }
// sqlx::encode::IsNull::No
// }
// }
impl Into<Role> for String {
fn into(self) -> Role {
match self.as_str() {
"admin" => Role::Admin,
"user" => Role::User,
_ => Role::User,
}
}
}
#[derive(Debug, Clone, Default)]
pub struct Account {
pub id: String,
@ -15,6 +63,7 @@ pub struct Account {
pub wallet: JsonValue,
pub email: Option<String>,
pub password: Option<Vec<u8>>,
pub role: Role,
pub facebook_id: Option<String>,
pub google_id: Option<String>,
pub gamecenter_id: Option<String>,