JavaScript is disabled, refresh for a better experience. ambee/giterated

ambee/giterated

Git repository hosting, collaboration, and discovery for the Fediverse.

Add users table

Amber - ⁨2⁩ years ago

parent: tbd commit: ⁨9f36e3f

⁨src/backend/user.rs⁩ - ⁨2204⁩ bytes
Raw
1 use std::error::Error;
2
3 use sqlx::PgPool;
4
5 use crate::messages::user::{
6 UserBioRequest, UserBioResponse, UserDisplayImageRequest, UserDisplayImageResponse,
7 UserDisplayNameRequest, UserDisplayNameResponse,
8 };
9
10 use super::{AuthBackend, UserBackend};
11
12 pub struct UserAuth {
13 pub pg_pool: PgPool,
14 }
15
16 impl UserAuth {
17 pub fn new(pool: PgPool) -> Self {
18 Self { pg_pool: pool }
19 }
20 }
21
22 #[async_trait::async_trait]
23 impl UserBackend for UserAuth {
24 async fn display_name(
25 &mut self,
26 request: UserDisplayNameRequest,
27 ) -> Result<UserDisplayNameResponse, Box<dyn std::error::Error + Send>> {
28 let db_row = sqlx::query_as!(
29 UserRow,
30 r#"SELECT * FROM users WHERE username = $1"#,
31 request.user.username
32 )
33 .await?;
34
35 Ok(UserDisplayNameResponse {
36 display_name: db_row.display_name,
37 })
38 }
39
40 async fn display_image(
41 &mut self,
42 request: UserDisplayImageRequest,
43 ) -> Result<UserDisplayImageResponse, Box<dyn std::error::Error + Send>> {
44 let db_row = sqlx::query_as!(
45 UserRow,
46 r#"SELECT * FROM users WHERE username = $1"#,
47 request.user.username
48 )
49 .await?;
50
51 Ok(UserDisplayImageResponse {
52 image_url: db_row.image_url,
53 })
54 }
55
56 async fn bio(
57 &mut self,
58 request: UserBioRequest,
59 ) -> Result<UserBioResponse, Box<dyn std::error::Error + Send>> {
60 let db_row = sqlx::query_as!(
61 UserRow,
62 r#"SELECT * FROM users WHERE username = $1"#,
63 request.user.username
64 )
65 .await?;
66
67 Ok(UserBioResponse { bio: db_row.bio })
68 }
69 }
70
71 #[async_trait::async_trait]
72 impl AuthBackend for UserAuth {
73 async fn register(&mut self, request: ()) -> Result<(), Box<dyn Error + Send>> {
74 todo!()
75 }
76
77 async fn login(&mut self, request: ()) -> Result<(), Box<dyn Error + Send>> {
78 todo!()
79 }
80 }
81
82 #[derive(Debug, sqlx::FromRow)]
83 struct UserRow {
84 pub username: String,
85 pub display_name: Option<String>,
86 pub bio: Option<String>,
87 pub email: Option<String>,
88 pub password: String,
89 }
90