@@ -12,6 +12,7 @@ use crate::{
1212pub ( crate ) struct Issuer {
1313 /// ID of this issuer
1414 id : i64 ,
15+ /// The [DomainName] of this issuer.
1516 pub ( crate ) domain_components : DomainName ,
1617}
1718
@@ -23,17 +24,38 @@ impl Issuer {
2324 self . id
2425 }
2526
26- /// Create (insert) the issuer entry for this sonata instance.
27- pub ( crate ) async fn create_own ( db : & Database ) -> Result < Option < Self > , Error > {
28- let config_domain = & SonataConfig :: get_or_panic ( ) . general . server_domain ;
29- let domain_name = DomainName :: new ( config_domain) . map_err ( |e| {
27+ /// Convert a [DomainName] into a `Vec<String>`
28+ fn domain_name_to_vec_string ( domain_name : DomainName ) -> Vec < String > {
29+ domain_name. to_string ( ) . split ( '.' ) . map ( |s| s. to_owned ( ) ) . collect :: < Vec < _ > > ( )
30+ }
31+
32+ /// Convert a `Vec<String>` to a [DomainName]
33+ fn vec_string_to_domain_name ( strs : Vec < String > ) -> Result < DomainName , Box < Error > > {
34+ match DomainName :: new ( & strs. join ( "." ) ) {
35+ Err ( e) => {
36+ error ! ( "Error: Invalid DomainName stored in issuers table: {e}" ) ;
37+ Err ( Error :: new_internal_error ( None ) . into ( ) )
38+ }
39+ Ok ( dn) => Ok ( dn) ,
40+ }
41+ }
42+
43+ /// Convert a `str` to a [DomainName]
44+ fn str_to_domain_name ( string : & str ) -> Result < DomainName , Box < Error > > {
45+ DomainName :: new ( string) . map_err ( |e| {
3046 Error :: new (
3147 crate :: errors:: Errcode :: IllegalInput ,
3248 Some ( Context :: new ( None , None , None , Some ( & e. to_string ( ) ) ) ) ,
3349 )
34- } ) ?;
35- let domain_name_separated =
36- domain_name. to_string ( ) . split ( '.' ) . map ( |s| s. to_owned ( ) ) . collect :: < Vec < _ > > ( ) ;
50+ . into ( )
51+ } )
52+ }
53+
54+ /// Create (insert) the issuer entry for this sonata instance.
55+ pub ( crate ) async fn create_own ( db : & Database ) -> Result < Option < Self > , Error > {
56+ let config_domain = & SonataConfig :: get_or_panic ( ) . general . server_domain ;
57+ let domain_name = Self :: str_to_domain_name ( config_domain) . map_err ( |e| * e) ?;
58+ let domain_name_separated = Self :: domain_name_to_vec_string ( domain_name) ;
3759 let record = query ! (
3860 r#"
3961 INSERT INTO issuers (domain_components)
@@ -48,15 +70,36 @@ impl Issuer {
4870 match record {
4971 Some ( row) => Ok ( Some ( Issuer {
5072 id : row. id ,
51- domain_components : match DomainName :: new ( & row. domain_components . join ( "." ) ) {
52- Err ( e) => {
53- error ! ( "Error: Invalid DomainName stored in issuers table: {e}" ) ;
54- return Err ( Error :: new_internal_error ( None ) ) ;
55- }
56- Ok ( dn) => dn,
57- } ,
73+ domain_components : Self :: vec_string_to_domain_name ( row. domain_components )
74+ . map_err ( |e| * e) ?,
5875 } ) ) ,
5976 None => Ok ( None ) ,
6077 }
6178 }
79+
80+ /// Get the issuer entryfor this sonata instance from the database. Returns
81+ /// `Ok(None)`, if the item does not exist.
82+ pub ( crate ) async fn get_own ( db : & Database ) -> Result < Option < Self > , Error > {
83+ let domain_name =
84+ Self :: str_to_domain_name ( & SonataConfig :: get_or_panic ( ) . general . server_domain )
85+ . map_err ( |e| * e) ?;
86+ let record = query ! (
87+ r#"
88+ SELECT id, domain_components
89+ FROM issuers
90+ WHERE domain_components = $1
91+ "# ,
92+ & Self :: domain_name_to_vec_string( domain_name)
93+ )
94+ . fetch_optional ( & db. pool )
95+ . await ?;
96+ Ok ( match record {
97+ Some ( row) => Some ( Self {
98+ id : row. id ,
99+ domain_components : Self :: vec_string_to_domain_name ( row. domain_components )
100+ . map_err ( |e| * e) ?,
101+ } ) ,
102+ None => None ,
103+ } )
104+ }
62105}
0 commit comments