user_update.rs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. use crate::{
  2. entities::parser::*,
  3. errors::{ErrorBuilder, ErrorCode, UserError},
  4. };
  5. use flowy_derive::ProtoBuf;
  6. use std::convert::TryInto;
  7. #[derive(ProtoBuf, Default)]
  8. pub struct UpdateUserRequest {
  9. #[pb(index = 1)]
  10. pub id: String,
  11. #[pb(index = 2, one_of)]
  12. pub name: Option<String>,
  13. #[pb(index = 3, one_of)]
  14. pub email: Option<String>,
  15. #[pb(index = 4, one_of)]
  16. pub password: Option<String>,
  17. }
  18. impl UpdateUserRequest {
  19. pub fn new(id: &str) -> Self {
  20. Self {
  21. id: id.to_owned(),
  22. ..Default::default()
  23. }
  24. }
  25. pub fn name(mut self, name: &str) -> Self {
  26. self.name = Some(name.to_owned());
  27. self
  28. }
  29. pub fn email(mut self, email: &str) -> Self {
  30. self.email = Some(email.to_owned());
  31. self
  32. }
  33. pub fn password(mut self, password: &str) -> Self {
  34. self.password = Some(password.to_owned());
  35. self
  36. }
  37. }
  38. #[derive(ProtoBuf, Default)]
  39. pub struct UpdateUserParams {
  40. #[pb(index = 1)]
  41. pub id: String,
  42. #[pb(index = 2, one_of)]
  43. pub name: Option<String>,
  44. #[pb(index = 3, one_of)]
  45. pub email: Option<String>,
  46. #[pb(index = 4, one_of)]
  47. pub password: Option<String>,
  48. }
  49. impl TryInto<UpdateUserParams> for UpdateUserRequest {
  50. type Error = UserError;
  51. fn try_into(self) -> Result<UpdateUserParams, Self::Error> {
  52. let id = UserId::parse(self.id)
  53. .map_err(|e| ErrorBuilder::new(ErrorCode::UserIdInvalid).msg(e).build())?
  54. .0;
  55. let name = match self.name {
  56. None => None,
  57. Some(name) => Some(
  58. UserName::parse(name)
  59. .map_err(|e| ErrorBuilder::new(e).build())?
  60. .0,
  61. ),
  62. };
  63. let email = match self.email {
  64. None => None,
  65. Some(email) => Some(
  66. UserEmail::parse(email)
  67. .map_err(|e| ErrorBuilder::new(e).build())?
  68. .0,
  69. ),
  70. };
  71. let password = match self.password {
  72. None => None,
  73. Some(password) => Some(
  74. UserPassword::parse(password)
  75. .map_err(|e| ErrorBuilder::new(e).build())?
  76. .0,
  77. ),
  78. };
  79. Ok(UpdateUserParams {
  80. id,
  81. name,
  82. email,
  83. password,
  84. })
  85. }
  86. }