This commit is contained in:
quantulr
2023-11-14 11:15:49 +08:00
parent bd1bb4952a
commit e0fe8f6a09
7 changed files with 55 additions and 15 deletions

View File

@ -27,6 +27,19 @@ pub struct Model {
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
pub enum Relation {
#[sea_orm(
belongs_to = "super::la_article_category::Entity",
from = "Column::Cid",
to = "super::la_article_category::Column::Id",
)]
LaArticleCategory
}
impl Related<super::la_article_category::Entity> for Entity {
fn to() -> RelationDef {
Relation::LaArticleCategory.def()
}
}
impl ActiveModelBehavior for ActiveModel {}

View File

@ -17,6 +17,15 @@ pub struct Model {
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
pub enum Relation {
#[sea_orm(has_many = "super::la_article::Entity")]
LaArticle
}
impl Related<super::la_article::Entity> for Entity {
fn to() -> RelationDef {
Relation::LaArticle.def()
}
}
impl ActiveModelBehavior for ActiveModel {}

View File

@ -1,6 +1,7 @@
use std::sync::Arc;
use crate::entity::la_article;
use crate::entity::la_article_category;
use crate::params::article::{ArticleDetailParams, ArticleListParams};
use crate::response::article::{ArticleDetail, ArticleListItem, SiblingArticle};
use crate::response::common::{BaseResponse, PageResponse};
@ -31,7 +32,15 @@ impl ArticleService {
let page_size = params.page_size.unwrap_or(20);
let articles_selection = la_article::Entity::find()
.filter(la_article::Column::IsDelete.ne(1))
.filter(la_article::Column::IsShow.ne(0));
.filter(la_article::Column::IsShow.ne(0))
.apply_if(Some(params.cid), |mut query, v| {
match v {
Some(cid) => {
query.filter(la_article::Column::Cid.eq(cid))
}
None => { query }
}
});
let count = match articles_selection.clone().count(&self.state.db_conn).await {
Ok(total) => total,
Err(_) => 0,
@ -118,13 +127,13 @@ impl ArticleService {
}
};
match la_article::Entity::find_by_id(id)
match la_article::Entity::find_by_id(id).find_also_related(la_article_category::Entity)
.filter(la_article::Column::IsDelete.eq(0))
.filter(la_article::Column::IsShow.eq(1))
.one(&self.state.db_conn)
.await
{
Ok(Some(article)) => {
Ok(Some((article, category))) => {
// 上一篇文章
let prev_result = la_article::Entity::find()
.filter(la_article::Column::IsDelete.eq(0))
@ -159,7 +168,10 @@ impl ArticleService {
let article_detail = ArticleDetail {
id: article.id,
cid: Option::from(article.cid),
category: None,
category: match category {
Some(category) => Some(category.name),
None => None
},
title: article.title,
intro: Option::from(article.intro),
summary: article.summary,

View File

@ -1 +1,2 @@
pub mod app;
pub mod article;

5
src/state/article.rs Normal file
View File

@ -0,0 +1,5 @@
use crate::service::artile::ArticleService;
pub struct ArticleState {
pub article_service: ArticleService
}