rename all sql table struct with Table signature

This commit is contained in:
appflowy 2021-07-19 17:37:58 +08:00
parent 7e9a68f9fd
commit 139222ddcb
9 changed files with 45 additions and 45 deletions

View File

@ -2,7 +2,7 @@ use flowy_user::{
entities::{SignInParams, SignUpParams, UserDetail},
errors::{ErrorBuilder, UserError, UserErrorCode},
prelude::UserServer,
sql_tables::User,
sql_tables::UserTable,
};
pub type ArcFlowyServer = std::sync::Arc<dyn FlowyServer>;
@ -14,9 +14,9 @@ pub struct FlowyServerMocker {}
impl FlowyServer for FlowyServerMocker {}
impl UserServer for FlowyServerMocker {
fn sign_up(&self, params: SignUpParams) -> Result<User, UserError> {
fn sign_up(&self, params: SignUpParams) -> Result<UserTable, UserError> {
let user_id = params.email.clone();
Ok(User::new(
Ok(UserTable::new(
user_id,
params.name,
params.email,
@ -24,9 +24,9 @@ impl UserServer for FlowyServerMocker {
))
}
fn sign_in(&self, params: SignInParams) -> Result<User, UserError> {
fn sign_in(&self, params: SignInParams) -> Result<UserTable, UserError> {
let user_id = params.email.clone();
Ok(User::new(
Ok(UserTable::new(
user_id,
"".to_owned(),
params.email,

View File

@ -29,9 +29,9 @@ pub struct UserDetail {
pub workspace: String,
}
use crate::sql_tables::User;
impl std::convert::From<User> for UserDetail {
fn from(user: User) -> Self {
use crate::sql_tables::UserTable;
impl std::convert::From<UserTable> for UserDetail {
fn from(user: UserTable) -> Self {
UserDetail {
id: user.id,
email: user.email,

View File

@ -1,12 +1,12 @@
use crate::{
entities::{SignInParams, SignUpParams, UserDetail},
errors::UserError,
sql_tables::User,
sql_tables::UserTable,
};
pub trait UserServer {
fn sign_up(&self, params: SignUpParams) -> Result<User, UserError>;
fn sign_in(&self, params: SignInParams) -> Result<User, UserError>;
fn sign_up(&self, params: SignUpParams) -> Result<UserTable, UserError>;
fn sign_in(&self, params: SignInParams) -> Result<UserTable, UserError>;
fn get_user_info(&self, user_id: &str) -> Result<UserDetail, UserError>;
fn sign_out(&self, user_id: &str) -> Result<(), UserError>;
}

View File

@ -14,7 +14,7 @@ use crate::{
errors::{ErrorBuilder, UserError, UserErrorCode},
event::UserEvent::*,
services::user_session::{database::UserDB, user_server::UserServer},
sql_tables::{User, UserChangeset},
sql_tables::{UserTable, UserTableChangeset},
};
use flowy_dispatch::prelude::{EventDispatch, ModuleRequest, ToBytes};
@ -56,14 +56,14 @@ impl UserSession {
self.database.get_connection(&user_id)
}
pub fn sign_in(&self, params: SignInParams) -> Result<User, UserError> {
pub fn sign_in(&self, params: SignInParams) -> Result<UserTable, UserError> {
let user = self.server.sign_in(params)?;
let _ = self.set_user_id(Some(user.id.clone()))?;
self.save_user(user)
}
pub fn sign_up(&self, params: SignUpParams) -> Result<User, UserError> {
pub fn sign_up(&self, params: SignUpParams) -> Result<UserTable, UserError> {
let user = self.server.sign_up(params)?;
let _ = self.set_user_id(Some(user.id.clone()))?;
self.save_user(user)
@ -84,7 +84,7 @@ impl UserSession {
Ok(())
}
fn save_user(&self, user: User) -> Result<User, UserError> {
fn save_user(&self, user: UserTable) -> Result<UserTable, UserError> {
let conn = self.get_db_connection()?;
let _ = diesel::insert_into(user_table::table)
.values(user.clone())
@ -94,7 +94,7 @@ impl UserSession {
}
pub fn update_user(&self, params: UpdateUserParams) -> Result<UserDetail, UserError> {
let changeset = UserChangeset::new(params);
let changeset = UserTableChangeset::new(params);
let conn = self.get_db_connection()?;
diesel_update_table!(user_table, changeset, conn);
@ -106,7 +106,7 @@ impl UserSession {
let user_id = self.get_user_id()?;
let user = dsl::user_table
.filter(user_table::id.eq(&user_id))
.first::<User>(&*(self.get_db_connection()?))?;
.first::<UserTable>(&*(self.get_db_connection()?))?;
match self.server.get_user_info(&user_id) {
Ok(_user_detail) => {

View File

@ -3,7 +3,7 @@ use flowy_database::schema::user_table;
#[derive(Clone, Default, Queryable, Identifiable, Insertable)]
#[table_name = "user_table"]
pub struct User {
pub struct UserTable {
pub(crate) id: String,
pub(crate) name: String,
pub(crate) password: String,
@ -11,7 +11,7 @@ pub struct User {
pub(crate) workspace: String,
}
impl User {
impl UserTable {
pub fn new(id: String, name: String, email: String, password: String) -> Self {
Self {
id,
@ -30,7 +30,7 @@ impl User {
#[derive(AsChangeset, Identifiable, Default, Debug)]
#[table_name = "user_table"]
pub struct UserChangeset {
pub struct UserTableChangeset {
pub id: String,
pub workspace: Option<String>,
pub name: Option<String>,
@ -38,9 +38,9 @@ pub struct UserChangeset {
pub password: Option<String>,
}
impl UserChangeset {
impl UserTableChangeset {
pub fn new(params: UpdateUserParams) -> Self {
UserChangeset {
UserTableChangeset {
id: params.id,
workspace: params.workspace,
name: params.name,

View File

@ -15,7 +15,7 @@ impl AppController {
pub fn new(user: Arc<dyn WorkspaceUser>) -> Self { Self { user } }
pub fn save_app(&self, params: CreateAppParams) -> Result<AppDetail, WorkspaceError> {
let app = App::new(params);
let app = AppTable::new(params);
let conn = self.user.db_connection()?;
let detail: AppDetail = app.clone().into();
@ -26,7 +26,7 @@ impl AppController {
}
pub fn update_app(&self, params: UpdateAppParams) -> Result<(), WorkspaceError> {
let changeset = AppChangeset::new(params);
let changeset = AppTableChangeset::new(params);
let conn = self.user.db_connection()?;
diesel_update_table!(app_table, changeset, conn);
Ok(())

View File

@ -16,7 +16,7 @@ impl WorkspaceController {
&self,
params: CreateWorkspaceParams,
) -> Result<WorkspaceDetail, WorkspaceError> {
let workspace = Workspace::new(params);
let workspace = WorkspaceTable::new(params);
let detail: WorkspaceDetail = workspace.clone().into();
let _ = diesel::insert_into(workspace_table::table)
@ -31,14 +31,14 @@ impl WorkspaceController {
pub fn get_workspace(
&self,
workspace_id: &str,
) -> DispatchFuture<Result<Workspace, WorkspaceError>> {
) -> DispatchFuture<Result<WorkspaceTable, WorkspaceError>> {
let user = self.user.clone();
let workspace_id = workspace_id.to_owned();
DispatchFuture {
fut: Box::pin(async move {
let workspace = dsl::workspace_table
.filter(workspace_table::id.eq(&workspace_id))
.first::<Workspace>(&*(user.db_connection()?))?;
.first::<WorkspaceTable>(&*(user.db_connection()?))?;
// TODO: fetch workspace from remote server
Ok(workspace)
@ -47,7 +47,7 @@ impl WorkspaceController {
}
pub fn update_workspace(&self, params: UpdateWorkspaceParams) -> Result<(), WorkspaceError> {
let changeset = WorkspaceChangeset::new(params);
let changeset = WorkspaceTableChangeset::new(params);
let conn = self.user.db_connection()?;
diesel_update_table!(workspace_table, changeset, conn);

View File

@ -1,7 +1,7 @@
use crate::{
entities::app::{AppDetail, ColorStyle, CreateAppParams, UpdateAppParams},
impl_sql_binary_expression,
sql_tables::workspace::Workspace,
sql_tables::workspace::WorkspaceTable,
};
use diesel::sql_types::Binary;
use flowy_database::schema::app_table;
@ -20,10 +20,10 @@ use std::convert::TryInto;
Insertable,
Associations,
)]
#[belongs_to(Workspace, foreign_key = "workspace_id")]
#[belongs_to(WorkspaceTable, foreign_key = "workspace_id")]
#[table_name = "app_table"]
#[serde(tag = "type")]
pub(crate) struct App {
pub(crate) struct AppTable {
pub id: String,
pub workspace_id: String, // equal to #[belongs_to(Workspace, foreign_key = "workspace_id")].
pub name: String,
@ -35,7 +35,7 @@ pub(crate) struct App {
pub version: i64,
}
impl App {
impl AppTable {
pub fn new(params: CreateAppParams) -> Self {
let app_id = uuid();
let time = timestamp();
@ -87,16 +87,16 @@ impl_sql_binary_expression!(ColorStyleCol);
#[derive(AsChangeset, Identifiable, Default, Debug)]
#[table_name = "app_table"]
pub struct AppChangeset {
pub struct AppTableChangeset {
pub id: String,
pub workspace_id: Option<String>,
pub name: Option<String>,
pub desc: Option<String>,
}
impl AppChangeset {
impl AppTableChangeset {
pub fn new(params: UpdateAppParams) -> Self {
AppChangeset {
AppTableChangeset {
id: params.app_id,
workspace_id: params.workspace_id,
name: params.name,
@ -105,7 +105,7 @@ impl AppChangeset {
}
}
impl std::convert::Into<AppDetail> for App {
impl std::convert::Into<AppDetail> for AppTable {
fn into(self) -> AppDetail {
AppDetail {
id: self.id,

View File

@ -6,7 +6,7 @@ use serde::{Deserialize, Serialize};
#[derive(PartialEq, Clone, Serialize, Deserialize, Debug, Queryable, Identifiable, Insertable)]
#[table_name = "workspace_table"]
#[serde(tag = "type")]
pub struct Workspace {
pub struct WorkspaceTable {
pub id: String,
pub name: String,
pub desc: String,
@ -16,20 +16,20 @@ pub struct Workspace {
pub version: i64,
}
impl Workspace {
impl WorkspaceTable {
#[allow(dead_code)]
pub fn new(params: CreateWorkspaceParams) -> Self {
let mut workspace = Workspace::default();
let mut workspace = WorkspaceTable::default();
workspace.name = params.name;
workspace.desc = params.desc;
workspace
}
}
impl std::default::Default for Workspace {
impl std::default::Default for WorkspaceTable {
fn default() -> Self {
let time = timestamp();
Workspace {
WorkspaceTable {
id: uuid(),
name: String::default(),
desc: String::default(),
@ -43,15 +43,15 @@ impl std::default::Default for Workspace {
#[derive(AsChangeset, Identifiable, Clone, Default, Debug)]
#[table_name = "workspace_table"]
pub struct WorkspaceChangeset {
pub struct WorkspaceTableChangeset {
pub id: String,
pub name: Option<String>,
pub desc: Option<String>,
}
impl WorkspaceChangeset {
impl WorkspaceTableChangeset {
pub fn new(params: UpdateWorkspaceParams) -> Self {
WorkspaceChangeset {
WorkspaceTableChangeset {
id: params.id,
name: params.name,
desc: params.desc,
@ -59,7 +59,7 @@ impl WorkspaceChangeset {
}
}
impl std::convert::Into<WorkspaceDetail> for Workspace {
impl std::convert::Into<WorkspaceDetail> for WorkspaceTable {
fn into(self) -> WorkspaceDetail {
WorkspaceDetail {
id: self.id,