Skip to content

Commit 68c3153

Browse files
author
davidwei
committed
Refactor search architecture: move integer ID enhancement to NoteService
- Add SearchUserNotes method to INoteService and NoteService - Move integer ID enhancement logic from SearchService to NoteService - Remove INoteRepository dependency from SearchService - Update NotesController to use SearchUserNotes method - Clean up SearchService to focus purely on search index operations - Update SearchServiceTests to remove repository-related tests - Add comprehensive tests for SearchUserNotes method Fixes #11
1 parent e92f30d commit 68c3153

File tree

6 files changed

+369
-210
lines changed

6 files changed

+369
-210
lines changed

src/HappyNotes.Api/Controllers/NotesController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public async Task<ApiResult> PurgeDeleted()
7979
public async Task<ApiResult<PageData<NoteDto>>> Search(string query, int pageNumber = 1, int pageSize = 10, string filter = "normal")
8080
{
8181
var filterType = filter.ToLower() == "deleted" ? NoteFilterType.Deleted : NoteFilterType.Normal;
82-
var results = await noteService.GetUserKeywordNotes(currentUser.Id, pageSize, pageNumber, query, filterType);
82+
var results = await noteService.SearchUserNotes(currentUser.Id, pageSize, pageNumber, query, filterType);
8383
return new SuccessfulResult<PageData<NoteDto>>(mapper.Map<PageData<NoteDto>>(results));
8484
}
8585
}

src/HappyNotes.Services/NoteService.cs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,62 @@ public async Task<PageData<Note>> GetUserKeywordNotes(long userId, int pageSize,
182182
};
183183
}
184184

185+
public async Task<PageData<Note>> SearchUserNotes(long userId, int pageSize, int pageNumber, string keyword, NoteFilterType filter = NoteFilterType.Normal)
186+
{
187+
keyword = keyword?.Trim() ?? string.Empty;
188+
if (keyword.Length == 0)
189+
{
190+
return new PageData<Note>()
191+
{
192+
DataList = [],
193+
PageIndex = pageNumber,
194+
PageSize = pageSize,
195+
TotalCount = 0,
196+
};
197+
}
198+
199+
// Bigram indexing requires at least 2 characters to generate valid tokens
200+
if (keyword.Length == 1)
201+
{
202+
return new PageData<Note>()
203+
{
204+
DataList = [],
205+
PageIndex = pageNumber,
206+
PageSize = pageSize,
207+
TotalCount = 0,
208+
};
209+
}
210+
211+
// Get search results from SearchService
212+
var (noteIds, total) = await searchService.GetNoteIdsByKeywordAsync(userId, keyword, pageNumber, pageSize, filter);
213+
var noteIdList = noteIds.ToList();
214+
215+
// Enhancement: if keyword is an integer and this is the first page,
216+
// check if the note ID belongs to the current user and prepend it to results
217+
if (pageNumber == 1 && long.TryParse(keyword, out long noteId))
218+
{
219+
// Check if this note ID belongs to the current user
220+
var note = await noteRepository.GetFirstOrDefaultAsync(n => n.Id == noteId);
221+
222+
if (note != null && note.UserId == userId)
223+
{
224+
// Remove the note from the list if it's already there to avoid duplication
225+
noteIdList.RemoveAll(id => id == noteId);
226+
// Prepend it to the beginning
227+
noteIdList.Insert(0, noteId);
228+
}
229+
}
230+
231+
var notes = await _GetNotesByIds(noteIdList);
232+
return new PageData<Note>()
233+
{
234+
DataList = notes,
235+
PageIndex = pageNumber,
236+
PageSize = pageSize,
237+
TotalCount = total,
238+
};
239+
}
240+
185241
public async Task<PageData<Note>> GetLinkedNotes(long userId, long noteId)
186242
{
187243
return await noteRepository.GetLinkedNotes(userId, noteId);

src/HappyNotes.Services/SearchService.cs

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using HappyNotes.Entities;
88
using HappyNotes.Models.Search;
99
using HappyNotes.Services.interfaces;
10-
using HappyNotes.Repositories.interfaces;
1110
using SqlSugar;
1211

1312
namespace HappyNotes.Services;
@@ -16,13 +15,11 @@ public class SearchService : ISearchService
1615
{
1716
private readonly IDatabaseClient _client;
1817
private readonly HttpClient _httpClient;
19-
private readonly INoteRepository _noteRepository;
2018

21-
public SearchService(IDatabaseClient client, HttpClient httpClient, ManticoreConnectionOptions options, INoteRepository noteRepository)
19+
public SearchService(IDatabaseClient client, HttpClient httpClient, ManticoreConnectionOptions options)
2220
{
2321
_client = client;
2422
_httpClient = httpClient;
25-
_noteRepository = noteRepository;
2623
_httpClient.BaseAddress = new Uri(options.HttpEndpoint);
2724
_httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
2825
}
@@ -69,22 +66,6 @@ public SearchService(IDatabaseClient client, HttpClient httpClient, ManticoreCon
6966
}
7067
}
7168

72-
// Enhancement: if query is an integer and this is the first page,
73-
// check if the note ID belongs to the current user and prepend it to results
74-
if (pageNumber == 1 && long.TryParse(query, out long noteId))
75-
{
76-
// Check if this note ID belongs to the current user
77-
var note = await _noteRepository.GetFirstOrDefaultAsync(n => n.Id == noteId);
78-
79-
if (note != null && note.UserId == userId)
80-
{
81-
// Remove the note from the list if it's already there to avoid duplication
82-
noteIdList.RemoveAll(id => id == noteId);
83-
// Prepend it to the beginning
84-
noteIdList.Insert(0, noteId);
85-
}
86-
}
87-
8869
return (noteIdList, (int)total);
8970
}
9071

src/HappyNotes.Services/interfaces/INoteService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public interface INoteService
2424

2525
Task<PageData<Note>> GetUserTagNotes(long userId, int pageSize, int pageNumber, string tag);
2626
Task<PageData<Note>> GetUserKeywordNotes(long userId, int pageSize, int pageNumber, string keyword, NoteFilterType filter = NoteFilterType.Normal);
27+
Task<PageData<Note>> SearchUserNotes(long userId, int pageSize, int pageNumber, string keyword, NoteFilterType filter = NoteFilterType.Normal);
2728

2829
Task<PageData<Note>> GetLinkedNotes(long userId, long noteId);
2930
Task<IList<Note>> Memories(long userId, string localTimezone);

0 commit comments

Comments
 (0)