add: post comments
add: user follow api, user block api
This commit is contained in:
parent
559ce19932
commit
8580e34be2
18 changed files with 296 additions and 49 deletions
|
@ -26,19 +26,57 @@ impl DataManager {
|
|||
owner: get!(x->3(i64)) as usize,
|
||||
journal: get!(x->4(i64)) as usize,
|
||||
context: serde_json::from_str(&get!(x->5(String))).unwrap(),
|
||||
replying_to: if let Some(id) = get!(x->6(Option<i64>)) {
|
||||
Some(id as usize)
|
||||
} else {
|
||||
None
|
||||
},
|
||||
// likes
|
||||
likes: get!(x->6(i64)) as isize,
|
||||
dislikes: get!(x->7(i64)) as isize,
|
||||
likes: get!(x->7(i64)) as isize,
|
||||
dislikes: get!(x->8(i64)) as isize,
|
||||
// other counts
|
||||
comment_count: get!(x->9(i64)) as usize,
|
||||
}
|
||||
}
|
||||
|
||||
auto_method!(get_post_by_id()@get_post_from_row -> "SELECT * FROM entries WHERE id = $1" --name="journal entry" --returns=JournalPost --cache-key-tmpl="atto.entry:{}");
|
||||
auto_method!(get_post_by_id()@get_post_from_row -> "SELECT * FROM posts WHERE id = $1" --name="post" --returns=JournalPost --cache-key-tmpl="atto.post:{}");
|
||||
|
||||
/// Get all posts which are comments on the given post by ID.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `id` - the ID of the post the requested posts are commenting on
|
||||
/// * `batch` - the limit of posts in each page
|
||||
/// * `page` - the page number
|
||||
pub async fn get_post_comments(
|
||||
&self,
|
||||
id: usize,
|
||||
batch: usize,
|
||||
page: usize,
|
||||
) -> Result<JournalPost> {
|
||||
let conn = match self.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
||||
let res = query_row!(
|
||||
&conn,
|
||||
"SELECT * FROM posts WHERE replying_to = $1 LIMIT $2 OFFSET $3",
|
||||
&[&(id as i64), &(batch as i64), &((page * batch) as i64)],
|
||||
|x| { Ok(Self::get_post_from_row(x)) }
|
||||
);
|
||||
|
||||
if res.is_err() {
|
||||
return Err(Error::GeneralNotFound("post".to_string()));
|
||||
}
|
||||
|
||||
Ok(res.unwrap())
|
||||
}
|
||||
|
||||
/// Create a new journal entry in the database.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `data` - a mock [`JournalEntry`] object to insert
|
||||
pub async fn create_entry(&self, data: JournalPost) -> Result<()> {
|
||||
pub async fn create_post(&self, data: JournalPost) -> Result<()> {
|
||||
// check values
|
||||
if data.content.len() < 2 {
|
||||
return Err(Error::DataTooShort("content".to_string()));
|
||||
|
@ -77,14 +115,22 @@ impl DataManager {
|
|||
|
||||
let res = execute!(
|
||||
&conn,
|
||||
"INSERT INTO entries VALUES ($1, $2, $3, $4, $5",
|
||||
"INSERT INTO posts VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)",
|
||||
&[
|
||||
&data.id.to_string().as_str(),
|
||||
&data.created.to_string().as_str(),
|
||||
&data.content.as_str(),
|
||||
&data.owner.to_string().as_str(),
|
||||
&data.journal.to_string().as_str(),
|
||||
&serde_json::to_string(&data.context).unwrap().as_str(),
|
||||
&Some(data.id.to_string()),
|
||||
&Some(data.created.to_string()),
|
||||
&Some(data.content),
|
||||
&Some(data.owner.to_string()),
|
||||
&Some(data.journal.to_string()),
|
||||
&Some(serde_json::to_string(&data.context).unwrap()),
|
||||
&if let Some(id) = data.replying_to {
|
||||
Some(id.to_string())
|
||||
} else {
|
||||
None
|
||||
},
|
||||
&Some(0.to_string()),
|
||||
&Some(0.to_string()),
|
||||
&Some(0.to_string())
|
||||
]
|
||||
);
|
||||
|
||||
|
@ -92,15 +138,24 @@ impl DataManager {
|
|||
return Err(Error::DatabaseError(e.to_string()));
|
||||
}
|
||||
|
||||
// incr comment count
|
||||
if let Some(id) = data.replying_to {
|
||||
self.incr_post_comments(id).await.unwrap();
|
||||
}
|
||||
|
||||
// return
|
||||
Ok(())
|
||||
}
|
||||
|
||||
auto_method!(delete_entry()@get_post_by_id:MANAGE_JOURNAL_ENTRIES -> "DELETE FROM entries WHERE id = $1" --cache-key-tmpl="atto.entry:{}");
|
||||
auto_method!(update_post_content(String)@get_post_by_id:MANAGE_JOURNAL_ENTRIES -> "UPDATE entries SET content = $1 WHERE id = $2" --cache-key-tmpl="atto.entry:{}");
|
||||
auto_method!(update_post_context(JournalPostContext)@get_post_by_id:MANAGE_JOURNAL_ENTRIES -> "UPDATE entries SET context = $1 WHERE id = $2" --serde --cache-key-tmpl="atto.entry:{}");
|
||||
auto_method!(delete_post()@get_post_by_id:MANAGE_JOURNAL_ENTRIES -> "DELETE FROM posts WHERE id = $1" --cache-key-tmpl="atto.post:{}");
|
||||
auto_method!(update_post_content(String)@get_post_by_id:MANAGE_JOURNAL_ENTRIES -> "UPDATE posts SET content = $1 WHERE id = $2" --cache-key-tmpl="atto.post:{}");
|
||||
auto_method!(update_post_context(JournalPostContext)@get_post_by_id:MANAGE_JOURNAL_ENTRIES -> "UPDATE posts SET context = $1 WHERE id = $2" --serde --cache-key-tmpl="atto.post:{}");
|
||||
|
||||
auto_method!(incr_post_likes() -> "UPDATE entries SET likes = likes + 1 WHERE id = $1" --cache-key-tmpl="atto.entry:{}" --incr);
|
||||
auto_method!(incr_post_dislikes() -> "UPDATE entries SET likes = dislikes + 1 WHERE id = $1" --cache-key-tmpl="atto.entry:{}" --incr);
|
||||
auto_method!(decr_post_likes() -> "UPDATE entries SET likes = likes - 1 WHERE id = $1" --cache-key-tmpl="atto.entry:{}" --decr);
|
||||
auto_method!(decr_post_dislikes() -> "UPDATE entries SET likes = dislikes - 1 WHERE id = $1" --cache-key-tmpl="atto.entry:{}" --decr);
|
||||
auto_method!(incr_post_likes() -> "UPDATE posts SET likes = likes + 1 WHERE id = $1" --cache-key-tmpl="atto.post:{}" --incr);
|
||||
auto_method!(incr_post_dislikes() -> "UPDATE posts SET likes = dislikes + 1 WHERE id = $1" --cache-key-tmpl="atto.post:{}" --incr);
|
||||
auto_method!(decr_post_likes() -> "UPDATE posts SET likes = likes - 1 WHERE id = $1" --cache-key-tmpl="atto.post:{}" --decr);
|
||||
auto_method!(decr_post_dislikes() -> "UPDATE posts SET likes = dislikes - 1 WHERE id = $1" --cache-key-tmpl="atto.post:{}" --decr);
|
||||
|
||||
auto_method!(incr_post_comments() -> "UPDATE posts SET comment_count = comment_count + 1 WHERE id = $1" --cache-key-tmpl="atto.post:{}" --incr);
|
||||
auto_method!(decr_post_comments() -> "UPDATE posts SET comment_count = comment_count - 1 WHERE id = $1" --cache-key-tmpl="atto.post:{}" --decr);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue