Add types and insert functionality to dao
Signed-off-by: trivernis <trivernis@protonmail.com>feature/tag-implications
parent
3bc2b046ea
commit
d0dfa2b638
@ -0,0 +1,71 @@
|
||||
use sea_orm_migration::prelude::*;
|
||||
|
||||
pub struct Migration;
|
||||
|
||||
impl MigrationName for Migration {
|
||||
fn name(&self) -> &str {
|
||||
"m20220611_122851_tag_implications"
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl MigrationTrait for Migration {
|
||||
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager.create_table(create_tag_impliations_table()).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager
|
||||
.drop_table(Table::drop().table(TagImplications::Table).to_owned())
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Iden)]
|
||||
enum TagImplications {
|
||||
Table,
|
||||
TagId,
|
||||
ImpliedTagId,
|
||||
}
|
||||
|
||||
#[derive(Iden)]
|
||||
enum Tags {
|
||||
Table,
|
||||
Id,
|
||||
}
|
||||
|
||||
fn create_tag_impliations_table() -> TableCreateStatement {
|
||||
Table::create()
|
||||
.if_not_exists()
|
||||
.table(TagImplications::Table)
|
||||
.col(
|
||||
ColumnDef::new(TagImplications::TagId)
|
||||
.big_integer()
|
||||
.not_null(),
|
||||
)
|
||||
.col(
|
||||
ColumnDef::new(TagImplications::ImpliedTagId)
|
||||
.big_integer()
|
||||
.not_null(),
|
||||
)
|
||||
.primary_key(
|
||||
Index::create()
|
||||
.col(TagImplications::TagId)
|
||||
.col(TagImplications::ImpliedTagId),
|
||||
)
|
||||
.foreign_key(
|
||||
ForeignKey::create()
|
||||
.from(TagImplications::Table, TagImplications::TagId)
|
||||
.to(Tags::Table, Tags::Id),
|
||||
)
|
||||
.foreign_key(
|
||||
ForeignKey::create()
|
||||
.from(TagImplications::Table, TagImplications::ImpliedTagId)
|
||||
.to(Tags::Table, Tags::Id),
|
||||
)
|
||||
.to_owned()
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
use sea_orm::prelude::*;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
|
||||
#[sea_orm(table_name = "tag_implications")]
|
||||
pub struct Model {
|
||||
#[sea_orm(primary_key)]
|
||||
pub tag_id: i64,
|
||||
pub implied_tag_id: i64,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {
|
||||
#[sea_orm(
|
||||
belongs_to = "super::tag::Entity",
|
||||
from = "Column::TagId",
|
||||
to = "super::tag::Column::Id"
|
||||
)]
|
||||
Tag,
|
||||
#[sea_orm(
|
||||
belongs_to = "super::tag::Entity",
|
||||
from = "Column::TagId",
|
||||
to = "super::tag::Column::Id"
|
||||
)]
|
||||
ImpliedTag,
|
||||
}
|
||||
|
||||
pub struct TagToImpliedLink;
|
||||
pub struct ImpliedToTagLink;
|
||||
|
||||
impl Linked for TagToImpliedLink {
|
||||
type FromEntity = super::tag::Entity;
|
||||
type ToEntity = super::tag::Entity;
|
||||
|
||||
fn link(&self) -> Vec<sea_orm::LinkDef> {
|
||||
vec![Relation::Tag.def().rev(), Relation::ImpliedTag.def()]
|
||||
}
|
||||
}
|
||||
|
||||
impl Linked for ImpliedToTagLink {
|
||||
type FromEntity = super::tag::Entity;
|
||||
type ToEntity = super::tag::Entity;
|
||||
|
||||
fn link(&self) -> Vec<sea_orm::LinkDef> {
|
||||
vec![Relation::ImpliedTag.def().rev(), Relation::Tag.def()]
|
||||
}
|
||||
}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
@ -0,0 +1,23 @@
|
||||
use crate::dto::AddTagImplicationDto;
|
||||
use mediarepo_core::error::RepoResult;
|
||||
use mediarepo_database::entities::tag_implication;
|
||||
|
||||
use super::TagDao;
|
||||
use sea_orm::{prelude::*, Set};
|
||||
|
||||
impl TagDao {
|
||||
pub async fn add_implications(&self, dtos: Vec<AddTagImplicationDto>) -> RepoResult<()> {
|
||||
let active_models = dtos
|
||||
.into_iter()
|
||||
.map(|dto| tag_implication::ActiveModel {
|
||||
tag_id: Set(dto.tag_id),
|
||||
implied_tag_id: Set(dto.implied_tag_id),
|
||||
})
|
||||
.collect::<Vec<tag_implication::ActiveModel>>();
|
||||
tag_implication::Entity::insert_many(active_models)
|
||||
.exec(&self.ctx.db)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue