feat: Allow filtering notes by tag name in the API query and perform tag ID lookup in the route handler.
This commit is contained in:
@@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize};
|
|||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
use validator::Validate;
|
use validator::Validate;
|
||||||
|
|
||||||
use notes_domain::{Note, NoteFilter, Tag};
|
use notes_domain::{Note, Tag};
|
||||||
|
|
||||||
/// Request to create a new note
|
/// Request to create a new note
|
||||||
#[derive(Debug, Deserialize, Validate)]
|
#[derive(Debug, Deserialize, Validate)]
|
||||||
@@ -47,17 +47,8 @@ pub struct UpdateNoteRequest {
|
|||||||
pub struct ListNotesQuery {
|
pub struct ListNotesQuery {
|
||||||
pub pinned: Option<bool>,
|
pub pinned: Option<bool>,
|
||||||
pub archived: Option<bool>,
|
pub archived: Option<bool>,
|
||||||
pub tag: Option<Uuid>,
|
/// Tag name to filter by (will be looked up by route handler)
|
||||||
}
|
pub tag: Option<String>,
|
||||||
|
|
||||||
impl From<ListNotesQuery> for NoteFilter {
|
|
||||||
fn from(query: ListNotesQuery) -> Self {
|
|
||||||
let mut filter = NoteFilter::new();
|
|
||||||
filter.is_pinned = query.pinned;
|
|
||||||
filter.is_archived = query.archived;
|
|
||||||
filter.tag_id = query.tag;
|
|
||||||
filter
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Query parameters for search
|
/// Query parameters for search
|
||||||
|
|||||||
@@ -33,9 +33,23 @@ pub async fn list_notes(
|
|||||||
)))?;
|
)))?;
|
||||||
let user_id = user.id();
|
let user_id = user.id();
|
||||||
|
|
||||||
let service = NoteService::new(state.note_repo, state.tag_repo);
|
// Build the filter, looking up tag_id by name if needed
|
||||||
|
let mut filter = notes_domain::NoteFilter::new();
|
||||||
|
filter.is_pinned = query.pinned;
|
||||||
|
filter.is_archived = query.archived;
|
||||||
|
|
||||||
let notes = service.list_notes(user_id, query.into()).await?;
|
// Look up tag by name if provided
|
||||||
|
if let Some(ref tag_name) = query.tag {
|
||||||
|
if let Ok(Some(tag)) = state.tag_repo.find_by_name(user_id, tag_name).await {
|
||||||
|
filter.tag_id = Some(tag.id);
|
||||||
|
} else {
|
||||||
|
// Tag not found, return empty results
|
||||||
|
return Ok(Json(vec![]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let service = NoteService::new(state.note_repo, state.tag_repo);
|
||||||
|
let notes = service.list_notes(user_id, filter).await?;
|
||||||
let response: Vec<NoteResponse> = notes.into_iter().map(NoteResponse::from).collect();
|
let response: Vec<NoteResponse> = notes.into_iter().map(NoteResponse::from).collect();
|
||||||
|
|
||||||
Ok(Json(response))
|
Ok(Json(response))
|
||||||
|
|||||||
Reference in New Issue
Block a user