magnetar/src/api_v1/note.rs

32 lines
958 B
Rust

use axum::extract::{Path, Query, State};
use axum::{debug_handler, Json};
use std::sync::Arc;
use crate::model::processing::note::NoteModel;
use crate::model::PackingContext;
use magnetar_sdk::endpoints::note::{GetNoteById, NoteByIdReq};
use magnetar_sdk::endpoints::{Req, Res};
use crate::service::MagnetarService;
use crate::web::auth::MaybeUser;
use crate::web::{ApiError, ObjectNotFound};
#[debug_handler]
pub async fn handle_note(
Path(id): Path<String>,
Query(NoteByIdReq {
context,
attachments,
}): Query<Req<GetNoteById>>,
State(service): State<Arc<MagnetarService>>,
MaybeUser(self_user): MaybeUser,
) -> Result<Json<Res<GetNoteById>>, ApiError> {
let ctx = PackingContext::new(service, self_user.clone()).await?;
let note = NoteModel
.fetch_single(&ctx, self_user.as_deref(), &id, context, attachments)
.await?
.ok_or_else(|| ObjectNotFound(id))?;
Ok(Json(note.into()))
}