diff --git a/src/ent/mod.rs b/src/ent/mod.rs index 0924651..f63a453 100644 --- a/src/ent/mod.rs +++ b/src/ent/mod.rs @@ -1,3 +1,5 @@ mod account; +mod status; pub use account::*; +pub use status::*; diff --git a/src/route/api/v1/statuses.rs b/src/route/api/v1/statuses.rs index e7e8033..bdaf265 100644 --- a/src/route/api/v1/statuses.rs +++ b/src/route/api/v1/statuses.rs @@ -4,7 +4,7 @@ use serde::Deserialize; use crate::core::*; use crate::ent; use crate::middle::AuthData; -use crate::model::NewNote; +use crate::model::{NewLike, NewNote}; use crate::state::AppState; #[derive(Deserialize)] @@ -56,6 +56,34 @@ async fn del_by_id(path: web::Path, state: AppState, auth: AuthData) -> Resu Ok(HttpResponse::Ok().json(response)) } +#[post("/{id}/favourite")] +async fn add_favourite(path: web::Path, state: AppState, auth: AuthData) -> Result { + let note_id = path.into_inner(); + let auth_account = auth.require()?; + let note = state.repo.notes.by_id(note_id).await?; + if state.repo.likes.by_note_and_account(note_id, auth_account.id).await.is_err() { + state.repo.likes.create(Insane::from(NewLike { + iri: None, + note_id: note.id, + account_id: auth_account.id, + })).await?; + } + let response = ent::Status::from_model(&state, ¬e, &auth).await?; + Ok(HttpResponse::Ok().json(response)) +} + +#[post("/{id}/unfavourite")] +async fn del_favourite(path: web::Path, state: AppState, auth: AuthData) -> Result { + let note_id = path.into_inner(); + let auth_account = auth.require()?; + let note = state.repo.notes.by_id(note_id).await?; + if state.repo.likes.by_note_and_account(note_id, auth_account.id).await.is_ok() { + state.repo.likes.delete(note.id, auth_account.id).await?; + } + let response = ent::Status::from_model(&state, ¬e, &auth).await?; + Ok(HttpResponse::Ok().json(response)) +} + pub fn configure(cfg: &mut web::ServiceConfig) { cfg.service(create_note) .service(get_by_id)