implement default sort ordering features

This commit is contained in:
yggverse 2026-01-08 00:22:07 +02:00
parent c324246cc6
commit 5d232f54c9
2 changed files with 19 additions and 5 deletions

View file

@ -11,7 +11,7 @@ use config::Config;
use feed::Feed;
use global::Global;
use meta::Meta;
use mysql::Mysql;
use mysql::{Mysql, Sort};
use rocket::{State, http::Status, response::content::RawXml, serde::Serialize};
use rocket_dyn_templates::{Template, context};
@ -63,7 +63,7 @@ fn index(
back: page.map(|p| uri!(index(search, if p > 2 { Some(p - 1) } else { None }))),
next: if page.unwrap_or(1) * global.list_limit >= total { None }
else { Some(uri!(index(search, Some(page.map_or(2, |p| p + 1))))) },
rows: db.contents(Some(global.list_limit)).map_err(|e| {
rows: db.contents(Sort::Desc, Some(global.list_limit)).map_err(|e| {
error!("Could not get contents: `{e}`");
Status::InternalServerError
})?
@ -123,7 +123,7 @@ fn rss(meta: &State<Meta>, db: &State<Mysql>) -> Result<RawXml<String>, Status>
1024, // @TODO
);
for c in db
.contents(Some(20)) // @TODO
.contents(Sort::Desc, Some(20)) // @TODO
.map_err(|e| {
error!("Could not load channel item contents: `{e}`");
Status::InternalServerError

View file

@ -114,13 +114,13 @@ impl Mysql {
Ok(total.unwrap_or(0))
}
pub fn contents(&self, limit: Option<usize>) -> Result<Vec<Content>, Error> {
pub fn contents(&self, sort: Sort, limit: Option<usize>) -> Result<Vec<Content>, Error> {
self.pool.get_conn()?.query(format!(
"SELECT `content_id`,
`channel_item_id`,
`source_id`,
`title`,
`description` FROM `content` LIMIT {}",
`description` FROM `content` ORDER BY `content_id` {sort} LIMIT {}",
limit.unwrap_or(DEFAULT_LIMIT)
))
}
@ -188,4 +188,18 @@ pub struct Content {
pub description: String,
}
pub enum Sort {
Asc,
Desc,
}
impl std::fmt::Display for Sort {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Self::Asc => write!(f, "ASC"),
Self::Desc => write!(f, "DESC"),
}
}
}
const DEFAULT_LIMIT: usize = 100;