initial commit
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: I4a6b498153eccd5407510dd541b7f4816a6a6964
This commit is contained in:
commit
6a73d11c4b
124 changed files with 34856 additions and 0 deletions
101
crates/pinakes-server/src/routes/collections.rs
Normal file
101
crates/pinakes-server/src/routes/collections.rs
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
use axum::Json;
|
||||
use axum::extract::{Path, State};
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::dto::*;
|
||||
use crate::error::ApiError;
|
||||
use crate::state::AppState;
|
||||
|
||||
use pinakes_core::model::{CollectionKind, MediaId};
|
||||
|
||||
pub async fn create_collection(
|
||||
State(state): State<AppState>,
|
||||
Json(req): Json<CreateCollectionRequest>,
|
||||
) -> Result<Json<CollectionResponse>, ApiError> {
|
||||
if req.name.is_empty() || req.name.len() > 255 {
|
||||
return Err(ApiError(
|
||||
pinakes_core::error::PinakesError::InvalidOperation(
|
||||
"collection name must be 1-255 characters".into(),
|
||||
),
|
||||
));
|
||||
}
|
||||
if let Some(ref desc) = req.description
|
||||
&& desc.len() > 10_000
|
||||
{
|
||||
return Err(ApiError(
|
||||
pinakes_core::error::PinakesError::InvalidOperation(
|
||||
"description exceeds 10000 characters".into(),
|
||||
),
|
||||
));
|
||||
}
|
||||
let kind = match req.kind.as_str() {
|
||||
"virtual" => CollectionKind::Virtual,
|
||||
_ => CollectionKind::Manual,
|
||||
};
|
||||
let col = pinakes_core::collections::create_collection(
|
||||
&state.storage,
|
||||
&req.name,
|
||||
kind,
|
||||
req.description.as_deref(),
|
||||
req.filter_query.as_deref(),
|
||||
)
|
||||
.await?;
|
||||
Ok(Json(CollectionResponse::from(col)))
|
||||
}
|
||||
|
||||
pub async fn list_collections(
|
||||
State(state): State<AppState>,
|
||||
) -> Result<Json<Vec<CollectionResponse>>, ApiError> {
|
||||
let cols = state.storage.list_collections().await?;
|
||||
Ok(Json(
|
||||
cols.into_iter().map(CollectionResponse::from).collect(),
|
||||
))
|
||||
}
|
||||
|
||||
pub async fn get_collection(
|
||||
State(state): State<AppState>,
|
||||
Path(id): Path<Uuid>,
|
||||
) -> Result<Json<CollectionResponse>, ApiError> {
|
||||
let col = state.storage.get_collection(id).await?;
|
||||
Ok(Json(CollectionResponse::from(col)))
|
||||
}
|
||||
|
||||
pub async fn delete_collection(
|
||||
State(state): State<AppState>,
|
||||
Path(id): Path<Uuid>,
|
||||
) -> Result<Json<serde_json::Value>, ApiError> {
|
||||
state.storage.delete_collection(id).await?;
|
||||
Ok(Json(serde_json::json!({"deleted": true})))
|
||||
}
|
||||
|
||||
pub async fn add_member(
|
||||
State(state): State<AppState>,
|
||||
Path(collection_id): Path<Uuid>,
|
||||
Json(req): Json<AddMemberRequest>,
|
||||
) -> Result<Json<serde_json::Value>, ApiError> {
|
||||
pinakes_core::collections::add_member(
|
||||
&state.storage,
|
||||
collection_id,
|
||||
MediaId(req.media_id),
|
||||
req.position.unwrap_or(0),
|
||||
)
|
||||
.await?;
|
||||
Ok(Json(serde_json::json!({"added": true})))
|
||||
}
|
||||
|
||||
pub async fn remove_member(
|
||||
State(state): State<AppState>,
|
||||
Path((collection_id, media_id)): Path<(Uuid, Uuid)>,
|
||||
) -> Result<Json<serde_json::Value>, ApiError> {
|
||||
pinakes_core::collections::remove_member(&state.storage, collection_id, MediaId(media_id))
|
||||
.await?;
|
||||
Ok(Json(serde_json::json!({"removed": true})))
|
||||
}
|
||||
|
||||
pub async fn get_members(
|
||||
State(state): State<AppState>,
|
||||
Path(collection_id): Path<Uuid>,
|
||||
) -> Result<Json<Vec<MediaResponse>>, ApiError> {
|
||||
let items = pinakes_core::collections::get_members(&state.storage, collection_id).await?;
|
||||
Ok(Json(items.into_iter().map(MediaResponse::from).collect()))
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue