Files
Modrinth-plus/src/database/models/user_item.rs

116 lines
3.0 KiB
Rust
Raw Normal View History

2020-09-28 10:48:15 -07:00
use super::ids::UserId;
pub struct User {
pub id: UserId,
pub github_id: Option<i64>,
2020-09-28 10:48:15 -07:00
pub username: String,
pub name: Option<String>,
2020-09-28 10:48:15 -07:00
pub email: Option<String>,
pub avatar_url: Option<String>,
pub bio: Option<String>,
2020-09-28 10:48:15 -07:00
pub created: chrono::DateTime<chrono::Utc>,
2020-09-28 21:05:42 -07:00
pub role: String,
2020-09-28 10:48:15 -07:00
}
impl User {
pub async fn insert(
&self,
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
) -> Result<(), sqlx::error::Error> {
sqlx::query!(
"
INSERT INTO users (
id, github_id, username, name, email,
avatar_url, bio, created
)
VALUES (
$1, $2, $3, $4, $5,
$6, $7, $8
)
",
self.id as UserId,
2020-09-28 22:30:13 -07:00
self.github_id,
2020-09-28 10:48:15 -07:00
&self.username,
self.name.as_ref(),
2020-09-28 10:48:15 -07:00
self.email.as_ref(),
self.avatar_url.as_ref(),
self.bio.as_ref(),
2020-09-28 10:48:15 -07:00
self.created,
)
2020-09-28 21:05:42 -07:00
.execute(&mut *transaction)
.await?;
2020-09-28 10:48:15 -07:00
Ok(())
}
2020-09-28 21:05:42 -07:00
pub async fn get<'a, 'b, E>(id: UserId, executor: E) -> Result<Option<Self>, sqlx::error::Error>
where
E: sqlx::Executor<'a, Database = sqlx::Postgres>,
2020-09-28 10:48:15 -07:00
{
let result = sqlx::query!(
"
SELECT u.github_id, u.name, u.email,
u.avatar_url, u.username, u.bio,
2020-09-28 21:05:42 -07:00
u.created, u.role
2020-09-28 10:48:15 -07:00
FROM users u
WHERE u.id = $1
",
id as UserId,
)
2020-09-28 21:05:42 -07:00
.fetch_optional(executor)
.await?;
2020-09-28 10:48:15 -07:00
if let Some(row) = result {
Ok(Some(User {
id,
2020-09-28 22:30:13 -07:00
github_id: row.github_id,
2020-09-28 10:48:15 -07:00
name: row.name,
email: row.email,
avatar_url: row.avatar_url,
username: row.username,
bio: row.bio,
created: row.created,
2020-09-28 21:05:42 -07:00
role: row.role,
2020-09-28 10:48:15 -07:00
}))
} else {
Ok(None)
}
}
pub async fn get_from_github_id<'a, 'b, E>(
2020-09-28 22:30:13 -07:00
github_id: u64,
2020-09-28 10:48:15 -07:00
executor: E,
) -> Result<Option<Self>, sqlx::error::Error>
2020-09-28 21:05:42 -07:00
where
E: sqlx::Executor<'a, Database = sqlx::Postgres>,
2020-09-28 10:48:15 -07:00
{
let result = sqlx::query!(
"
SELECT u.id, u.name, u.email,
u.avatar_url, u.username, u.bio,
2020-09-28 21:05:42 -07:00
u.created, u.role
2020-09-28 10:48:15 -07:00
FROM users u
WHERE u.github_id = $1
",
2020-09-28 22:30:13 -07:00
github_id as i64,
2020-09-28 10:48:15 -07:00
)
2020-09-28 21:05:42 -07:00
.fetch_optional(executor)
.await?;
2020-09-28 10:48:15 -07:00
if let Some(row) = result {
Ok(Some(User {
id: UserId(row.id),
github_id: Some(github_id as i64),
2020-09-28 10:48:15 -07:00
name: row.name,
email: row.email,
avatar_url: row.avatar_url,
username: row.username,
bio: row.bio,
created: row.created,
2020-09-28 21:05:42 -07:00
role: row.role,
2020-09-28 10:48:15 -07:00
}))
} else {
Ok(None)
}
}
2020-09-28 21:05:42 -07:00
}