/ src / actor / account.rs
account.rs
 1  use crate::consts::IPFS_GATEWAY;
 2  use candid::{CandidType, Principal};
 3  use serde::{Deserialize, Serialize};
 4  
 5  #[derive(CandidType, Clone, Debug, Serialize, Deserialize, PartialEq)]
 6  pub enum AccountPageResponse {
 7      User(UserAccountPageResponse),
 8  }
 9  
10  #[derive(CandidType, Clone, Debug, Serialize, Deserialize, PartialEq)]
11  pub struct UserAccountPageResponse {
12      pub name: String,
13      pub birth_name: String,
14      pub mail_address: String,
15      pub image: AccountImage,
16  }
17  
18  impl UserAccountPageResponse {
19      pub fn update_with_request(&mut self, request: &SetUserProfileRequest) {
20          self.name.clone_from(&request.name);
21          self.birth_name.clone_from(&request.birth_name);
22          self.mail_address.clone_from(&request.mail_address);
23          self.image.clone_from(&request.image);
24      }
25  }
26  
27  #[derive(thiserror::Error, Debug, CandidType, Clone, Serialize, Deserialize, PartialEq)]
28  pub enum AccountPageError {
29      #[error("account (key: {0}) not found (AccontPageError::AccountNotFound)")]
30      AccountNotFound(Principal),
31  }
32  
33  #[derive(CandidType, Clone, Debug, Serialize, Deserialize, PartialEq)]
34  pub struct SetUserProfileRequest {
35      pub name: String,
36      pub birth_name: String,
37      pub mail_address: String,
38      pub image: AccountImage,
39  }
40  
41  impl SetUserProfileRequest {
42      pub fn equal_to_response(&self, response: UserAccountPageResponse) -> bool {
43          self.name == response.name
44              && self.birth_name == response.birth_name
45              && self.mail_address == response.mail_address
46              && self.image == response.image
47      }
48  }
49  
50  impl From<UserAccountPageResponse> for SetUserProfileRequest {
51      fn from(response: UserAccountPageResponse) -> Self {
52          Self {
53              name: response.name,
54              birth_name: response.birth_name,
55              mail_address: response.mail_address,
56              image: response.image,
57          }
58      }
59  }
60  
61  #[derive(thiserror::Error, Debug, CandidType, Clone, Serialize, Deserialize, PartialEq)]
62  pub enum SetUserProfileError {
63      #[error("account (key: {0}) not found (AccontPageError::AccountNotFound)")]
64      AccountNotFound(Principal),
65      #[error("account (key: {0}) is not a user (SetUserProfileError::AccountIsNotUser)")]
66      AccountIsNotUser(Principal),
67  }
68  
69  #[derive(Debug, CandidType, Clone, Serialize, Deserialize, PartialEq)]
70  pub enum AccountImage {
71      None,
72      CID(String),
73      BLOB(Vec<u8>),
74  }
75  
76  impl AccountImage {
77      pub fn to_url(&self) -> String {
78          match self {
79              AccountImage::None => "".to_string(),
80              AccountImage::CID(cid) => format!("{}{}", IPFS_GATEWAY, cid),
81              AccountImage::BLOB(_) => "".to_string(),
82          }
83      }
84  
85      pub fn is_none(&self) -> bool {
86          self == &AccountImage::None
87      }
88  }