File tree Expand file tree Collapse file tree 2 files changed +18
-11
lines changed Expand file tree Collapse file tree 2 files changed +18
-11
lines changed Original file line number Diff line number Diff line change @@ -2,7 +2,7 @@ use crate::app::AppState;
2
2
use crate :: auth:: AuthCheck ;
3
3
use crate :: controllers:: helpers:: ok_true;
4
4
use crate :: models:: NewEmail ;
5
- use crate :: schema:: { emails , users} ;
5
+ use crate :: schema:: users;
6
6
use crate :: util:: errors:: { bad_request, server_error, AppResult } ;
7
7
use axum:: extract:: Path ;
8
8
use axum:: response:: Response ;
@@ -99,16 +99,8 @@ pub async fn update_user(
99
99
verified : false ,
100
100
} ;
101
101
102
- let token = diesel:: insert_into ( emails:: table)
103
- . values ( & new_email)
104
- . on_conflict ( emails:: user_id)
105
- . do_update ( )
106
- . set ( & new_email)
107
- . returning ( emails:: token)
108
- . get_result :: < String > ( & mut conn)
109
- . await
110
- . map ( SecretString :: from)
111
- . map_err ( |_| server_error ( "Error in creating token" ) ) ?;
102
+ let token = new_email. insert_or_update ( & mut conn) . await ;
103
+ let token = token. map_err ( |_| server_error ( "Error in creating token" ) ) ?;
112
104
113
105
// This swallows any errors that occur while attempting to send the email. Some users have
114
106
// an invalid email set in their GitHub profile, and we should let them sign in even though
Original file line number Diff line number Diff line change @@ -51,4 +51,19 @@ impl NewEmail<'_> {
51
51
. map ( Into :: into)
52
52
. optional ( )
53
53
}
54
+
55
+ pub async fn insert_or_update (
56
+ & self ,
57
+ conn : & mut AsyncPgConnection ,
58
+ ) -> QueryResult < SecretString > {
59
+ diesel:: insert_into ( emails:: table)
60
+ . values ( self )
61
+ . on_conflict ( emails:: user_id)
62
+ . do_update ( )
63
+ . set ( self )
64
+ . returning ( emails:: token)
65
+ . get_result :: < String > ( conn)
66
+ . await
67
+ . map ( Into :: into)
68
+ }
54
69
}
You can’t perform that action at this time.
0 commit comments