Skip to content

Commit 894efbf

Browse files
feat(src): add tags
1 parent 40b35fc commit 894efbf

3 files changed

Lines changed: 40 additions & 9 deletions

File tree

src/post.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ pub struct Post {
3737
pub date: String,
3838
pub content: String,
3939
#[serde(skip_serializing)]
40-
pub commits: Option<String>,
40+
pub commits: Option<Vec<String>>,
41+
pub tags: Option<Vec<String>>,
4142
pub real_commits: Option<Vec<Commit>>,
4243
}

src/services/post.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ impl PostService {
1616
Self { pool }
1717
}
1818

19-
fn row_to_post(row: &rusqlite::Row) -> rusqlite::Result<Post> {
19+
pub 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);
@@ -26,7 +26,11 @@ impl PostService {
2626
let quote_author: Option<String> = row.get("quote_author")?;
2727
let date: String = row.get("date")?;
2828
let content: String = row.get("content")?;
29-
let commits: Option<String> = row.get("commits")?;
29+
let commits_str: Option<String> = row.get("commits")?;
30+
let commits = commits_str.and_then(|s| serde_json::from_str(&s).ok());
31+
32+
let tags_str: Option<String> = row.get("tags")?;
33+
let tags = tags_str.and_then(|s| serde_json::from_str(&s).ok());
3034

3135
Ok(Post {
3236
id,
@@ -38,6 +42,7 @@ impl PostService {
3842
date,
3943
content,
4044
commits,
45+
tags,
4146
real_commits: None,
4247
})
4348
}
@@ -166,12 +171,12 @@ impl PostService {
166171
.map(|mut v| v.remove(0))
167172
}
168173

169-
async fn bulk_convert_to_posts(&self, mut posts: Vec<Post>) -> Result<Vec<Post>> {
174+
pub async fn bulk_convert_to_posts(&self, mut posts: Vec<Post>) -> Result<Vec<Post>> {
170175
let all_commit_ids: Vec<_> = posts
171176
.iter()
172177
.filter_map(|post| post.commits.as_ref())
173-
.flat_map(|commits| commits.split_whitespace())
174-
.map(|s| s.to_owned())
178+
.flatten()
179+
.cloned()
175180
.collect();
176181

177182
let commits_map = if !all_commit_ids.is_empty() {
@@ -215,9 +220,10 @@ impl PostService {
215220

216221
for post in &mut posts {
217222
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())
223+
let real_commits = ids
224+
.iter()
225+
.filter_map(|id| commits_map.get(id))
226+
.cloned()
221227
.collect();
222228
post.real_commits = Some(real_commits);
223229
}

templates/macros.html

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ <h1><a href="/post/{{ post.id }}">{{ post.title }}</a></h1>
66
<h1>{{ post.title }}</h1>
77
{% endif %}
88
<p><small>Published on {{ post.date }}</small></p>
9+
{% if post.tags and post.tags | length > 0 %}
10+
<div class="tags">
11+
<span>Tags: </span>
12+
{% for tag in post.tags %}
13+
<a href="/tag/{{ tag }}">{{ tag }}</a>{% if not loop.last %}, {% endif %}
14+
{% endfor %}
15+
</div>
16+
{% endif %}
917
<div>{{ post.content|safe }}</div>
1018
</article>
1119
{% endmacro post %}
@@ -19,13 +27,29 @@ <h1>{{ post.title }}</h1>
1927
{% endif %} {% if post.via %} (<a href="{{ post.via }}">via</a>) {%
2028
endif %}
2129
<p><small>Published on {{ post.date }}</small></p>
30+
{% if post.tags and post.tags | length > 0 %}
31+
<div class="tags">
32+
<span>Tags: </span>
33+
{% for tag in post.tags %}
34+
<a href="/tag/{{ tag }}">{{ tag }}</a>{% if not loop.last %}, {% endif %}
35+
{% endfor %}
36+
</div>
37+
{% endif %}
2238
<div>{{ post.content|safe }}</div>
2339
</article>
2440
{% endmacro link %}
2541

2642
{% macro quote(post) %}
2743
<article>
2844
<p><small>Published on {{ post.date }}</small></p>
45+
{% if post.tags and post.tags | length > 0 %}
46+
<div class="tags">
47+
<span>Tags: </span>
48+
{% for tag in post.tags %}
49+
<a href="/tag/{{ tag }}">{{ tag }}</a>{% if not loop.last %}, {% endif %}
50+
{% endfor %}
51+
</div>
52+
{% endif %}
2953
<blockquote>
3054
{{ post.content }}
3155
<footer>{{ post.quote_author|display_some }}</footer>

0 commit comments

Comments
 (0)