2020-10-20 00:24:40 -05:00
|
|
|
#![allow(dead_code)]
|
2020-07-23 22:46:33 +02:00
|
|
|
// TODO: remove attr once routes are created
|
2020-06-30 19:23:52 +02:00
|
|
|
|
2020-07-23 22:46:33 +02:00
|
|
|
use thiserror::Error;
|
|
|
|
|
|
2020-08-12 14:54:03 -05:00
|
|
|
pub mod categories;
|
2020-07-23 22:46:33 +02:00
|
|
|
pub mod ids;
|
|
|
|
|
pub mod mod_item;
|
2021-02-13 12:11:13 -07:00
|
|
|
pub mod report_item;
|
2020-07-23 22:46:33 +02:00
|
|
|
pub mod team_item;
|
2020-09-28 10:48:15 -07:00
|
|
|
pub mod user_item;
|
2020-09-28 21:05:42 -07:00
|
|
|
pub mod version_item;
|
2020-07-23 22:46:33 +02:00
|
|
|
|
|
|
|
|
pub use ids::*;
|
2020-06-30 19:23:52 +02:00
|
|
|
pub use mod_item::Mod;
|
2020-07-16 10:16:35 -07:00
|
|
|
pub use team_item::Team;
|
|
|
|
|
pub use team_item::TeamMember;
|
2020-09-28 21:05:42 -07:00
|
|
|
pub use user_item::User;
|
2020-07-16 10:16:35 -07:00
|
|
|
pub use version_item::FileHash;
|
2020-06-30 19:23:52 +02:00
|
|
|
pub use version_item::Version;
|
2020-07-16 10:16:35 -07:00
|
|
|
pub use version_item::VersionFile;
|
2020-06-30 19:23:52 +02:00
|
|
|
|
2020-07-23 22:46:33 +02:00
|
|
|
#[derive(Error, Debug)]
|
|
|
|
|
pub enum DatabaseError {
|
2021-01-28 09:07:10 -07:00
|
|
|
#[error("Error while interacting with the database: {0}")]
|
2020-07-23 22:46:33 +02:00
|
|
|
DatabaseError(#[from] sqlx::error::Error),
|
|
|
|
|
#[error("Error while trying to generate random ID")]
|
|
|
|
|
RandomIdError,
|
2020-08-12 14:54:03 -05:00
|
|
|
#[error(
|
|
|
|
|
"Invalid identifier: Category/version names must contain only ASCII \
|
|
|
|
|
alphanumeric characters or '_-'."
|
|
|
|
|
)]
|
|
|
|
|
InvalidIdentifier(String),
|
2020-11-09 19:39:23 -07:00
|
|
|
#[error("Invalid permissions bitflag!")]
|
|
|
|
|
BitflagError,
|
|
|
|
|
#[error("A database request failed")]
|
|
|
|
|
Other(String),
|
2020-06-30 19:23:52 +02:00
|
|
|
}
|
2020-10-17 21:34:23 -05:00
|
|
|
|
|
|
|
|
impl ids::ChannelId {
|
|
|
|
|
pub async fn get_id<'a, E>(
|
|
|
|
|
channel: &str,
|
|
|
|
|
exec: E,
|
|
|
|
|
) -> Result<Option<ids::ChannelId>, DatabaseError>
|
|
|
|
|
where
|
|
|
|
|
E: sqlx::Executor<'a, Database = sqlx::Postgres>,
|
|
|
|
|
{
|
|
|
|
|
let result = sqlx::query!(
|
|
|
|
|
"
|
|
|
|
|
SELECT id FROM release_channels
|
|
|
|
|
WHERE channel = $1
|
|
|
|
|
",
|
|
|
|
|
channel
|
|
|
|
|
)
|
|
|
|
|
.fetch_optional(exec)
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
Ok(result.map(|r| ids::ChannelId(r.id)))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ids::StatusId {
|
|
|
|
|
pub async fn get_id<'a, E>(
|
|
|
|
|
status: &crate::models::mods::ModStatus,
|
|
|
|
|
exec: E,
|
|
|
|
|
) -> Result<Option<Self>, DatabaseError>
|
|
|
|
|
where
|
|
|
|
|
E: sqlx::Executor<'a, Database = sqlx::Postgres>,
|
|
|
|
|
{
|
|
|
|
|
let result = sqlx::query!(
|
|
|
|
|
"
|
|
|
|
|
SELECT id FROM statuses
|
|
|
|
|
WHERE status = $1
|
|
|
|
|
",
|
|
|
|
|
status.as_str()
|
|
|
|
|
)
|
|
|
|
|
.fetch_optional(exec)
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
Ok(result.map(|r| ids::StatusId(r.id)))
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-27 10:57:04 -07:00
|
|
|
|
|
|
|
|
impl ids::SideTypeId {
|
|
|
|
|
pub async fn get_id<'a, E>(
|
|
|
|
|
side: &crate::models::mods::SideType,
|
|
|
|
|
exec: E,
|
|
|
|
|
) -> Result<Option<Self>, DatabaseError>
|
|
|
|
|
where
|
|
|
|
|
E: sqlx::Executor<'a, Database = sqlx::Postgres>,
|
|
|
|
|
{
|
|
|
|
|
let result = sqlx::query!(
|
|
|
|
|
"
|
|
|
|
|
SELECT id FROM side_types
|
|
|
|
|
WHERE name = $1
|
|
|
|
|
",
|
|
|
|
|
side.as_str()
|
|
|
|
|
)
|
|
|
|
|
.fetch_optional(exec)
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
Ok(result.map(|r| ids::SideTypeId(r.id)))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ids::DonationPlatformId {
|
|
|
|
|
pub async fn get_id<'a, E>(id: &str, exec: E) -> Result<Option<Self>, DatabaseError>
|
|
|
|
|
where
|
|
|
|
|
E: sqlx::Executor<'a, Database = sqlx::Postgres>,
|
|
|
|
|
{
|
|
|
|
|
let result = sqlx::query!(
|
|
|
|
|
"
|
|
|
|
|
SELECT id FROM donation_platforms
|
|
|
|
|
WHERE short = $1
|
|
|
|
|
",
|
|
|
|
|
id
|
|
|
|
|
)
|
|
|
|
|
.fetch_optional(exec)
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
Ok(result.map(|r| ids::DonationPlatformId(r.id)))
|
|
|
|
|
}
|
|
|
|
|
}
|