diff --git a/crates/http/src/main.rs b/crates/http/src/main.rs index 27035d9..f88b15e 100644 --- a/crates/http/src/main.rs +++ b/crates/http/src/main.rs @@ -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, db: &State) -> Result, 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 diff --git a/crates/mysql/src/lib.rs b/crates/mysql/src/lib.rs index 5e52fe5..32f8018 100644 --- a/crates/mysql/src/lib.rs +++ b/crates/mysql/src/lib.rs @@ -114,13 +114,13 @@ impl Mysql { Ok(total.unwrap_or(0)) } - pub fn contents(&self, limit: Option) -> Result, Error> { + pub fn contents(&self, sort: Sort, limit: Option) -> Result, 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;