11use crate :: get_secrets;
2- use crate :: serenity:: { User , UserId , Command } ;
2+ use crate :: serenity:: { Command , User , UserId } ;
33use crate :: Error ;
44use sqlx:: postgres:: PgPool ;
5+ use sqlx:: types:: chrono:: { DateTime , Utc } ;
56use tracing:: { span, Level } ;
67
78pub struct Client {
89 pub pool : PgPool ,
910}
1011
12+ pub struct DbPassenger {
13+ pub id : i64 ,
14+ pub created_at : Option < DateTime < Utc > > // This should not be optional but whatever i guess
15+ }
16+
1117impl Client {
1218 pub async fn new ( ) -> Result < Self , sqlx:: Error > {
1319 let secrets = get_secrets ( ) ;
@@ -27,7 +33,7 @@ impl Client {
2733 }
2834
2935 /// Adds a user, a.k.a 'Passenger' to the database
30- pub async fn add_user ( self : Self , user : User ) -> Result < UserId , Error > {
36+ pub async fn add_user ( & self , user : User ) -> Result < UserId , Error > {
3137 sqlx:: query!(
3238 r#"INSERT INTO users(id) VALUES ( $1 )"# ,
3339 user. id. get( ) as i32
@@ -39,25 +45,50 @@ impl Client {
3945 }
4046
4147 /// Gets a user, a.k.a 'Passenger' from the database
42- pub async fn get_user ( self : Self , user : User ) -> Result < UserId , Error > {
43- sqlx:: query! ( r#"SELECT * FROM users WHERE id = $1"# , user. id. get( ) as i32 )
48+ pub async fn get_user ( & self , user : User ) -> Result < DbPassenger , Error > {
49+ let result = sqlx:: query_as! ( DbPassenger , r#"SELECT * FROM users WHERE id = $1"# , user. id. get( ) as i32 )
4450 . fetch_one ( & self . pool )
4551 . await ?;
4652
47- Ok ( user . id )
53+ Ok ( result )
4854 }
4955
50- pub async fn command_exists ( & self , command : Command ) -> Result < bool , Error > {
51- sqlx:: query!( r#"SELECT * FROM command_stats WHERE command_name = $1"# , command. name)
56+ /// Gets an Agency ID by checking short or long name
57+ pub async fn get_agency ( & self , agency : String ) -> Result < i32 , Error > {
58+ let result = sqlx:: query!(
59+ r#"SELECT id FROM agencies WHERE LOWER(short_name) = LOWER($1) OR LOWER(long_name) = LOWER($1)"# ,
60+ agency
61+ )
5262 . fetch_one ( & self . pool )
5363 . await ?;
64+ Ok ( result. id )
65+ }
66+
67+ pub async fn command_exists ( & self , command : Command ) -> Result < bool , Error > {
68+ sqlx:: query!(
69+ r#"SELECT * FROM command_stats WHERE command_name = $1"# ,
70+ command. name
71+ )
72+ . fetch_one ( & self . pool )
73+ . await ?;
5474 Ok ( true )
5575 }
5676
5777 /// Adds an use to a specified command
5878 pub async fn update_command ( & self , command : Command ) -> Result < ( ) , Error > {
59- self . command_exists ( command. clone ( ) ) . await . expect ( & format ! ( "Command '{}' does not exist" , command. name. to_string( ) ) ) ;
79+ self . command_exists ( command. clone ( ) ) . await . expect ( & format ! (
80+ "Command '{}' does not exist" ,
81+ command. name. to_string( )
82+ ) ) ;
6083 sqlx:: query!( r#"UPDATE command_stats SET command_count = (command_count + 1) WHERE command_name = $1"# , command. clone( ) . name) . execute ( & self . pool ) . await ?;
6184 Ok ( ( ) )
6285 }
6386}
87+
88+ impl Clone for Client {
89+ fn clone ( & self ) -> Self {
90+ Self {
91+ pool : self . pool . clone ( ) ,
92+ }
93+ }
94+ }
0 commit comments