fix: post page panic

This commit is contained in:
trisua 2025-04-12 22:42:57 -04:00
parent 7960484bf9
commit 9d091d5421
5 changed files with 26 additions and 2 deletions

View file

@ -231,7 +231,7 @@
<div class="card flex flex-col gap-4">
<!-- prettier-ignore -->
{% for post in replies %}
{{ components::post(post=post[0], owner=post[1], question=post[4], secondary=true, show_community=false) }}
{{ components::post(post=post[0], owner=post[1], question=post[3], secondary=true, show_community=false) }}
{% endfor %}
{{ components::pagination(page=page, items=replies|length) }}

View file

@ -592,6 +592,7 @@ impl DataManager {
auto_method!(incr_user_post_count()@get_user_by_id -> "UPDATE users SET post_count = post_count + 1 WHERE id = $1" --cache-key-tmpl=cache_clear_user --incr);
auto_method!(decr_user_post_count()@get_user_by_id -> "UPDATE users SET post_count = post_count - 1 WHERE id = $1" --cache-key-tmpl=cache_clear_user --decr);
auto_method!(update_user_request_count(i64)@get_user_by_id -> "UPDATE users SET request_count = $1 WHERE id = $2" --cache-key-tmpl=cache_clear_user);
auto_method!(incr_user_request_count()@get_user_by_id -> "UPDATE users SET request_count = request_count + 1 WHERE id = $1" --cache-key-tmpl=cache_clear_user --incr);
auto_method!(decr_user_request_count()@get_user_by_id -> "UPDATE users SET request_count = request_count - 1 WHERE id = $1" --cache-key-tmpl=cache_clear_user --decr);
}

View file

@ -551,6 +551,27 @@ macro_rules! auto_method {
}
};
($name:ident($x:ty)@$select_fn:ident -> $query:literal --cache-key-tmpl=$cache_key_tmpl:ident) => {
pub async fn $name(&self, id: usize, x: $x) -> Result<()> {
let y = self.$select_fn(id).await?;
let conn = match self.connect().await {
Ok(c) => c,
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
};
let res = execute!(&conn, $query, params![&x, &(id as i64)]);
if let Err(e) = res {
return Err(Error::DatabaseError(e.to_string()));
}
self.$cache_key_tmpl(&y).await;
Ok(())
}
};
($name:ident($x:ty)@$select_fn:ident -> $query:literal --serde --cache-key-tmpl=$cache_key_tmpl:ident) => {
pub async fn $name(&self, id: usize, x: $x) -> Result<()> {
let y = self.$select_fn(id).await?;

View file

@ -164,6 +164,8 @@ impl DataManager {
}
}
self.update_user_request_count(user.id, 0).await?;
Ok(())
}
}

View file

@ -285,7 +285,7 @@ pub struct Question {
pub receiver: usize,
pub content: String,
/// The `is_global` flag allows any (authenticated) user to respond
/// to the question. Normally, ownly the `receiver` can do so.
/// to the question. Normally, only the `receiver` can do so.
///
/// If `is_global` is true, `receiver` should be 0 (and vice versa).
pub is_global: bool,