1+ use std:: collections:: HashMap ;
2+ use aws_sdk_dynamodb:: types:: AttributeValue ;
3+ use http:: StatusCode ;
4+ use lambda_http:: tracing:: { error, info} ;
15use serde:: { Deserialize , Serialize } ;
6+ use crate :: dynamo:: { as_map, as_map_vec, as_string} ;
7+
8+ #[ derive( Clone , Serialize , Deserialize ) ]
9+ pub struct VerifyRole {
10+ pub role_id : String ,
11+ pub role_name : String ,
12+ pub pattern : String ,
13+ pub member_count : u32 ,
14+ }
15+
16+ #[ derive( Clone , Serialize , Deserialize ) ]
17+ pub struct Verify {
18+ pub roles : Vec < VerifyRole >
19+ }
220
321#[ derive( Clone , Serialize , Deserialize ) ]
422pub struct Guild {
5- guild_id : String ,
6- verify : String
23+ pub guild_id : String ,
24+ pub verify : Verify
25+ }
26+
27+ impl From < & HashMap < String , AttributeValue > > for Guild {
28+ fn from ( item : & HashMap < String , AttributeValue > ) -> Self {
29+ Guild {
30+ guild_id : as_string ( item. get ( "guild_id" ) , & "" . to_string ( ) ) ,
31+ verify : as_map ( item. get ( "verify" ) ) . map (
32+ |m| Verify {
33+ roles : as_map_vec ( m. get ( "roles" ) )
34+ . into_iter ( )
35+ . map ( |role_map| VerifyRole {
36+ role_id : as_string ( role_map. get ( "role_id" ) , & "" . to_string ( ) ) ,
37+ role_name : as_string ( role_map. get ( "role_name" ) , & "" . to_string ( ) ) ,
38+ pattern : as_string ( role_map. get ( "pattern" ) , & "" . to_string ( ) ) ,
39+ member_count : role_map. get ( "member_count" )
40+ . and_then ( |v| v. as_n ( ) . ok ( ) )
41+ . and_then ( |n| n. parse :: < u32 > ( ) . ok ( ) )
42+ . unwrap_or ( 0 ) ,
43+ } )
44+ . collect ( ) ,
45+ } ) . unwrap_or ( Verify { roles : vec ! [ ] } ) ,
46+ }
47+ }
48+ }
49+
50+ impl Into < HashMap < String , AttributeValue > > for Guild {
51+ fn into ( self ) -> HashMap < String , AttributeValue > {
52+ let mut item = HashMap :: new ( ) ;
53+ item. insert ( "guild_id" . to_string ( ) , AttributeValue :: S ( self . guild_id ) ) ;
54+
55+ let roles: Vec < AttributeValue > = self . verify . roles . into_iter ( ) . map ( |role| {
56+ let mut role_map = HashMap :: new ( ) ;
57+ role_map. insert ( "role_id" . to_string ( ) , AttributeValue :: S ( role. role_id ) ) ;
58+ role_map. insert ( "role_name" . to_string ( ) , AttributeValue :: S ( role. role_name ) ) ;
59+ role_map. insert ( "pattern" . to_string ( ) , AttributeValue :: S ( role. pattern ) ) ;
60+ role_map. insert ( "member_count" . to_string ( ) , AttributeValue :: N ( role. member_count . to_string ( ) ) ) ;
61+ AttributeValue :: M ( role_map)
62+ } ) . collect ( ) ;
63+
64+ item. insert ( "verify" . to_string ( ) , AttributeValue :: M ( HashMap :: from ( [ ( "roles" . to_string ( ) , AttributeValue :: L ( roles) ) ] ) ) ) ;
65+
66+ item
67+ }
68+ }
69+
70+ impl Guild {
71+ pub async fn from_db ( guild_id : & str , dynamo : & aws_sdk_dynamodb:: Client ) -> Option < Guild > {
72+ info ! ( "a" ) ;
73+ let a = dynamo. query ( ) . table_name ( format ! ( "kb2_guilds_{}" , std:: env:: var( "DEPLOYMENT_ENV" ) . expect( "DEPLOYMENT_ENV must be set" ) , ) ) ;
74+ info ! ( "b" ) ;
75+ let b = a. key_condition_expression ( "#uid = :uid" ) ;
76+ info ! ( "c" ) ;
77+ let c = b. expression_attribute_names ( "#uid" , "guild_id" ) ;
78+ info ! ( "d" ) ;
79+ let d = c. expression_attribute_values ( ":uid" , AttributeValue :: S ( guild_id. to_string ( ) ) ) ;
80+ info ! ( "e" ) ;
81+ let e = d. send ( ) ;
82+ info ! ( "f" ) ;
83+ let f = e. await ;
84+ info ! ( "g" ) ;
85+
86+ match f
87+ . map_err ( |_| StatusCode :: INTERNAL_SERVER_ERROR )
88+ . and_then ( |resp| {
89+ info ! ( "100" ) ;
90+ let items = resp. items . unwrap_or_default ( ) ;
91+ info ! ( "200" ) ;
92+ if items. is_empty ( ) {
93+ return Err ( StatusCode :: NOT_FOUND ) ;
94+ }
95+ let guild: Guild = ( & items[ 0 ] ) . into ( ) ;
96+ Ok ( guild)
97+ } ) {
98+ Ok ( guild) => Some ( guild) ,
99+ Err ( e) => {
100+ error ! ( "Error fetching guild from DynamoDB: {}" , e) ;
101+ None
102+ }
103+ }
104+
105+ }
106+
107+ pub async fn save ( & self , dynamo : & aws_sdk_dynamodb:: Client ) {
108+
109+ match dynamo
110+ . put_item ( )
111+ . table_name ( format ! ( "kb2_guilds_{}" , std:: env:: var( "DEPLOYMENT_ENV" ) . expect( "DEPLOYMENT_ENV must be set" ) ) )
112+ . set_item ( Some ( self . clone ( ) . into ( ) ) )
113+ . send ( )
114+ . await
115+ {
116+ Ok ( _) => ( ) ,
117+ Err ( e) => {
118+ error ! ( "DynamoDB write error: {}" , e) ;
119+ panic ! ( "Failed to save guild to DynamoDB" ) ;
120+ }
121+ }
122+ }
7123}
0 commit comments