user_profile.rs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. use flowy_derive::{ProtoBuf, ProtoBuf_Enum};
  2. #[derive(Default, ProtoBuf)]
  3. pub struct UserToken {
  4. #[pb(index = 1)]
  5. pub token: String,
  6. }
  7. #[derive(Debug, ProtoBuf_Enum)]
  8. pub enum UserStatus {
  9. Unknown = 0,
  10. Login = 1,
  11. Expired = 2,
  12. }
  13. impl std::default::Default for UserStatus {
  14. fn default() -> Self { UserStatus::Unknown }
  15. }
  16. #[derive(ProtoBuf, Default, Debug, PartialEq, Eq, Clone)]
  17. pub struct UserProfile {
  18. #[pb(index = 1)]
  19. pub id: String,
  20. #[pb(index = 2)]
  21. pub email: String,
  22. #[pb(index = 3)]
  23. pub name: String,
  24. #[pb(index = 4)]
  25. pub token: String,
  26. }
  27. use crate::{
  28. entities::parser::{UserEmail, UserId, UserName, UserPassword},
  29. errors::UserError,
  30. sql_tables::UserTable,
  31. };
  32. use std::convert::TryInto;
  33. impl std::convert::From<UserTable> for UserProfile {
  34. fn from(user: UserTable) -> Self {
  35. UserProfile {
  36. id: user.id,
  37. email: user.email,
  38. name: user.name,
  39. token: user.token,
  40. }
  41. }
  42. }
  43. #[derive(ProtoBuf, Default)]
  44. pub struct UpdateUserRequest {
  45. #[pb(index = 1)]
  46. pub id: String,
  47. #[pb(index = 2, one_of)]
  48. pub name: Option<String>,
  49. #[pb(index = 3, one_of)]
  50. pub email: Option<String>,
  51. #[pb(index = 4, one_of)]
  52. pub password: Option<String>,
  53. }
  54. impl UpdateUserRequest {
  55. pub fn new(id: &str) -> Self {
  56. Self {
  57. id: id.to_owned(),
  58. ..Default::default()
  59. }
  60. }
  61. pub fn name(mut self, name: &str) -> Self {
  62. self.name = Some(name.to_owned());
  63. self
  64. }
  65. pub fn email(mut self, email: &str) -> Self {
  66. self.email = Some(email.to_owned());
  67. self
  68. }
  69. pub fn password(mut self, password: &str) -> Self {
  70. self.password = Some(password.to_owned());
  71. self
  72. }
  73. }
  74. #[derive(ProtoBuf, Default, Clone, Debug)]
  75. pub struct UpdateUserParams {
  76. // TODO: remove user id
  77. #[pb(index = 1)]
  78. pub id: String,
  79. #[pb(index = 2, one_of)]
  80. pub name: Option<String>,
  81. #[pb(index = 3, one_of)]
  82. pub email: Option<String>,
  83. #[pb(index = 4, one_of)]
  84. pub password: Option<String>,
  85. }
  86. impl UpdateUserParams {
  87. pub fn new(user_id: &str) -> Self {
  88. Self {
  89. id: user_id.to_owned(),
  90. ..Default::default()
  91. }
  92. }
  93. pub fn name(mut self, name: &str) -> Self {
  94. self.name = Some(name.to_owned());
  95. self
  96. }
  97. pub fn email(mut self, email: &str) -> Self {
  98. self.email = Some(email.to_owned());
  99. self
  100. }
  101. pub fn password(mut self, password: &str) -> Self {
  102. self.password = Some(password.to_owned());
  103. self
  104. }
  105. }
  106. impl TryInto<UpdateUserParams> for UpdateUserRequest {
  107. type Error = UserError;
  108. fn try_into(self) -> Result<UpdateUserParams, Self::Error> {
  109. let id = UserId::parse(self.id).map_err(|e| UserError::user_id().context(e))?.0;
  110. let name = match self.name {
  111. None => None,
  112. Some(name) => Some(UserName::parse(name).map_err(|e| UserError::code(e))?.0),
  113. };
  114. let email = match self.email {
  115. None => None,
  116. Some(email) => Some(UserEmail::parse(email).map_err(|e| UserError::code(e))?.0),
  117. };
  118. let password = match self.password {
  119. None => None,
  120. Some(password) => Some(UserPassword::parse(password).map_err(|e| UserError::code(e))?.0),
  121. };
  122. Ok(UpdateUserParams { id, name, email, password })
  123. }
  124. }