List favorites newest first

GET /image/favorites had no ORDER BY, so SQLite returned rows in index
order (by path). Order by id descending so the app's Favorites view
(FileViewer #73) shows what was just added on top.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Cameron CordesandClaude Opus 5.5 committed 2026-11-02 08:49:33 -05:00
1 parent 4d9e62d05d
commit ee921e7a73
1 file changed
+29
+29
View File
@@ -364,8 +364,10 @@ impl FavoriteDao for SqliteFavoriteDao {
fn get_favorites(&mut self, user_id: i32) -> Result<Vec<Favorite>, DbError> {
use schema::favorites::dsl::*;
// Newest first, so a Favorites view lists what was just added on top.
favorites
.filter(userid.eq(user_id))
.order(id.desc())
.load::<Favorite>(self.connection.lock().unwrap().deref_mut())
.map_err(|e| DbError::log(DbErrorKind::QueryError, e))
}
@@ -3456,3 +3458,30 @@ mod exif_dao_tests {
);
}
}
#[cfg(test)]
mod favorite_dao_tests {
use super::*;
use crate::database::test::in_memory_db_connection;
#[test]
fn favorites_list_newest_first() {
let mut dao =
SqliteFavoriteDao::from_shared(Arc::new(Mutex::new(in_memory_db_connection())));
for p in ["b.jpg", "c.jpg", "a.jpg"] {
dao.add_favorite(1, p).unwrap();
}
dao.add_favorite(2, "z.jpg").unwrap();
let paths: Vec<String> = dao
.get_favorites(1)
.unwrap()
.into_iter()
.map(|f| f.path)
.collect();
assert_eq!(
paths,
["a.jpg", "c.jpg", "b.jpg"],
"insertion order, newest first; per user"
);
}
}