Skip to content

Commit 2e8f150

Browse files
refactor: remove QueryPost object
1 parent 6f7070c commit 2e8f150

3 files changed

Lines changed: 30 additions & 53 deletions

File tree

src/post.rs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,6 @@ impl From<String> for ContentType {
2626
}
2727
}
2828

29-
#[derive(Debug, Clone)]
30-
pub struct QueryPost {
31-
pub id: String,
32-
pub content_type: ContentType,
33-
pub title: Option<String>,
34-
pub link: Option<String>,
35-
pub via: Option<String>,
36-
pub quote_author: Option<String>,
37-
pub date: String,
38-
pub content: String,
39-
pub commits: Option<String>,
40-
}
41-
42-
impl QueryPost {}
43-
4429
#[derive(Debug, Clone, Serialize)]
4530
pub struct Post {
4631
pub id: String,
@@ -51,5 +36,7 @@ pub struct Post {
5136
pub quote_author: Option<String>,
5237
pub date: String,
5338
pub content: String,
39+
#[serde(skip_serializing)]
40+
pub commits: Option<String>,
5441
pub real_commits: Option<Vec<Commit>>,
5542
}

src/rss.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{post::ContentType, post::QueryPost, AppState};
1+
use crate::{post::ContentType, post::Post, AppState};
22
use axum::response::IntoResponse;
33
use axum::{
44
extract::State,
@@ -13,8 +13,8 @@ struct RssEntry {
1313
guid: String,
1414
}
1515

16-
impl From<QueryPost> for RssEntry {
17-
fn from(post: QueryPost) -> Self {
16+
impl From<Post> for RssEntry {
17+
fn from(post: Post) -> Self {
1818
let full_url = format!("https://jonathansm.com/post/{}", post.id);
1919

2020
let (title, content) = match post.content_type {

src/services/post.rs

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::post::{Commit, ContentType, Post, QueryPost};
1+
use crate::post::{Commit, ContentType, Post};
22
use anyhow::{Context, Result};
33
use r2d2::Pool;
44
use r2d2_sqlite::SqliteConnectionManager;
@@ -16,7 +16,7 @@ impl PostService {
1616
Self { pool }
1717
}
1818

19-
fn row_to_query_post(row: &rusqlite::Row) -> rusqlite::Result<QueryPost> {
19+
fn row_to_post(row: &rusqlite::Row) -> rusqlite::Result<Post> {
2020
let id: String = row.get("id")?;
2121
let content_type_str: String = row.get("content_type")?;
2222
let content_type = ContentType::from(content_type_str);
@@ -28,7 +28,7 @@ impl PostService {
2828
let content: String = row.get("content")?;
2929
let commits: Option<String> = row.get("commits")?;
3030

31-
Ok(QueryPost {
31+
Ok(Post {
3232
id,
3333
content_type,
3434
title,
@@ -38,6 +38,7 @@ impl PostService {
3838
date,
3939
content,
4040
commits,
41+
real_commits: None,
4142
})
4243
}
4344

@@ -48,7 +49,7 @@ impl PostService {
4849
let mut stmt = conn.prepare(
4950
"SELECT * FROM posts WHERE content_type != 'special' ORDER BY date DESC LIMIT 5",
5051
)?;
51-
let iter = stmt.query_map([], Self::row_to_query_post)?;
52+
let iter = stmt.query_map([], Self::row_to_post)?;
5253
let mut result = Vec::new();
5354
for post in iter {
5455
result.push(post?);
@@ -71,7 +72,7 @@ impl PostService {
7172
let mut stmt = conn.prepare(
7273
"SELECT * FROM posts WHERE content_type != 'special' ORDER BY date DESC LIMIT ? OFFSET ?",
7374
)?;
74-
let iter = stmt.query_map(params![POSTS_PER_PAGE, offset], Self::row_to_query_post)?;
75+
let iter = stmt.query_map(params![POSTS_PER_PAGE, offset], Self::row_to_post)?;
7576
let mut result = Vec::new();
7677
for post in iter {
7778
result.push(post?);
@@ -110,7 +111,7 @@ impl PostService {
110111
let result = conn.query_row(
111112
"SELECT * FROM posts WHERE id = ? AND content_type != 'special'",
112113
[&id_clone],
113-
Self::row_to_query_post,
114+
Self::row_to_post,
114115
)?;
115116
anyhow::Result::<_>::Ok(result)
116117
})
@@ -129,7 +130,7 @@ impl PostService {
129130
let result = conn.query_row(
130131
"SELECT * FROM posts WHERE id = ? AND content_type = 'special'",
131132
[&id_clone],
132-
Self::row_to_query_post,
133+
Self::row_to_post,
133134
)?;
134135
anyhow::Result::<_>::Ok(result)
135136
})
@@ -140,14 +141,14 @@ impl PostService {
140141
self.convert_to_post(query).await
141142
}
142143

143-
pub async fn get_rss_entries(&self) -> Result<Vec<QueryPost>> {
144+
pub async fn get_rss_entries(&self) -> Result<Vec<Post>> {
144145
let pool = self.pool.clone();
145146
task::spawn_blocking(move || {
146147
let conn = pool.get()?;
147148
let mut stmt = conn.prepare(
148149
"SELECT * FROM posts WHERE content_type != 'special' ORDER BY date DESC LIMIT 20",
149150
)?;
150-
let iter = stmt.query_map([], Self::row_to_query_post)?;
151+
let iter = stmt.query_map([], Self::row_to_post)?;
151152
let mut result = Vec::new();
152153
for post in iter {
153154
result.push(post?);
@@ -159,14 +160,14 @@ impl PostService {
159160
.context("Failed to fetch RSS entries")
160161
}
161162

162-
async fn convert_to_post(&self, query: QueryPost) -> Result<Post> {
163-
self.bulk_convert_to_posts(vec![query])
163+
async fn convert_to_post(&self, post: Post) -> Result<Post> {
164+
self.bulk_convert_to_posts(vec![post])
164165
.await
165166
.map(|mut v| v.remove(0))
166167
}
167168

168-
async fn bulk_convert_to_posts(&self, query_posts: Vec<QueryPost>) -> Result<Vec<Post>> {
169-
let all_commit_ids: Vec<_> = query_posts
169+
async fn bulk_convert_to_posts(&self, mut posts: Vec<Post>) -> Result<Vec<Post>> {
170+
let all_commit_ids: Vec<_> = posts
170171
.iter()
171172
.filter_map(|post| post.commits.as_ref())
172173
.flat_map(|commits| commits.split_whitespace())
@@ -212,27 +213,16 @@ impl PostService {
212213
HashMap::new()
213214
};
214215

215-
Ok(query_posts
216-
.into_iter()
217-
.map(|post| {
218-
let real_commits = post.commits.as_ref().map(|ids| {
219-
ids.split_whitespace()
220-
.filter_map(|id| commits_map.get(id).cloned())
221-
.collect()
222-
});
223-
224-
Post {
225-
id: post.id,
226-
content_type: post.content_type,
227-
title: post.title,
228-
link: post.link,
229-
via: post.via,
230-
quote_author: post.quote_author,
231-
date: post.date,
232-
content: post.content,
233-
real_commits,
234-
}
235-
})
236-
.collect())
216+
for post in &mut posts {
217+
if let Some(ids) = &post.commits {
218+
let real_commits: Vec<Commit> = ids
219+
.split_whitespace()
220+
.filter_map(|id| commits_map.get(id).cloned())
221+
.collect();
222+
post.real_commits = Some(real_commits);
223+
}
224+
}
225+
226+
Ok(posts)
237227
}
238228
}

0 commit comments

Comments
 (0)