diff --git a/server/src/api/mod.rs b/server/src/api/mod.rs index cf6f94c..800587f 100644 --- a/server/src/api/mod.rs +++ b/server/src/api/mod.rs @@ -23,7 +23,8 @@ async fn get_repos( ) -> crate::Result>> { let (total_pages, repos) = global .db - .repos( + .repo + .page( pagination.per_page.unwrap_or(25), pagination.page.unwrap_or(1) - 1, ) @@ -37,7 +38,8 @@ async fn get_single_repo( ) -> crate::Result> { let repo = global .db - .repo(id) + .repo + .by_id(id) .await? .ok_or(axum::http::StatusCode::NOT_FOUND)?; @@ -50,7 +52,8 @@ async fn get_packages( ) -> crate::Result>> { let (total_pages, pkgs) = global .db - .packages( + .pkg + .page( pagination.per_page.unwrap_or(25), pagination.page.unwrap_or(1) - 1, ) @@ -65,7 +68,8 @@ async fn get_single_package( ) -> crate::Result> { let entry = global .db - .full_package(id) + .pkg + .full(id) .await? .ok_or(axum::http::StatusCode::NOT_FOUND)?; diff --git a/server/src/db/mod.rs b/server/src/db/mod.rs index 1da82dd..6ff5a8f 100644 --- a/server/src/db/mod.rs +++ b/server/src/db/mod.rs @@ -1,12 +1,9 @@ mod conn; pub mod entities; mod migrator; +mod query; -use sea_orm::{ - ActiveModelTrait, ColumnTrait, ConnectOptions, Database, DatabaseConnection, DeleteResult, - DeriveActiveEnum, EntityTrait, EnumIter, InsertResult, ModelTrait, NotSet, PaginatorTrait, - QueryFilter, QueryOrder, Set, -}; +use sea_orm::{ConnectOptions, Database, DatabaseConnection, DeriveActiveEnum, EnumIter}; use sea_orm_migration::MigratorTrait; use serde::{Deserialize, Serialize}; @@ -42,7 +39,9 @@ pub struct FullPackage { #[derive(Clone, Debug)] pub struct RieterDb { - pub conn: DatabaseConnection, + conn: DatabaseConnection, + pub pkg: query::PackageQuery, + pub repo: query::RepoQuery, } impl RieterDb { @@ -51,244 +50,10 @@ impl RieterDb { Migrator::up(&db, None).await?; - Ok(Self { conn: db }) - } - - pub async fn repos(&self, per_page: u64, page: u64) -> Result<(u64, Vec)> { - let paginator = Repo::find() - .order_by_asc(repo::Column::Id) - .paginate(&self.conn, per_page); - let repos = paginator.fetch_page(page).await?; - let total_pages = paginator.num_pages().await?; - - Ok((total_pages, repos)) - } - - pub async fn repo(&self, id: i32) -> Result> { - repo::Entity::find_by_id(id).one(&self.conn).await - } - - pub async fn repo_by_name(&self, name: &str) -> Result> { - Repo::find() - .filter(repo::Column::Name.eq(name)) - .one(&self.conn) - .await - } - - pub async fn insert_repo( - &self, - name: &str, - description: Option<&str>, - ) -> Result> { - let model = repo::ActiveModel { - id: NotSet, - name: Set(String::from(name)), - description: Set(description.map(String::from)), - }; - - Repo::insert(model).exec(&self.conn).await - } - - pub async fn packages(&self, per_page: u64, page: u64) -> Result<(u64, Vec)> { - let paginator = Package::find() - .order_by_asc(package::Column::Id) - .paginate(&self.conn, per_page); - let packages = paginator.fetch_page(page).await?; - let total_pages = paginator.num_pages().await?; - - Ok((total_pages, packages)) - } - - pub async fn package(&self, id: i32) -> Result> { - package::Entity::find_by_id(id).one(&self.conn).await - } - - pub async fn package_by_fields( - &self, - repo_id: i32, - name: &str, - version: Option<&str>, - arch: &str, - ) -> Result> { - let mut query = Package::find() - .filter(package::Column::RepoId.eq(repo_id)) - .filter(package::Column::Name.eq(name)) - .filter(package::Column::Arch.eq(arch)); - - if let Some(version) = version { - query = query.filter(package::Column::Version.eq(version)); - } - - query.one(&self.conn).await - } - - pub async fn delete_packages_with_arch( - &self, - repo_id: i32, - arch: &str, - ) -> Result { - Package::delete_many() - .filter(package::Column::RepoId.eq(repo_id)) - .filter(package::Column::Arch.eq(arch)) - .exec(&self.conn) - .await - } - - pub async fn insert_package( - &self, - repo_id: i32, - pkg: crate::repo::package::Package, - ) -> Result<()> { - let info = pkg.info; - - let model = package::ActiveModel { - id: NotSet, - repo_id: Set(repo_id), - base: Set(info.base), - name: Set(info.name), - version: Set(info.version), - arch: Set(info.arch), - size: Set(info.size), - c_size: Set(info.csize), - description: Set(info.description), - url: Set(info.url), - build_date: Set(info.build_date), - packager: Set(info.packager), - pgp_sig: Set(info.pgpsig), - pgp_sig_size: Set(info.pgpsigsize), - sha256_sum: Set(info.sha256sum), - }; - - let pkg_entry = model.insert(&self.conn).await?; - - // Insert all the related tables - PackageLicense::insert_many(info.licenses.iter().map(|s| package_license::ActiveModel { - package_id: Set(pkg_entry.id), - value: Set(s.to_string()), - })) - .on_empty_do_nothing() - .exec(self) - .await?; - - PackageGroup::insert_many(info.groups.iter().map(|s| package_group::ActiveModel { - package_id: Set(pkg_entry.id), - value: Set(s.to_string()), - })) - .on_empty_do_nothing() - .exec(self) - .await?; - - PackageReplaces::insert_many(info.replaces.iter().map(|s| package_replaces::ActiveModel { - package_id: Set(pkg_entry.id), - value: Set(s.to_string()), - })) - .on_empty_do_nothing() - .exec(self) - .await?; - - PackageConflicts::insert_many(info.conflicts.iter().map(|s| { - package_conflicts::ActiveModel { - package_id: Set(pkg_entry.id), - value: Set(s.to_string()), - } - })) - .on_empty_do_nothing() - .exec(self) - .await?; - - PackageProvides::insert_many(info.provides.iter().map(|s| package_provides::ActiveModel { - package_id: Set(pkg_entry.id), - value: Set(s.to_string()), - })) - .on_empty_do_nothing() - .exec(self) - .await?; - - PackageFile::insert_many(pkg.files.iter().map(|s| package_file::ActiveModel { - package_id: Set(pkg_entry.id), - value: Set(s.display().to_string()), - })) - .on_empty_do_nothing() - .exec(self) - .await?; - - let deps = info - .depends - .iter() - .map(|d| (PackageDepend::Depend, d)) - .chain(info.makedepends.iter().map(|d| (PackageDepend::Make, d))) - .chain(info.checkdepends.iter().map(|d| (PackageDepend::Check, d))) - .chain(info.optdepends.iter().map(|d| (PackageDepend::Opt, d))) - .map(|(t, s)| package_depends::ActiveModel { - package_id: Set(pkg_entry.id), - r#type: Set(t), - value: Set(s.to_string()), - }); - - PackageDepends::insert_many(deps) - .on_empty_do_nothing() - .exec(self) - .await?; - - Ok(()) - } - - pub async fn full_package(&self, id: i32) -> Result> { - if let Some(entry) = self.package(id).await? { - let licenses = entry - .find_related(PackageLicense) - .all(self) - .await? - .into_iter() - .map(|e| e.value) - .collect(); - let groups = entry - .find_related(PackageGroup) - .all(self) - .await? - .into_iter() - .map(|e| e.value) - .collect(); - let replaces = entry - .find_related(PackageReplaces) - .all(self) - .await? - .into_iter() - .map(|e| e.value) - .collect(); - let provides = entry - .find_related(PackageProvides) - .all(self) - .await? - .into_iter() - .map(|e| e.value) - .collect(); - let depends = entry - .find_related(PackageDepends) - .all(self) - .await? - .into_iter() - .map(|e| (e.r#type, e.value)) - .collect(); - let files = entry - .find_related(PackageFile) - .all(self) - .await? - .into_iter() - .map(|e| e.value) - .collect(); - - Ok(Some(FullPackage { - entry, - licenses, - groups, - replaces, - provides, - depends, - files, - })) - } else { - Ok(None) - } + Ok(Self { + conn: db.clone(), + pkg: query::PackageQuery::new(db.clone()), + repo: query::RepoQuery::new(db.clone()), + }) } } diff --git a/server/src/db/query/mod.rs b/server/src/db/query/mod.rs new file mode 100644 index 0000000..a52cccf --- /dev/null +++ b/server/src/db/query/mod.rs @@ -0,0 +1,7 @@ +mod package; +mod repo; + +pub use package::PackageQuery; +pub use repo::RepoQuery; + +type Result = std::result::Result; diff --git a/server/src/db/query/package.rs b/server/src/db/query/package.rs new file mode 100644 index 0000000..eed5ead --- /dev/null +++ b/server/src/db/query/package.rs @@ -0,0 +1,213 @@ +use sea_orm::*; + +use crate::db::*; + +#[derive(Clone, Debug)] +pub struct PackageQuery { + conn: DatabaseConnection, +} + +impl PackageQuery { + pub fn new(conn: DatabaseConnection) -> Self { + Self { conn } + } + + pub async fn page( + &self, + per_page: u64, + page: u64, + ) -> super::Result<(u64, Vec)> { + let paginator = Package::find() + .order_by_asc(package::Column::Id) + .paginate(&self.conn, per_page); + let packages = paginator.fetch_page(page).await?; + let total_pages = paginator.num_pages().await?; + + Ok((total_pages, packages)) + } + + pub async fn by_id(&self, id: i32) -> Result> { + package::Entity::find_by_id(id).one(&self.conn).await + } + + pub async fn by_fields( + &self, + repo_id: i32, + name: &str, + version: Option<&str>, + arch: &str, + ) -> Result> { + let mut query = Package::find() + .filter(package::Column::RepoId.eq(repo_id)) + .filter(package::Column::Name.eq(name)) + .filter(package::Column::Arch.eq(arch)); + + if let Some(version) = version { + query = query.filter(package::Column::Version.eq(version)); + } + + query.one(&self.conn).await + } + + pub async fn delete_with_arch(&self, repo_id: i32, arch: &str) -> Result { + Package::delete_many() + .filter(package::Column::RepoId.eq(repo_id)) + .filter(package::Column::Arch.eq(arch)) + .exec(&self.conn) + .await + } + + pub async fn insert(&self, repo_id: i32, pkg: crate::repo::package::Package) -> Result<()> { + let info = pkg.info; + + let model = package::ActiveModel { + id: NotSet, + repo_id: Set(repo_id), + base: Set(info.base), + name: Set(info.name), + version: Set(info.version), + arch: Set(info.arch), + size: Set(info.size), + c_size: Set(info.csize), + description: Set(info.description), + url: Set(info.url), + build_date: Set(info.build_date), + packager: Set(info.packager), + pgp_sig: Set(info.pgpsig), + pgp_sig_size: Set(info.pgpsigsize), + sha256_sum: Set(info.sha256sum), + }; + + let pkg_entry = model.insert(&self.conn).await?; + + // Insert all the related tables + PackageLicense::insert_many(info.licenses.iter().map(|s| package_license::ActiveModel { + package_id: Set(pkg_entry.id), + value: Set(s.to_string()), + })) + .on_empty_do_nothing() + .exec(&self.conn) + .await?; + + PackageGroup::insert_many(info.groups.iter().map(|s| package_group::ActiveModel { + package_id: Set(pkg_entry.id), + value: Set(s.to_string()), + })) + .on_empty_do_nothing() + .exec(&self.conn) + .await?; + + PackageReplaces::insert_many(info.replaces.iter().map(|s| package_replaces::ActiveModel { + package_id: Set(pkg_entry.id), + value: Set(s.to_string()), + })) + .on_empty_do_nothing() + .exec(&self.conn) + .await?; + + PackageConflicts::insert_many(info.conflicts.iter().map(|s| { + package_conflicts::ActiveModel { + package_id: Set(pkg_entry.id), + value: Set(s.to_string()), + } + })) + .on_empty_do_nothing() + .exec(&self.conn) + .await?; + + PackageProvides::insert_many(info.provides.iter().map(|s| package_provides::ActiveModel { + package_id: Set(pkg_entry.id), + value: Set(s.to_string()), + })) + .on_empty_do_nothing() + .exec(&self.conn) + .await?; + + PackageFile::insert_many(pkg.files.iter().map(|s| package_file::ActiveModel { + package_id: Set(pkg_entry.id), + value: Set(s.display().to_string()), + })) + .on_empty_do_nothing() + .exec(&self.conn) + .await?; + + let deps = info + .depends + .iter() + .map(|d| (PackageDepend::Depend, d)) + .chain(info.makedepends.iter().map(|d| (PackageDepend::Make, d))) + .chain(info.checkdepends.iter().map(|d| (PackageDepend::Check, d))) + .chain(info.optdepends.iter().map(|d| (PackageDepend::Opt, d))) + .map(|(t, s)| package_depends::ActiveModel { + package_id: Set(pkg_entry.id), + r#type: Set(t), + value: Set(s.to_string()), + }); + + PackageDepends::insert_many(deps) + .on_empty_do_nothing() + .exec(&self.conn) + .await?; + + Ok(()) + } + + pub async fn full(&self, id: i32) -> Result> { + if let Some(entry) = self.by_id(id).await? { + let licenses = entry + .find_related(PackageLicense) + .all(&self.conn) + .await? + .into_iter() + .map(|e| e.value) + .collect(); + let groups = entry + .find_related(PackageGroup) + .all(&self.conn) + .await? + .into_iter() + .map(|e| e.value) + .collect(); + let replaces = entry + .find_related(PackageReplaces) + .all(&self.conn) + .await? + .into_iter() + .map(|e| e.value) + .collect(); + let provides = entry + .find_related(PackageProvides) + .all(&self.conn) + .await? + .into_iter() + .map(|e| e.value) + .collect(); + let depends = entry + .find_related(PackageDepends) + .all(&self.conn) + .await? + .into_iter() + .map(|e| (e.r#type, e.value)) + .collect(); + let files = entry + .find_related(PackageFile) + .all(&self.conn) + .await? + .into_iter() + .map(|e| e.value) + .collect(); + + Ok(Some(FullPackage { + entry, + licenses, + groups, + replaces, + provides, + depends, + files, + })) + } else { + Ok(None) + } + } +} diff --git a/server/src/db/query/repo.rs b/server/src/db/query/repo.rs new file mode 100644 index 0000000..5e54fdc --- /dev/null +++ b/server/src/db/query/repo.rs @@ -0,0 +1,49 @@ +use sea_orm::*; + +use crate::db::*; + +#[derive(Clone, Debug)] +pub struct RepoQuery { + conn: DatabaseConnection, +} + +impl RepoQuery { + pub fn new(conn: DatabaseConnection) -> Self { + Self { conn } + } + + pub async fn page(&self, per_page: u64, page: u64) -> Result<(u64, Vec)> { + let paginator = Repo::find() + .order_by_asc(repo::Column::Id) + .paginate(&self.conn, per_page); + let repos = paginator.fetch_page(page).await?; + let total_pages = paginator.num_pages().await?; + + Ok((total_pages, repos)) + } + + pub async fn by_id(&self, id: i32) -> Result> { + repo::Entity::find_by_id(id).one(&self.conn).await + } + + pub async fn by_name(&self, name: &str) -> Result> { + Repo::find() + .filter(repo::Column::Name.eq(name)) + .one(&self.conn) + .await + } + + pub async fn insert( + &self, + name: &str, + description: Option<&str>, + ) -> Result> { + let model = repo::ActiveModel { + id: NotSet, + name: Set(String::from(name)), + description: Set(description.map(String::from)), + }; + + Repo::insert(model).exec(&self.conn).await + } +} diff --git a/server/src/repo/mod.rs b/server/src/repo/mod.rs index ba8fbf7..21acf81 100644 --- a/server/src/repo/mod.rs +++ b/server/src/repo/mod.rs @@ -126,25 +126,26 @@ async fn post_package_archive( tracing::info!("Added '{}' to repository '{}'", pkg.file_name(), repo); // Query the repo for its ID, or create it if it does not already exist - let res = global.db.repo_by_name(&repo).await?; + let res = global.db.repo.by_name(&repo).await?; let repo_id = if let Some(repo_entity) = res { repo_entity.id } else { - global.db.insert_repo(&repo, None).await?.last_insert_id + global.db.repo.insert(&repo, None).await?.last_insert_id }; // If the package already exists in the database, we remove it first let res = global .db - .package_by_fields(repo_id, &pkg.info.name, None, &pkg.info.arch) + .pkg + .by_fields(repo_id, &pkg.info.name, None, &pkg.info.arch) .await?; if let Some(entry) = res { entry.delete(&global.db).await?; } - global.db.insert_package(repo_id, pkg).await?; + global.db.pkg.insert(repo_id, pkg).await?; Ok(()) } @@ -169,7 +170,7 @@ async fn delete_repo( .await??; if repo_removed { - let res = global.db.repo_by_name(&repo).await?; + let res = global.db.repo.by_name(&repo).await?; if let Some(repo_entry) = res { repo_entry.delete(&global.db).await?; @@ -200,13 +201,10 @@ async fn delete_arch_repo( .await??; if repo_removed { - let res = global.db.repo_by_name(&repo).await?; + let res = global.db.repo.by_name(&repo).await?; if let Some(repo_entry) = res { - global - .db - .delete_packages_with_arch(repo_entry.id, &arch) - .await?; + global.db.pkg.delete_with_arch(repo_entry.id, &arch).await?; } tracing::info!("Removed architecture '{}' from repository '{}'", arch, repo); @@ -229,12 +227,13 @@ async fn delete_package( .await??; if let Some((name, version, release, arch)) = res { - let res = global.db.repo_by_name(&repo).await?; + let res = global.db.repo.by_name(&repo).await?; if let Some(repo_entry) = res { let res = global .db - .package_by_fields( + .pkg + .by_fields( repo_entry.id, &name, Some(&format!("{}-{}", version, release)),