@@ -4,101 +4,16 @@ use sqlx::{FromRow, PgPool};
44use uuid:: Uuid ;
55
66#[ derive( Debug , Clone , FromRow , Serialize , Deserialize ) ]
7- pub struct DBProjectApiKey {
8- pub project_id : Uuid ,
9- pub name : Option < String > ,
10- pub hash : String ,
11- pub shorthand : String ,
12- }
13-
14- impl DBProjectApiKey {
15- pub fn into_with_raw ( self , raw : String ) -> ProjectApiKey {
16- ProjectApiKey {
17- project_id : self . project_id ,
18- name : self . name ,
19- hash : self . hash ,
20- shorthand : self . shorthand ,
21- raw,
22- }
23- }
24- }
25-
26- // TODO: Remove this and use DBProjectApiKey directly
27- #[ derive( Clone ) ]
7+ #[ serde( rename_all = "camelCase" ) ]
288pub struct ProjectApiKey {
299 pub project_id : Uuid ,
3010 pub name : Option < String > ,
3111 pub hash : String ,
3212 pub shorthand : String ,
33- pub raw : String ,
34- }
35-
36- impl Into < DBProjectApiKey > for ProjectApiKey {
37- fn into ( self ) -> DBProjectApiKey {
38- DBProjectApiKey {
39- project_id : self . project_id ,
40- name : self . name ,
41- hash : self . hash ,
42- shorthand : self . shorthand ,
43- }
44- }
45- }
46-
47- #[ derive( Serialize , FromRow ) ]
48- pub struct ProjectApiKeyResponse {
49- pub id : Uuid ,
50- pub project_id : Uuid ,
51- pub name : Option < String > ,
52- pub shorthand : String ,
53- }
54-
55- pub async fn create_project_api_key (
56- pool : & PgPool ,
57- project_id : & Uuid ,
58- name : & Option < String > ,
59- hash : & String ,
60- shorthand : & String ,
61- ) -> Result < DBProjectApiKey > {
62- let key_info = sqlx:: query_as :: < _ , DBProjectApiKey > (
63- "INSERT
64- INTO project_api_keys (shorthand, project_id, name, hash)
65- VALUES ($1, $2, $3, $4)
66- RETURNING id, project_id, name, hash, shorthand" ,
67- )
68- . bind ( & shorthand)
69- . bind ( & project_id)
70- . bind ( & name)
71- . bind ( & hash)
72- . fetch_one ( pool)
73- . await ?;
74-
75- Ok ( key_info)
7613}
7714
78- pub async fn get_api_keys_for_project (
79- db : & PgPool ,
80- project_id : & Uuid ,
81- ) -> Result < Vec < ProjectApiKeyResponse > > {
82- let api_keys = sqlx:: query_as :: < _ , ProjectApiKeyResponse > (
83- "SELECT
84- project_api_keys.project_id,
85- project_api_keys.name,
86- project_api_keys.id,
87- project_api_keys.shorthand
88- FROM
89- project_api_keys
90- WHERE
91- project_api_keys.project_id = $1" ,
92- )
93- . bind ( project_id)
94- . fetch_all ( db)
95- . await ?;
96-
97- Ok ( api_keys)
98- }
99-
100- pub async fn get_api_key ( pool : & PgPool , hash : & String ) -> Result < DBProjectApiKey > {
101- let api_key = match sqlx:: query_as :: < _ , DBProjectApiKey > (
15+ pub async fn get_api_key ( pool : & PgPool , hash : & String ) -> Result < ProjectApiKey > {
16+ let api_key = match sqlx:: query_as :: < _ , ProjectApiKey > (
10217 "SELECT
10318 project_api_keys.hash,
10419 project_api_keys.project_id,
@@ -121,23 +36,3 @@ pub async fn get_api_key(pool: &PgPool, hash: &String) -> Result<DBProjectApiKey
12136
12237 Ok ( api_key)
12338}
124-
125- #[ derive( FromRow ) ]
126- struct ProjectApiKeyHash {
127- hash : String ,
128- }
129-
130- pub async fn delete_api_key ( pool : & PgPool , id : & Uuid , project_id : & Uuid ) -> Result < String > {
131- let row = sqlx:: query_as :: < _ , ProjectApiKeyHash > (
132- "DELETE
133- FROM project_api_keys
134- WHERE id = $1 AND project_id = $2
135- RETURNING hash" ,
136- )
137- . bind ( id)
138- . bind ( project_id)
139- . fetch_one ( pool)
140- . await ?;
141-
142- Ok ( row. hash )
143- }
0 commit comments