diff --git a/migration/src/lib.rs b/migration/src/lib.rs index 3ca3c3f..d6597c6 100644 --- a/migration/src/lib.rs +++ b/migration/src/lib.rs @@ -2,6 +2,7 @@ pub use sea_orm_migration::prelude::*; mod m20220101_000001_create_table; mod m20230727_015400_load_default_data; +mod m20230727_063415_create_mod_beat_saber_versions; pub struct Migrator; @@ -11,6 +12,7 @@ impl MigratorTrait for Migrator { vec![ Box::new(m20220101_000001_create_table::Migration), Box::new(m20230727_015400_load_default_data::Migration), + Box::new(m20230727_063415_create_mod_beat_saber_versions::Migration), ] } } diff --git a/migration/src/m20230727_063415_create_mod_beat_saber_versions.rs b/migration/src/m20230727_063415_create_mod_beat_saber_versions.rs new file mode 100644 index 0000000..1dbf45a --- /dev/null +++ b/migration/src/m20230727_063415_create_mod_beat_saber_versions.rs @@ -0,0 +1,87 @@ +use sea_orm_migration::prelude::*; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .create_table( + Table::create() + .table(ModBeatSaberVersions::Table) + .if_not_exists() + .col( + ColumnDef::new(ModBeatSaberVersions::ModId) + .uuid() + .not_null(), + ) + .col( + ColumnDef::new(ModBeatSaberVersions::BeatSaberVersionId) + .uuid() + .not_null(), + ) + .foreign_key( + ForeignKey::create() + .name("fk_mod_beat_saber_versions_mods_mod_id") + .from( + ModBeatSaberVersions::Table, + ModBeatSaberVersions::ModId, + ) + .to(Mods::Table, Mods::Id) + .on_delete(ForeignKeyAction::Cascade) + .on_update(ForeignKeyAction::Cascade), + ) + .foreign_key( + ForeignKey::create() + .name("fk_mod_beat_saber_versions_beat_saber_versions_beat_saber_version_id") + .from( + ModBeatSaberVersions::Table, + ModBeatSaberVersions::BeatSaberVersionId, + ) + .to(BeatSaberVersions::Table, BeatSaberVersions::Id) + .on_delete(ForeignKeyAction::Cascade) + .on_update(ForeignKeyAction::Cascade), + ) + .to_owned(), + ) + .await + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .drop_table(Table::drop().table(ModBeatSaberVersions::Table).to_owned()) + .await + } +} + +/// Learn more at https://docs.rs/sea-query#iden +#[derive(Iden)] +enum ModBeatSaberVersions { + Table, + ModId, + BeatSaberVersionId, +} + +#[derive(Iden)] +enum Mods { + Table, + Id, + Slug, + Name, + Description, + Icon, + Cover, + Author, + Category, + Stats, + CreatedAt, + UpdatedAt, +} + +#[derive(Iden)] +enum BeatSaberVersions { + Table, + Id, + Ver, +} \ No newline at end of file diff --git a/src/mods.rs b/src/mods.rs index 12da3ce..9ba9583 100644 --- a/src/mods.rs +++ b/src/mods.rs @@ -94,7 +94,7 @@ pub struct ModCategory { pub desc: Option, } -pub async fn find_all(db: &Database, limit: i32, offset: i32) -> FieldResult> { +pub async fn find_all(db: &Database, limit: i32, offset: i32, version: Option) -> FieldResult> { let limit = limit as u64; let offset = offset as u64; diff --git a/src/schema.rs b/src/schema.rs index dedf64f..e8a7cd2 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -1,4 +1,6 @@ -use juniper::{graphql_value, EmptyMutation, EmptySubscription, FieldResult, RootNode}; +use juniper::{ + graphql_value, EmptyMutation, EmptySubscription, FieldResult, GraphQLEnum, RootNode, +}; #[derive(GraphQLEnum)] enum Episode { @@ -7,7 +9,6 @@ enum Episode { Jedi, } -use juniper::{GraphQLEnum}; use uuid::Uuid; use crate::auth::Authorization; @@ -22,13 +23,12 @@ impl QueryRoot { async fn user_by_id(db: &Database, id: Uuid, auth: Option) -> FieldResult { users::find_by_id(db, id, Authorization::parse(auth)).await } - + async fn users( db: &Database, limit: Option, offset: Option, auth: Option, - _version: Option, ) -> FieldResult> { if limit > Some(10) { return Err(juniper::FieldError::new( @@ -36,16 +36,27 @@ impl QueryRoot { graphql_value!({ "limit": "Limit must be less than 10" }), )); } - users::find_all(db, limit.unwrap_or(10), offset.unwrap_or(0), Authorization::parse(auth)).await + users::find_all( + db, + limit.unwrap_or(10), + offset.unwrap_or(0), + Authorization::parse(auth), + ) + .await } - async fn mods(db: &Database, limit: Option, offset: Option) -> FieldResult> { + async fn mods( + db: &Database, + limit: Option, + offset: Option, + version: Option, + ) -> FieldResult> { if limit > Some(10) { return Err(juniper::FieldError::new( "Limit must be less than 10", graphql_value!({ "limit": "Limit must be less than 10" }), )); } - mods::find_all(db, limit.unwrap_or(10), offset.unwrap_or(0)).await + mods::find_all(db, limit.unwrap_or(10), offset.unwrap_or(0), version).await } async fn mod_by_id(db: &Database, id: Uuid) -> FieldResult { mods::find_by_id(db, id).await diff --git a/src/versions.rs b/src/versions.rs index a19e533..8965cfb 100644 --- a/src/versions.rs +++ b/src/versions.rs @@ -7,9 +7,7 @@ use serde::{Serialize, Deserialize}; use sea_orm::{ColumnTrait, EntityTrait, QueryFilter}; use uuid::Uuid; -use crate::{ - Database, -}; +use crate::Database; #[derive(GraphQLObject, Debug, Deserialize, Serialize)] pub struct GVersion {