Skip to content

Commit

Permalink
fix: fix article list handler and refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
zhanglun committed Dec 4, 2024
1 parent 747dfc2 commit 2246d09
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 116 deletions.
282 changes: 168 additions & 114 deletions src-tauri/src/feed/article.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,30 @@ impl Article {
/// get articles
pub fn get_article(filter: ArticleFilter) -> ArticleQueryResult {
let mut connection = establish_connection();
let mut query = diesel::sql_query("").into_boxed();
let mut query = diesel::sql_query("
SELECT
A.id, A.uuid,
A.feed_uuid,
C.title as feed_title,
C.link as feed_url,
C.logo as feed_logo,
A.link,
A.title,
A.feed_url,
A.description as description,
A.author,
A.pub_date,
A.create_date,
A.read_status,
A.starred
FROM
articles as A
LEFT JOIN
feeds as C
ON C.uuid = A.feed_uuid").into_boxed();
let mut limit = 12;
let mut conditions = vec![];
let mut params = vec![];

if let Some(channel_uuid) = filter.feed_uuid {
let mut relations = vec![];
Expand Down Expand Up @@ -152,127 +174,157 @@ impl Article {
channel_uuids.push(channel_uuid.clone());
}

let params = format!("?{}", ", ?".repeat(channel_uuids.len() - 1));
query = query.sql(format!(
"
SELECT
A.id, A.uuid,
A.feed_uuid,
C.title as feed_title,
C.link as feed_url,
C.logo as feed_logo,
A.link,
A.title,
A.feed_url,
A.description as description,
A.author,
A.pub_date,
A.create_date,
A.read_status,
A.starred
FROM
articles as A
LEFT JOIN
feeds as C
ON C.uuid = A.feed_uuid
WHERE C.uuid in ({}) AND A.uuid IS NOT NULL",
params
));
let in_params = format!("?{}", ", ?".repeat(channel_uuids.len() - 1));
conditions.push(format!("C.uuid in ({}) AND A.uuid IS NOT NULL", in_params));
// query = query.sql(format!(
// "
// SELECT
// A.id, A.uuid,
// A.feed_uuid,
// C.title as feed_title,
// C.link as feed_url,
// C.logo as feed_logo,
// A.link,
// A.title,
// A.feed_url,
// A.description as description,
// A.author,
// A.pub_date,
// A.create_date,
// A.read_status,
// A.starred
// FROM
// articles as A
// LEFT JOIN
// feeds as C
// ON C.uuid = A.feed_uuid
// WHERE C.uuid in ({}) AND A.uuid IS NOT NULL",
// params
// ));

// for uuid in channel_uuids {
// query = query.bind::<Text, _>(uuid);
// }

for uuid in channel_uuids {
query = query.bind::<Text, _>(uuid);
params.push(uuid);
}
} else if let Some(_is_today) = filter.is_today {
query = query.sql(
"
SELECT
A.id, A.uuid,
A.feed_uuid,
C.title as feed_title,
C.link as feed_url,
C.logo as feed_logo,
A.link,
A.title,
A.feed_url,
A.description as description,
A.author,
A.pub_date,
A.create_date,
A.read_status,
A.starred
FROM
articles as A
LEFT JOIN
feeds as C
ON C.uuid = A.feed_uuid
WHERE DATE(A.create_date) = DATE('now')",
);
} else if let Some(_is_starred) = filter.is_starred {
query = query.sql(
"
SELECT
A.id, A.uuid,
A.feed_uuid,
C.title as feed_title,
C.link as feed_url,
C.logo as feed_logo,
A.link,
A.title,
A.feed_url,
A.description as description,
A.author,
A.pub_date,
A.create_date,
A.read_status,
A.starred
FROM
articles as A
LEFT JOIN
feeds as C
ON C.uuid = A.feed_uuid
WHERE A.starred = 1
",
);
} else {
query = query.sql(
"
SELECT
A.id, A.uuid,
A.feed_uuid,
C.title as feed_title,
C.link as feed_url,
C.logo as feed_logo,
A.link,
A.title,
A.feed_url,
A.description as description,
A.author,
A.pub_date,
A.create_date,
A.read_status,
A.starred
FROM
articles as A
LEFT JOIN
feeds as C
ON C.uuid = A.feed_uuid ",
);
}

match filter.read_status {
Some(0) => {
1;
}
Some(status) => {
query = query
.sql(" AND A.read_status = ?")
.bind::<Integer, _>(status);
}
None => {
1;
if let Some(_is_today) = filter.is_today {
conditions.push("DATE(A.create_date) = DATE('now')".to_string());
}

if let Some(is_starred) = filter.is_starred {
conditions.push("A.starred = ?".to_string());
params.push(is_starred.to_string());
}

if let Some(read_status) = filter.read_status {
if read_status > 0 {
conditions.push("A.read_status = ?".to_string());
params.push(read_status.to_string());
}
}

if conditions.len() > 0 {
query = query.sql(format!(" WHERE {}", conditions.join(" AND ")));
}

// else if let Some(_is_today) = filter.is_today {
// query = query.sql(
// "
// SELECT
// A.id, A.uuid,
// A.feed_uuid,
// C.title as feed_title,
// C.link as feed_url,
// C.logo as feed_logo,
// A.link,
// A.title,
// A.feed_url,
// A.description as description,
// A.author,
// A.pub_date,
// A.create_date,
// A.read_status,
// A.starred
// FROM
// articles as A
// LEFT JOIN
// feeds as C
// ON C.uuid = A.feed_uuid
// WHERE DATE(A.create_date) = DATE('now')",
// );
// } else if let Some(_is_starred) = filter.is_starred {
// query = query.sql(
// "
// SELECT
// A.id, A.uuid,
// A.feed_uuid,
// C.title as feed_title,
// C.link as feed_url,
// C.logo as feed_logo,
// A.link,
// A.title,
// A.feed_url,
// A.description as description,
// A.author,
// A.pub_date,
// A.create_date,
// A.read_status,
// A.starred
// FROM
// articles as A
// LEFT JOIN
// feeds as C
// ON C.uuid = A.feed_uuid
// WHERE A.starred = 1
// ",
// );
// } else {
// query = query.sql(
// "
// SELECT
// A.id, A.uuid,
// A.feed_uuid,
// C.title as feed_title,
// C.link as feed_url,
// C.logo as feed_logo,
// A.link,
// A.title,
// A.feed_url,
// A.description as description,
// A.author,
// A.pub_date,
// A.create_date,
// A.read_status,
// A.starred
// FROM
// articles as A
// LEFT JOIN
// feeds as C
// ON C.uuid = A.feed_uuid ",
// );
// }

// match filter.read_status {
// Some(0) => {
// 1;
// }
// Some(status) => {
// query = query
// .sql(" WHERE A.read_status = ?")
// .bind::<Integer, _>(status);
// }
// None => {
// 1;
// }
// }

for param in params {
query = query.bind::<Text, _>(param);
}
query = query.sql(" ORDER BY A.pub_date DESC ");

if let Some(l) = filter.limit {
Expand All @@ -284,6 +336,8 @@ impl Article {
query = query.sql(" OFFSET ?").bind::<Integer, _>((c - 1) * limit);
}

log::info!("query: {:?}", diesel::debug_query(&query).to_string());

let result = query
.load::<ArticleQueryItem>(&mut connection)
.expect("Expect loading articles");
Expand Down
2 changes: 0 additions & 2 deletions src-tauri/src/server/handlers/article.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ pub struct StarParam {

#[post("/api/articles/{uuid}/read")]
pub async fn handle_update_article_read_status(uuid: web::Path<String>, body: web::Json<ReadParam>) -> Result<impl Responder> {
println!("%c Line:23 🍞 body {:?}", body);
let body = body.into_inner();
let res = feed::article::Article::update_article_read_status(uuid.to_string(), body.read_status);

Expand All @@ -32,7 +31,6 @@ pub async fn handle_update_article_read_status(uuid: web::Path<String>, body: we

#[post("/api/articles/{uuid}/star")]
pub async fn handle_update_article_star_status(uuid: web::Path<String>, body: web::Json<StarParam>) -> Result<impl Responder> {
println!("%c Line:23 🍞 body {:?}", body);
let body = body.into_inner();
let res = feed::article::Article::update_article_star_status(uuid.to_string(), body.starred);

Expand Down

0 comments on commit 2246d09

Please sign in to comment.