Add visitor counter

master
kirbylife 2024-06-01 01:26:37 -06:00
parent b016fc5bab
commit bbba1ead64
3 changed files with 14 additions and 2 deletions

View File

@ -79,4 +79,14 @@ pub mod posts {
))
.get_result(&connection)
}
pub fn add_visit(post_id: i32) {
use crate::schema::posts::dsl::*;
let connection = establish_connection();
diesel::update(posts.filter(id.eq(post_id)))
.set(views.eq(views + 1))
.execute(&connection)
.unwrap();
}
}

View File

@ -78,6 +78,8 @@ fn show_post(title: String) -> Template {
match posts::get_post(id) {
Ok(mut post) => {
posts::add_visit(id);
let mut comrak_options = Options::default();
comrak_options.extension.table = true;
comrak_options.extension.autolink = true;

View File

@ -43,13 +43,13 @@
{% if loop.index is odd %}
<p class="colored">{{ post.id }}</p>
<p class="colored"><a href="/post/{{ post.title | slugify }}-{{ post.id }}">{{ post.title }}</a></p>
<p class="colored center">0</p>
<p class="colored center">{{ post.views }}</p>
<p class="colored center">{{ post.published }}</p>
<p class="colored"><a href="/admin/edit_post/{{ post.id }}">Editar</a></p>
{% else %}
<p>{{ post.id }}</p>
<p><a href="/post/{{ post.title | slugify }}-{{ post.id }}">{{ post.title }}</a></p>
<p class="center">0</p>
<p class="center">{{ post.views }}</p>
<p class="center">{{ post.published }}</p>
<p><a href="/admin/edit_post/{{ post.id }}">Editar</a></p>
{% endif %}