summaryrefslogtreecommitdiff
path: root/src/types/like.rs
diff options
context:
space:
mode:
authorTyler Murphy <tylermurphy534@gmail.com>2023-02-12 14:11:50 -0500
committerTyler Murphy <tylermurphy534@gmail.com>2023-02-12 14:11:50 -0500
commit3d71da490947aacc52a3b77efdc13d5f0458c57f (patch)
tree8047eb6966655cffc772cbde4d73982fb7064a28 /src/types/like.rs
parentdocs is ssr'd (diff)
downloadxssbook-3d71da490947aacc52a3b77efdc13d5f0458c57f.tar.gz
xssbook-3d71da490947aacc52a3b77efdc13d5f0458c57f.tar.bz2
xssbook-3d71da490947aacc52a3b77efdc13d5f0458c57f.zip
refactor
Diffstat (limited to 'src/types/like.rs')
-rw-r--r--src/types/like.rs48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/types/like.rs b/src/types/like.rs
new file mode 100644
index 0000000..bf10b2d
--- /dev/null
+++ b/src/types/like.rs
@@ -0,0 +1,48 @@
+use serde::Serialize;
+use tracing::instrument;
+
+use crate::database;
+use crate::types::http::{ResponseCode, Result};
+
+#[derive(Serialize)]
+pub struct Like {
+ pub user_id: u64,
+ pub post_id: u64
+}
+
+impl Like {
+ #[instrument()]
+ pub fn add_liked(user_id: u64, post_id: u64) -> Result<()> {
+
+ let Ok(liked) = database::likes::add_liked(user_id, post_id) else {
+ return Err(ResponseCode::BadRequest.text("Failed to add like status"))
+ };
+
+ if !liked {
+ return Err(ResponseCode::InternalServerError.text("Failed to add like status"));
+ }
+
+ Ok(())
+ }
+
+ #[instrument()]
+ pub fn remove_liked(user_id: u64, post_id: u64) -> Result<()> {
+ let Ok(liked) = database::likes::remove_liked(user_id, post_id) else {
+ return Err(ResponseCode::BadRequest.text("Failed to remove like status"))
+ };
+
+ if !liked {
+ return Err(ResponseCode::InternalServerError.text("Failed to remove like status"));
+ }
+
+ Ok(())
+ }
+
+ #[instrument()]
+ pub fn reterieve_all() -> Result<Vec<Self>> {
+ let Ok(likes) = database::likes::get_all_likes() else {
+ return Err(ResponseCode::InternalServerError.text("Failed to fetch likes"))
+ };
+ Ok(likes)
+ }
+}