feat: add open ai key to database (#1852)

* feat: add open ai key to database

* chore: refactor code
This commit is contained in:
Lucas.Xu
2023-02-14 10:04:36 +08:00
committed by GitHub
parent 7207e35349
commit 72e155f5b9
12 changed files with 125 additions and 15 deletions

View File

@ -0,0 +1 @@
ALTER TABLE user_table DROP COLUMN openai_key;

View File

@ -0,0 +1 @@
ALTER TABLE user_table ADD COLUMN openai_key TEXT NOT NULL DEFAULT '';

View File

@ -146,6 +146,7 @@ diesel::table! {
email -> Text,
workspace -> Text,
icon_url -> Text,
openai_key -> Text,
}
}

View File

@ -2,7 +2,8 @@ use crate::errors::ErrorCode;
use flowy_derive::ProtoBuf;
use std::convert::TryInto;
use user_model::{
UpdateUserProfileParams, UserEmail, UserIcon, UserId, UserName, UserPassword, UserProfile,
UpdateUserProfileParams, UserEmail, UserIcon, UserId, UserName, UserOpenaiKey, UserPassword,
UserProfile,
};
#[derive(Default, ProtoBuf)]
@ -33,6 +34,9 @@ pub struct UserProfilePB {
#[pb(index = 5)]
pub icon_url: String,
#[pb(index = 6)]
pub openai_key: String,
}
impl std::convert::From<UserProfile> for UserProfilePB {
@ -43,6 +47,7 @@ impl std::convert::From<UserProfile> for UserProfilePB {
name: user_profile.name,
token: user_profile.token,
icon_url: user_profile.icon_url,
openai_key: user_profile.openai_key,
}
}
}
@ -63,6 +68,9 @@ pub struct UpdateUserProfilePayloadPB {
#[pb(index = 5, one_of)]
pub icon_url: Option<String>,
#[pb(index = 6, one_of)]
pub openai_key: Option<String>,
}
impl UpdateUserProfilePayloadPB {
@ -92,6 +100,11 @@ impl UpdateUserProfilePayloadPB {
self.icon_url = Some(icon_url.to_owned());
self
}
pub fn openai_key(mut self, openai_key: &str) -> Self {
self.openai_key = Some(openai_key.to_owned());
self
}
}
impl TryInto<UpdateUserProfileParams> for UpdateUserProfilePayloadPB {
@ -120,12 +133,18 @@ impl TryInto<UpdateUserProfileParams> for UpdateUserProfilePayloadPB {
Some(icon_url) => Some(UserIcon::parse(icon_url)?.0),
};
let openai_key = match self.openai_key {
None => None,
Some(openai_key) => Some(UserOpenaiKey::parse(openai_key)?.0),
};
Ok(UpdateUserProfileParams {
id,
name,
email,
password,
icon_url,
openai_key,
})
}
}

View File

@ -84,6 +84,7 @@ pub struct UserTable {
pub(crate) email: String,
pub(crate) workspace: String, // deprecated
pub(crate) icon_url: String,
pub(crate) openai_key: String,
}
impl UserTable {
@ -95,6 +96,7 @@ impl UserTable {
token,
icon_url: "".to_owned(),
workspace: "".to_owned(),
openai_key: "".to_owned(),
}
}
@ -124,6 +126,7 @@ impl std::convert::From<UserTable> for UserProfile {
name: table.name,
token: table.token,
icon_url: table.icon_url,
openai_key: table.openai_key,
}
}
}
@ -136,6 +139,7 @@ pub struct UserTableChangeset {
pub name: Option<String>,
pub email: Option<String>,
pub icon_url: Option<String>,
pub openai_key: Option<String>,
}
impl UserTableChangeset {
@ -146,6 +150,7 @@ impl UserTableChangeset {
name: params.name,
email: params.email,
icon_url: params.icon_url,
openai_key: params.openai_key,
}
}
}