record all opened/closed history, change sort order to DESC

This commit is contained in:
yggverse 2025-03-08 21:15:35 +02:00
parent 268af30830
commit f90378a911
2 changed files with 11 additions and 11 deletions

View file

@ -5,8 +5,8 @@ pub struct Item {
pub id: i64, pub id: i64,
pub request: GString, pub request: GString,
pub created: DateTime, pub created: DateTime,
pub opened: DateTime, pub opened: Vec<DateTime>,
pub closed: Option<DateTime>, pub closed: Vec<DateTime>,
} }
impl Item { impl Item {
@ -17,19 +17,19 @@ impl Item {
id, id,
request, request,
created: now(), created: now(),
opened: now(), opened: vec![now()],
closed: None, closed: vec![],
} }
} }
// Actions // Actions
pub fn open(&mut self) { pub fn open(&mut self) {
self.opened = now() self.opened.push(now())
} }
pub fn close(&mut self) { pub fn close(&mut self) {
self.closed = Some(now()) self.closed.push(now())
} }
} }

View file

@ -42,14 +42,14 @@ impl Memory {
// Getters // Getters
/// Get recent Items vector sorted by `closed` ASC /// Get recent Items vector sorted by `closed` DESC
pub fn recently_closed(&self, limit: Option<usize>) -> Vec<Item> { pub fn recently_closed(&self, limit: Option<usize>) -> Vec<Item> {
let mut recent: Vec<Item> = Vec::new(); let mut recent: Vec<Item> = Vec::new();
for (i, item) in self for (i, item) in self
.0 .0
.iter() .iter()
.filter(|x| x.closed.is_some()) .filter(|x| !x.closed.is_empty())
.sorted_by(|a, b| Ord::cmp(&a.closed, &b.closed)) .sorted_by(|a, b| Ord::cmp(&b.closed.last(), &a.closed.last()))
.enumerate() .enumerate()
{ {
if limit.is_some_and(|l| i > l) { if limit.is_some_and(|l| i > l) {
@ -60,13 +60,13 @@ impl Memory {
recent recent
} }
/// Get recent Items vector sorted by `opened` ASC /// Get recent Items vector sorted by `opened` DESC
pub fn recently_opened(&self, limit: Option<usize>) -> Vec<Item> { pub fn recently_opened(&self, limit: Option<usize>) -> Vec<Item> {
let mut recent: Vec<Item> = Vec::new(); let mut recent: Vec<Item> = Vec::new();
for (i, item) in self for (i, item) in self
.0 .0
.iter() .iter()
.sorted_by(|a, b| Ord::cmp(&a.opened, &b.opened)) .sorted_by(|a, b| Ord::cmp(&b.opened.last(), &a.opened.last()))
.enumerate() .enumerate()
{ {
if limit.is_some_and(|l| i > l) { if limit.is_some_and(|l| i > l) {