add favourite and unfavourite endpoints
This commit is contained in:
parent
922fa83772
commit
4c15d2ffc1
2 changed files with 31 additions and 1 deletions
|
@ -1,3 +1,5 @@
|
||||||
mod account;
|
mod account;
|
||||||
|
mod status;
|
||||||
|
|
||||||
pub use account::*;
|
pub use account::*;
|
||||||
|
pub use status::*;
|
||||||
|
|
|
@ -4,7 +4,7 @@ use serde::Deserialize;
|
||||||
use crate::core::*;
|
use crate::core::*;
|
||||||
use crate::ent;
|
use crate::ent;
|
||||||
use crate::middle::AuthData;
|
use crate::middle::AuthData;
|
||||||
use crate::model::NewNote;
|
use crate::model::{NewLike, NewNote};
|
||||||
use crate::state::AppState;
|
use crate::state::AppState;
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[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))
|
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, ¬e, &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, ¬e, &auth).await?;
|
||||||
|
Ok(HttpResponse::Ok().json(response))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn configure(cfg: &mut web::ServiceConfig) {
|
pub fn configure(cfg: &mut web::ServiceConfig) {
|
||||||
cfg.service(create_note)
|
cfg.service(create_note)
|
||||||
.service(get_by_id)
|
.service(get_by_id)
|
||||||
|
|
Loading…
Reference in a new issue