xssbook/src/types/post.rs

93 lines
2.6 KiB
Rust
Raw Normal View History

2023-01-28 16:52:32 +00:00
use core::fmt;
2023-01-26 22:29:16 +00:00
use std::collections::HashSet;
use serde::Serialize;
2023-01-28 16:52:32 +00:00
use tracing::instrument;
2023-01-26 22:29:16 +00:00
use crate::database;
2023-01-28 22:57:52 +00:00
use crate::types::http::{Result, ResponseCode};
2023-01-26 22:29:16 +00:00
#[derive(Serialize)]
pub struct Post {
pub post_id: u64,
pub user_id: u64,
pub content: String,
pub likes: HashSet<u64>,
pub comments: Vec<(u64, String)>,
pub date: u64
}
2023-01-28 16:52:32 +00:00
impl fmt::Debug for Post {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Post")
.field("post_id", &self.post_id)
.finish()
}
}
2023-01-26 22:29:16 +00:00
impl Post {
2023-01-28 16:52:32 +00:00
#[instrument()]
2023-01-26 22:29:16 +00:00
pub fn from_post_id(post_id: u64) -> Result<Self> {
let Ok(Some(post)) = database::posts::get_post(post_id) else {
return Err(ResponseCode::BadRequest.text("Post does not exist"))
2023-01-26 22:29:16 +00:00
};
Ok(post)
}
2023-01-28 16:52:32 +00:00
#[instrument()]
2023-01-26 22:29:16 +00:00
pub fn from_post_page(page: u64) -> Result<Vec<Self>> {
let Ok(posts) = database::posts::get_post_page(page) else {
return Err(ResponseCode::BadRequest.text("Failed to fetch posts"))
2023-01-26 22:29:16 +00:00
};
Ok(posts)
}
2023-01-28 16:52:32 +00:00
#[instrument()]
2023-01-26 22:29:16 +00:00
pub fn from_user_id(user_id: u64) -> Result<Vec<Self>> {
let Ok(posts) = database::posts::get_users_posts(user_id) else {
return Err(ResponseCode::BadRequest.text("Failed to fetch posts"))
2023-01-26 22:29:16 +00:00
};
Ok(posts)
}
2023-01-28 16:52:32 +00:00
#[instrument()]
2023-01-26 22:29:16 +00:00
pub fn new(user_id: u64, content: String) -> Result<Self> {
let Ok(post) = database::posts::add_post(user_id, &content) else {
2023-01-28 16:52:32 +00:00
tracing::error!("Failed to create post");
return Err(ResponseCode::InternalServerError.text("Failed to create post"))
2023-01-26 22:29:16 +00:00
};
Ok(post)
}
2023-01-28 16:52:32 +00:00
#[instrument()]
2023-01-26 22:29:16 +00:00
pub fn comment(&mut self, user_id: u64, content: String) -> Result<()> {
self.comments.push((user_id, content));
if database::posts::update_post(self.post_id, &self.likes, &self.comments).is_err() {
2023-01-28 16:52:32 +00:00
tracing::error!("Failed to comment on post");
return Err(ResponseCode::InternalServerError.text("Failed to comment on post"))
2023-01-26 22:29:16 +00:00
}
Ok(())
}
2023-01-28 16:52:32 +00:00
#[instrument()]
2023-01-26 22:29:16 +00:00
pub fn like(&mut self, user_id: u64, state: bool) -> Result<()> {
if state {
self.likes.insert(user_id);
} else {
self.likes.remove(&user_id);
}
if database::posts::update_post(self.post_id, &self.likes, &self.comments).is_err() {
2023-01-28 16:52:32 +00:00
tracing::error!("Failed to change like state on post");
return Err(ResponseCode::InternalServerError.text("Failed to change like state on post"))
2023-01-26 22:29:16 +00:00
}
Ok(())
}
}