feat: added distro routes and manager
This commit is contained in:
parent
5e1dfd22da
commit
58def483aa
11 changed files with 216 additions and 60 deletions
|
|
@ -13,6 +13,15 @@ pub struct Model {
|
|||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {}
|
||||
pub enum Relation {
|
||||
#[sea_orm(has_many = "super::repo::Entity")]
|
||||
Repo,
|
||||
}
|
||||
|
||||
impl Related<super::repo::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
Relation::Repo.def()
|
||||
}
|
||||
}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
||||
|
|
|
|||
|
|
@ -8,16 +8,31 @@ use serde::{Deserialize, Serialize};
|
|||
pub struct Model {
|
||||
#[sea_orm(primary_key)]
|
||||
pub id: i32,
|
||||
pub distro_id: i32,
|
||||
pub name: String,
|
||||
pub description: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {
|
||||
#[sea_orm(
|
||||
belongs_to = "super::distro::Entity",
|
||||
from = "Column::DistroId",
|
||||
to = "super::distro::Column::Id",
|
||||
on_update = "NoAction",
|
||||
on_delete = "Cascade"
|
||||
)]
|
||||
Distro,
|
||||
#[sea_orm(has_many = "super::package::Entity")]
|
||||
Package,
|
||||
}
|
||||
|
||||
impl Related<super::distro::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
Relation::Distro.def()
|
||||
}
|
||||
}
|
||||
|
||||
impl Related<super::package::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
Relation::Package.def()
|
||||
|
|
|
|||
|
|
@ -43,8 +43,16 @@ impl MigrationTrait for Migration {
|
|||
.auto_increment()
|
||||
.primary_key(),
|
||||
)
|
||||
.col(ColumnDef::new(Repo::DistroId).integer().not_null())
|
||||
.col(ColumnDef::new(Repo::Name).string().not_null().unique_key())
|
||||
.col(ColumnDef::new(Repo::Description).string())
|
||||
.foreign_key(
|
||||
ForeignKey::create()
|
||||
.name("fk-repo-distro_id")
|
||||
.from(Repo::Table, Repo::DistroId)
|
||||
.to(Distro::Table, Distro::Id)
|
||||
.on_delete(ForeignKeyAction::Cascade),
|
||||
)
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
|
@ -232,6 +240,7 @@ pub enum Distro {
|
|||
pub enum Repo {
|
||||
Table,
|
||||
Id,
|
||||
DistroId,
|
||||
Name,
|
||||
Description,
|
||||
}
|
||||
|
|
|
|||
47
server/src/db/query/distro.rs
Normal file
47
server/src/db/query/distro.rs
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
use crate::db::*;
|
||||
|
||||
use sea_orm::{sea_query::IntoCondition, *};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct Filter {
|
||||
name: Option<String>,
|
||||
}
|
||||
|
||||
impl IntoCondition for Filter {
|
||||
fn into_condition(self) -> Condition {
|
||||
Condition::all().add_option(
|
||||
self.name
|
||||
.map(|name| distro::Column::Name.like(format!("%{}%", name))),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn page(
|
||||
conn: &DbConn,
|
||||
per_page: u64,
|
||||
page: u64,
|
||||
filter: Filter,
|
||||
) -> Result<(u64, Vec<distro::Model>)> {
|
||||
let paginator = Distro::find()
|
||||
.filter(filter)
|
||||
.order_by_asc(distro::Column::Id)
|
||||
.paginate(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(conn: &DbConn, id: i32) -> Result<Option<distro::Model>> {
|
||||
distro::Entity::find_by_id(id).one(conn).await
|
||||
}
|
||||
|
||||
pub async fn insert(conn: &DbConn, name: &str, description: Option<&str>) -> Result<distro::Model> {
|
||||
let model = distro::ActiveModel {
|
||||
id: NotSet,
|
||||
name: Set(String::from(name)),
|
||||
description: Set(description.map(String::from)),
|
||||
};
|
||||
|
||||
model.insert(conn).await
|
||||
}
|
||||
|
|
@ -1,3 +1,4 @@
|
|||
pub mod distro;
|
||||
pub mod package;
|
||||
pub mod repo;
|
||||
|
||||
|
|
|
|||
|
|
@ -43,9 +43,15 @@ pub async fn by_name(conn: &DbConn, name: &str) -> Result<Option<repo::Model>> {
|
|||
.await
|
||||
}
|
||||
|
||||
pub async fn insert(conn: &DbConn, name: &str, description: Option<&str>) -> Result<repo::Model> {
|
||||
pub async fn insert(
|
||||
conn: &DbConn,
|
||||
distro_id: i32,
|
||||
name: &str,
|
||||
description: Option<&str>,
|
||||
) -> Result<repo::Model> {
|
||||
let model = repo::ActiveModel {
|
||||
id: NotSet,
|
||||
distro_id: Set(distro_id),
|
||||
name: Set(String::from(name)),
|
||||
description: Set(description.map(String::from)),
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue