Skip to content

Commit 761801f

Browse files
committed
add last_insert and last_payload to stream list response
1 parent 4b78c0d commit 761801f

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

docs/openapi.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,17 @@
118118
},
119119
"updated": {
120120
"type": "string"
121+
},
122+
"last_insert": {
123+
"type": "string",
124+
"readOnly": true
125+
},
126+
"last_payload": {
127+
"type": "object",
128+
"readOnly": true,
129+
"additionalProperties": {
130+
"type": "string"
131+
}
121132
}
122133
}
123134
},

src/db.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ pub struct Stream {
1515
key: String,
1616
created: DateTime<Utc>,
1717
updated: DateTime<Utc>,
18+
last_insert: DateTime<Utc>,
19+
last_payload: Value,
1820
}
1921

2022
#[derive(Debug, Deserialize, Serialize)]
@@ -47,7 +49,19 @@ pub fn create_table(conn: Connection) -> Result<(), Error> {
4749
}
4850

4951
pub fn list_streams(conn: Connection) -> Result<Vec<Stream>, Error> {
50-
let mut stmt = conn.prepare("SELECT id, key, created, updated FROM streams")?;
52+
let mut stmt = conn.prepare(
53+
"SELECT
54+
streams.id,
55+
streams.key,
56+
streams.created,
57+
streams.updated,
58+
max(data.updated) as last_insert,
59+
max(data.payload) as last_payload
60+
FROM streams
61+
INNER JOIN data on data.stream_id = streams.id
62+
GROUP BY streams.id
63+
ORDER BY data.created DESC, data.id DESC",
64+
)?;
5165

5266
let results = stmt
5367
.query_map(NO_PARAMS, |row| {
@@ -56,6 +70,8 @@ pub fn list_streams(conn: Connection) -> Result<Vec<Stream>, Error> {
5670
key: row.get(1)?,
5771
created: row.get(2)?,
5872
updated: row.get(3)?,
73+
last_insert: row.get(4)?,
74+
last_payload: row.get(5)?,
5975
})
6076
})
6177
.and_then(|mapped_rows| Ok(mapped_rows.map(|row| row.unwrap()).collect::<Vec<Stream>>()))?;
@@ -70,7 +86,7 @@ pub fn list_data(conn: Connection, key: String) -> Result<Vec<Data>, Error> {
7086
FROM data
7187
INNER JOIN streams ON streams.id = data.stream_id
7288
WHERE streams.key = ?
73-
ORDER BY data.updated DESC, data.id DESC",
89+
ORDER BY data.created DESC, data.id DESC",
7490
)?;
7591

7692
let results = stmt

0 commit comments

Comments
 (0)