From 307601aa4b06b8d242b1bcd30a279fa48178ce54 Mon Sep 17 00:00:00 2001 From: Gabriel Kaszewski Date: Tue, 23 Dec 2025 11:13:37 +0100 Subject: [PATCH] feat: Allow filtering notes by tag name in the API query and perform tag ID lookup in the route handler. --- notes-api/src/dto.rs | 15 +++------------ notes-api/src/routes/notes.rs | 18 ++++++++++++++++-- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/notes-api/src/dto.rs b/notes-api/src/dto.rs index e617a5b..5aaf823 100644 --- a/notes-api/src/dto.rs +++ b/notes-api/src/dto.rs @@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize}; use uuid::Uuid; use validator::Validate; -use notes_domain::{Note, NoteFilter, Tag}; +use notes_domain::{Note, Tag}; /// Request to create a new note #[derive(Debug, Deserialize, Validate)] @@ -47,17 +47,8 @@ pub struct UpdateNoteRequest { pub struct ListNotesQuery { pub pinned: Option, pub archived: Option, - pub tag: Option, -} - -impl From 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 - } + /// Tag name to filter by (will be looked up by route handler) + pub tag: Option, } /// Query parameters for search diff --git a/notes-api/src/routes/notes.rs b/notes-api/src/routes/notes.rs index c427679..b4da472 100644 --- a/notes-api/src/routes/notes.rs +++ b/notes-api/src/routes/notes.rs @@ -33,9 +33,23 @@ pub async fn list_notes( )))?; 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 = notes.into_iter().map(NoteResponse::from).collect(); Ok(Json(response))