@@ -16,34 +16,20 @@ pub enum NewCrateOwnerInvitationOutcome {
1616 InviteCreated { plaintext_token : SecretString } ,
1717}
1818
19- /// The model representing a row in the `crate_owner_invitations` database table.
20- #[ derive( Clone , Debug , Identifiable , Queryable ) ]
21- #[ diesel( primary_key( invited_user_id, crate_id) ) ]
22- pub struct CrateOwnerInvitation {
19+ #[ derive( Clone , Debug , Insertable ) ]
20+ #[ diesel( table_name = crate_owner_invitations, check_for_backend( diesel:: pg:: Pg ) ) ]
21+ pub struct NewCrateOwnerInvitation {
2322 pub invited_user_id : i32 ,
2423 pub invited_by_user_id : i32 ,
2524 pub crate_id : i32 ,
26- pub created_at : NaiveDateTime ,
27- #[ diesel( deserialize_as = String ) ]
28- pub token : SecretString ,
2925}
3026
31- impl CrateOwnerInvitation {
27+ impl NewCrateOwnerInvitation {
3228 pub async fn create (
33- invited_user_id : i32 ,
34- invited_by_user_id : i32 ,
35- crate_id : i32 ,
29+ & self ,
3630 conn : & mut AsyncPgConnection ,
3731 config : & config:: Server ,
3832 ) -> QueryResult < NewCrateOwnerInvitationOutcome > {
39- #[ derive( Insertable , Clone , Copy , Debug ) ]
40- #[ diesel( table_name = crate_owner_invitations, check_for_backend( diesel:: pg:: Pg ) ) ]
41- struct NewRecord {
42- invited_user_id : i32 ,
43- invited_by_user_id : i32 ,
44- crate_id : i32 ,
45- }
46-
4733 // Before actually creating the invite, check if an expired invitation already exists
4834 // and delete it from the database. This allows obtaining a new invite if the old one
4935 // expired, instead of returning "already exists".
@@ -52,7 +38,7 @@ impl CrateOwnerInvitation {
5238 // This does a SELECT FOR UPDATE + DELETE instead of a DELETE with a WHERE clause to
5339 // use the model's `is_expired` method, centralizing our expiration checking logic.
5440 let existing: Option < CrateOwnerInvitation > = crate_owner_invitations:: table
55- . find ( ( invited_user_id, crate_id) )
41+ . find ( ( self . invited_user_id , self . crate_id ) )
5642 . for_update ( )
5743 . first ( conn)
5844 . await
@@ -70,11 +56,7 @@ impl CrateOwnerInvitation {
7056 . await ?;
7157
7258 let res: Option < CrateOwnerInvitation > = diesel:: insert_into ( crate_owner_invitations:: table)
73- . values ( & NewRecord {
74- invited_user_id,
75- invited_by_user_id,
76- crate_id,
77- } )
59+ . values ( self )
7860 // The ON CONFLICT DO NOTHING clause results in not creating the invite if another one
7961 // already exists. This does not cause problems with expired invitation as those are
8062 // deleted before doing this INSERT.
@@ -90,7 +72,21 @@ impl CrateOwnerInvitation {
9072 None => NewCrateOwnerInvitationOutcome :: AlreadyExists ,
9173 } )
9274 }
75+ }
9376
77+ /// The model representing a row in the `crate_owner_invitations` database table.
78+ #[ derive( Clone , Debug , Identifiable , Queryable ) ]
79+ #[ diesel( primary_key( invited_user_id, crate_id) ) ]
80+ pub struct CrateOwnerInvitation {
81+ pub invited_user_id : i32 ,
82+ pub invited_by_user_id : i32 ,
83+ pub crate_id : i32 ,
84+ pub created_at : NaiveDateTime ,
85+ #[ diesel( deserialize_as = String ) ]
86+ pub token : SecretString ,
87+ }
88+
89+ impl CrateOwnerInvitation {
9490 pub async fn find_by_id (
9591 user_id : i32 ,
9692 crate_id : i32 ,
0 commit comments