add favourite and unfavourite endpoints

This commit is contained in:
anna 2022-12-27 04:37:38 +01:00
parent 922fa83772
commit 4c15d2ffc1
Signed by: fef
GPG key ID: EC22E476DC2D3D84
2 changed files with 31 additions and 1 deletions

View file

@ -1,3 +1,5 @@
mod account;
mod status;
pub use account::*;
pub use status::*;

View file

@ -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<Id>, state: AppState, auth: AuthData) -> Resu
Ok(HttpResponse::Ok().json(response))
}
#[post("/{id}/favourite")]
async fn add_favourite(path: web::Path<Id>, state: AppState, auth: AuthData) -> Result<HttpResponse> {
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, &note, &auth).await?;
Ok(HttpResponse::Ok().json(response))
}
#[post("/{id}/unfavourite")]
async fn del_favourite(path: web::Path<Id>, state: AppState, auth: AuthData) -> Result<HttpResponse> {
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, &note, &auth).await?;
Ok(HttpResponse::Ok().json(response))
}
pub fn configure(cfg: &mut web::ServiceConfig) {
cfg.service(create_note)
.service(get_by_id)