From cc93a765462acf3e06b57c33f59e255f78aef81a Mon Sep 17 00:00:00 2001 From: LD-Reborn Date: Thu, 25 Dec 2025 14:55:30 +0100 Subject: [PATCH 1/5] Fixed DRY violations regarding result models --- src/Shared/Models/BaseModels.cs | 12 ++++ src/Shared/Models/EntityResults.cs | 24 ++----- src/Shared/Models/SearchdomainResults.cs | 91 ++++-------------------- src/Shared/Models/ServerModels.cs | 8 +-- 4 files changed, 32 insertions(+), 103 deletions(-) create mode 100644 src/Shared/Models/BaseModels.cs diff --git a/src/Shared/Models/BaseModels.cs b/src/Shared/Models/BaseModels.cs new file mode 100644 index 0000000..df2454c --- /dev/null +++ b/src/Shared/Models/BaseModels.cs @@ -0,0 +1,12 @@ +using System.Text.Json.Serialization; + +namespace Shared.Models; + +public class SuccesMessageBaseModel +{ + [JsonPropertyName("Success")] + public required bool Success { get; set; } + [JsonPropertyName("Message")] + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + public string? Message { get; set; } +} \ No newline at end of file diff --git a/src/Shared/Models/EntityResults.cs b/src/Shared/Models/EntityResults.cs index 0e99110..ed9b7a4 100644 --- a/src/Shared/Models/EntityResults.cs +++ b/src/Shared/Models/EntityResults.cs @@ -3,14 +3,10 @@ using System.Text.Json.Serialization; namespace Shared.Models; -public class EntityQueryResults +public class EntityQueryResults : SuccesMessageBaseModel { [JsonPropertyName("Results")] public required List Results { get; set; } - [JsonPropertyName("Success")] - public required bool Success { get; set; } - [JsonPropertyName("Message")] - public string? Message { get; set; } } public class EntityQueryResult @@ -24,18 +20,14 @@ public class EntityQueryResult public Dictionary? Attributes { get; set; } } -public class EntityIndexResult -{ - [JsonPropertyName("Success")] - public required bool Success { get; set; } - [JsonPropertyName("Message")] - public string? Message { get; set; } -} +public class EntityIndexResult : SuccesMessageBaseModel {} public class EntityListResults { [JsonPropertyName("Results")] public required List Results { get; set; } + [JsonPropertyName("Message")] + public string? Message { get; set; } [JsonPropertyName("Success")] public required bool Success { get; set; } } @@ -80,11 +72,5 @@ public class EmbeddingResult public required float[] Embeddings { get; set; } } -public class EntityDeleteResults -{ - [JsonPropertyName("Success")] - public required bool Success { get; set; } - [JsonPropertyName("Message")] - public string? Message { get; set; } -} +public class EntityDeleteResults : SuccesMessageBaseModel {} diff --git a/src/Shared/Models/SearchdomainResults.cs b/src/Shared/Models/SearchdomainResults.cs index f6f1de7..e45c901 100644 --- a/src/Shared/Models/SearchdomainResults.cs +++ b/src/Shared/Models/SearchdomainResults.cs @@ -11,109 +11,46 @@ public class SearchdomainListResults public string? Message { get; set; } } -public class SearchdomainCreateResults +public class SearchdomainCreateResults : SuccesMessageBaseModel { - [JsonPropertyName("Success")] - public required bool Success { get; set; } - - [JsonPropertyName("Message")] - public string? Message { get; set; } - [JsonPropertyName("Id")] public int? Id { get; set; } } -public class SearchdomainUpdateResults +public class SearchdomainUpdateResults : SuccesMessageBaseModel {} + +public class SearchdomainDeleteResults : SuccesMessageBaseModel { - [JsonPropertyName("Success")] - public required bool Success { get; set; } - - [JsonPropertyName("Message")] - public string? Message { get; set; } -} - -public class SearchdomainDeleteResults -{ - [JsonPropertyName("Success")] - public required bool Success { get; set; } - - [JsonPropertyName("Message")] - public string? Message { get; set; } - [JsonPropertyName("DeletedEntities")] public required int DeletedEntities { get; set; } } -public class SearchdomainSearchesResults +public class SearchdomainSearchesResults : SuccesMessageBaseModel { - [JsonPropertyName("Success")] - public required bool Success { get; set; } - - [JsonPropertyName("Message")] - public string? Message { get; set; } [JsonPropertyName("Searches")] public required Dictionary Searches { get; set; } } -public class SearchdomainDeleteSearchResult +public class SearchdomainDeleteSearchResult : SuccesMessageBaseModel {} + +public class SearchdomainUpdateSearchResult : SuccesMessageBaseModel {} + +public class SearchdomainSettingsResults : SuccesMessageBaseModel { - [JsonPropertyName("Success")] - public required bool Success { get; set; } - - [JsonPropertyName("Message")] - public string? Message { get; set; } -} - -public class SearchdomainUpdateSearchResult -{ - [JsonPropertyName("Success")] - public required bool Success { get; set; } - - [JsonPropertyName("Message")] - public string? Message { get; set; } -} - -public class SearchdomainSettingsResults -{ - [JsonPropertyName("Success")] - public required bool Success { get; set; } - - [JsonPropertyName("Message")] - public string? Message { get; set; } - [JsonPropertyName("Settings")] public required SearchdomainSettings? Settings { get; set; } } -public class SearchdomainSearchCacheSizeResults +public class SearchdomainSearchCacheSizeResults : SuccesMessageBaseModel { - [JsonPropertyName("Success")] - public required bool Success { get; set; } - - [JsonPropertyName("Message")] - public string? Message { get; set; } - [JsonPropertyName("SearchCacheSizeBytes")] public required long? SearchCacheSizeBytes { get; set; } } -public class SearchdomainInvalidateCacheResults +public class SearchdomainInvalidateCacheResults : SuccesMessageBaseModel {} + +public class SearchdomainGetDatabaseSizeResult : SuccesMessageBaseModel { - [JsonPropertyName("Success")] - public required bool Success { get; set; } - - [JsonPropertyName("Message")] - public string? Message { get; set; } -} - -public class SearchdomainGetDatabaseSizeResult -{ - [JsonPropertyName("Success")] - public required bool Success { get; set; } - - [JsonPropertyName("Message")] - public string? Message { get; set; } - [JsonPropertyName("SearchdomainDatabaseSizeBytes")] public required long? SearchdomainDatabaseSizeBytes { get; set; } } diff --git a/src/Shared/Models/ServerModels.cs b/src/Shared/Models/ServerModels.cs index 278582e..f149ee2 100644 --- a/src/Shared/Models/ServerModels.cs +++ b/src/Shared/Models/ServerModels.cs @@ -2,14 +2,8 @@ using System.Text.Json.Serialization; namespace Shared.Models; -public class ServerGetModelsResult +public class ServerGetModelsResult : SuccesMessageBaseModel { - [JsonPropertyName("Success")] - public required bool Success { get; set; } - - [JsonPropertyName("Message")] - public string? Message { get; set; } - [JsonPropertyName("Models")] public string[]? Models { get; set; } } \ No newline at end of file From 26d0561c3bfc8568d727d2fd5f9b9681207b5e64 Mon Sep 17 00:00:00 2001 From: LD-Reborn Date: Thu, 25 Dec 2025 14:55:58 +0100 Subject: [PATCH 2/5] Fixed wrong return model returned in EntityController methods --- src/Server/Controllers/EntityController.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Server/Controllers/EntityController.cs b/src/Server/Controllers/EntityController.cs index bf11613..71a03a3 100644 --- a/src/Server/Controllers/EntityController.cs +++ b/src/Server/Controllers/EntityController.cs @@ -95,7 +95,7 @@ public class EntityController : ControllerBase if (returnEmbeddings && !returnModels) { _logger.LogError("Invalid request for {searchdomain} - embeddings return requested but without models - not possible!", [searchdomain]); - return Ok(new EntityQueryResults() {Results = [], Success = false, Message = "Invalid request" }); + return Ok(new EntityListResults() {Results = [], Success = false, Message = "Invalid request" }); } Searchdomain searchdomain_; try @@ -104,11 +104,11 @@ public class EntityController : ControllerBase } catch (SearchdomainNotFoundException) { _logger.LogError("Unable to retrieve the searchdomain {searchdomain} - it likely does not exist yet", [searchdomain]); - return Ok(new EntityQueryResults() {Results = [], Success = false, Message = "Searchdomain not found" }); + return Ok(new EntityListResults() {Results = [], Success = false, Message = "Searchdomain not found" }); } catch (Exception ex) { _logger.LogError("Unable to retrieve the searchdomain {searchdomain} - {ex.Message} - {ex.StackTrace}", [searchdomain, ex.Message, ex.StackTrace]); - return Ok(new EntityQueryResults() {Results = [], Success = false, Message = "Unable to retrieve the searchdomain - it likely exists, but some other error happened." }); + return Ok(new EntityListResults() {Results = [], Success = false, Message = "Unable to retrieve the searchdomain - it likely exists, but some other error happened." }); } EntityListResults entityListResults = new() {Results = [], Success = true}; foreach (Entity entity in searchdomain_.entityCache) @@ -157,11 +157,11 @@ public class EntityController : ControllerBase } catch (SearchdomainNotFoundException) { _logger.LogError("Unable to retrieve the searchdomain {searchdomain} - it likely does not exist yet", [searchdomain]); - return Ok(new EntityQueryResults() {Results = [], Success = false, Message = "Searchdomain not found" }); + return Ok(new EntityDeleteResults() {Success = false, Message = "Searchdomain not found" }); } catch (Exception ex) { _logger.LogError("Unable to retrieve the searchdomain {searchdomain} - {ex.Message} - {ex.StackTrace}", [searchdomain, ex.Message, ex.StackTrace]); - return Ok(new EntityQueryResults() {Results = [], Success = false, Message = "Unable to retrieve the searchdomain - it likely exists, but some other error happened." }); + return Ok(new EntityDeleteResults() {Success = false, Message = "Unable to retrieve the searchdomain - it likely exists, but some other error happened." }); } Entity? entity_ = SearchdomainHelper.CacheGetEntity(searchdomain_.entityCache, entityName); From 665a392b5adcbbeacd2730a6b43e9352187c5548 Mon Sep 17 00:00:00 2001 From: LD-Reborn Date: Thu, 25 Dec 2025 15:25:23 +0100 Subject: [PATCH 3/5] Fixed redundant Searchdomain retrieval error messages --- .../Controllers/SearchdomainController.cs | 170 ++++-------------- src/Server/Helper/SearchdomainHelper.cs | 17 ++ 2 files changed, 48 insertions(+), 139 deletions(-) diff --git a/src/Server/Controllers/SearchdomainController.cs b/src/Server/Controllers/SearchdomainController.cs index 286d8b5..a89c0b1 100644 --- a/src/Server/Controllers/SearchdomainController.cs +++ b/src/Server/Controllers/SearchdomainController.cs @@ -87,71 +87,38 @@ public class SearchdomainController : ControllerBase [HttpGet("Update")] public ActionResult Update(string searchdomain, string newName, string settings = "{}") { - try + (Searchdomain? searchdomain_, int? httpStatusCode, string? message) = SearchdomainHelper.TryGetSearchdomain(_domainManager, searchdomain, _logger); + if (searchdomain_ is null || httpStatusCode is not null) return StatusCode(httpStatusCode ?? 500, new SearchdomainUpdateResults(){Success = false, Message = message}); + Dictionary parameters = new() { - Searchdomain searchdomain_ = _domainManager.GetSearchdomain(searchdomain); - Dictionary parameters = new() - { - {"name", newName}, - {"settings", settings}, - {"id", searchdomain_.id} - }; - searchdomain_.helper.ExecuteSQLNonQuery("UPDATE searchdomain set name = @name, settings = @settings WHERE id = @id", parameters); - } catch (SearchdomainNotFoundException) - { - _logger.LogError("Unable to update searchdomain {searchdomain} - not found", [searchdomain]); - return Ok(new SearchdomainUpdateResults() { Success = false, Message = $"Unable to update searchdomain {searchdomain} - not found" }); - } catch (Exception ex) - { - _logger.LogError("Unable to update searchdomain {searchdomain} - Exception: {ex.Message} - {ex.StackTrace}", [searchdomain, ex.Message, ex.StackTrace]); - return Ok(new SearchdomainUpdateResults() { Success = false, Message = $"Unable to update searchdomain {searchdomain}" }); - } + {"name", newName}, + {"settings", settings}, + {"id", searchdomain_.id} + }; + searchdomain_.helper.ExecuteSQLNonQuery("UPDATE searchdomain set name = @name, settings = @settings WHERE id = @id", parameters); return Ok(new SearchdomainUpdateResults(){Success = true}); } [HttpPost("UpdateSettings")] public ActionResult UpdateSettings(string searchdomain, [FromBody] SearchdomainSettings request) { - try + (Searchdomain? searchdomain_, int? httpStatusCode, string? message) = SearchdomainHelper.TryGetSearchdomain(_domainManager, searchdomain, _logger); + if (searchdomain_ is null || httpStatusCode is not null) return StatusCode(httpStatusCode ?? 500, new SearchdomainUpdateResults(){Success = false, Message = message}); + Dictionary parameters = new() { - Searchdomain searchdomain_ = _domainManager.GetSearchdomain(searchdomain); - Dictionary parameters = new() - { - {"settings", JsonSerializer.Serialize(request)}, - {"id", searchdomain_.id} - }; - searchdomain_.helper.ExecuteSQLNonQuery("UPDATE searchdomain set settings = @settings WHERE id = @id", parameters); - searchdomain_.settings = request; - } catch (SearchdomainNotFoundException) - { - _logger.LogError("Unable to update settings for searchdomain {searchdomain} - not found", [searchdomain]); - return Ok(new SearchdomainUpdateResults() { Success = false, Message = $"Unable to update settings for searchdomain {searchdomain} - not found" }); - } catch (Exception ex) - { - _logger.LogError("Unable to update settings for searchdomain {searchdomain} - Exception: {ex.Message} - {ex.StackTrace}", [searchdomain, ex.Message, ex.StackTrace]); - return Ok(new SearchdomainUpdateResults() { Success = false, Message = $"Unable to update settings for searchdomain {searchdomain}" }); - } + {"settings", JsonSerializer.Serialize(request)}, + {"id", searchdomain_.id} + }; + searchdomain_.helper.ExecuteSQLNonQuery("UPDATE searchdomain set settings = @settings WHERE id = @id", parameters); + searchdomain_.settings = request; return Ok(new SearchdomainUpdateResults(){Success = true}); } [HttpGet("GetSearches")] public ActionResult GetSearches(string searchdomain) { - Searchdomain searchdomain_; - try - { - searchdomain_ = _domainManager.GetSearchdomain(searchdomain); - } - catch (SearchdomainNotFoundException) - { - _logger.LogError("Unable to retrieve the searchdomain {searchdomain} - it likely does not exist yet", [searchdomain]); - return Ok(new SearchdomainSearchesResults() { Searches = [], Success = false, Message = "Searchdomain not found" }); - } - catch (Exception ex) - { - _logger.LogError("Unable to retrieve the searchdomain {searchdomain} - {ex.Message} - {ex.StackTrace}", [searchdomain, ex.Message, ex.StackTrace]); - return Ok(new SearchdomainSearchesResults() { Searches = [], Success = false, Message = ex.Message }); - } + (Searchdomain? searchdomain_, int? httpStatusCode, string? message) = SearchdomainHelper.TryGetSearchdomain(_domainManager, searchdomain, _logger); + if (searchdomain_ is null || httpStatusCode is not null) return StatusCode(httpStatusCode ?? 500, new SearchdomainUpdateResults(){Success = false, Message = message}); Dictionary searchCache = searchdomain_.searchCache; return Ok(new SearchdomainSearchesResults() { Searches = searchCache, Success = true }); @@ -160,21 +127,8 @@ public class SearchdomainController : ControllerBase [HttpDelete("Searches")] public ActionResult DeleteSearch(string searchdomain, string query) { - Searchdomain searchdomain_; - try - { - searchdomain_ = _domainManager.GetSearchdomain(searchdomain); - } - catch (SearchdomainNotFoundException) - { - _logger.LogError("Unable to retrieve the searchdomain {searchdomain} - it likely does not exist yet", [searchdomain]); - return Ok(new SearchdomainDeleteSearchResult() { Success = false, Message = "Searchdomain not found" }); - } - catch (Exception ex) - { - _logger.LogError("Unable to retrieve the searchdomain {searchdomain} - {ex.Message} - {ex.StackTrace}", [searchdomain, ex.Message, ex.StackTrace]); - return Ok(new SearchdomainDeleteSearchResult() { Success = false, Message = ex.Message }); - } + (Searchdomain? searchdomain_, int? httpStatusCode, string? message) = SearchdomainHelper.TryGetSearchdomain(_domainManager, searchdomain, _logger); + if (searchdomain_ is null || httpStatusCode is not null) return StatusCode(httpStatusCode ?? 500, new SearchdomainUpdateResults(){Success = false, Message = message}); Dictionary searchCache = searchdomain_.searchCache; bool containsKey = searchCache.ContainsKey(query); if (containsKey) @@ -188,21 +142,8 @@ public class SearchdomainController : ControllerBase [HttpPatch("Searches")] public ActionResult UpdateSearch(string searchdomain, string query, [FromBody]List results) { - Searchdomain searchdomain_; - try - { - searchdomain_ = _domainManager.GetSearchdomain(searchdomain); - } - catch (SearchdomainNotFoundException) - { - _logger.LogError("Unable to retrieve the searchdomain {searchdomain} - it likely does not exist yet", [searchdomain]); - return Ok(new SearchdomainUpdateSearchResult() { Success = false, Message = "Searchdomain not found" }); - } - catch (Exception ex) - { - _logger.LogError("Unable to retrieve the searchdomain {searchdomain} - {ex.Message} - {ex.StackTrace}", [searchdomain, ex.Message, ex.StackTrace]); - return Ok(new SearchdomainUpdateSearchResult() { Success = false, Message = ex.Message }); - } + (Searchdomain? searchdomain_, int? httpStatusCode, string? message) = SearchdomainHelper.TryGetSearchdomain(_domainManager, searchdomain, _logger); + if (searchdomain_ is null || httpStatusCode is not null) return StatusCode(httpStatusCode ?? 500, new SearchdomainUpdateResults(){Success = false, Message = message}); Dictionary searchCache = searchdomain_.searchCache; bool containsKey = searchCache.ContainsKey(query); if (containsKey) @@ -218,21 +159,8 @@ public class SearchdomainController : ControllerBase [HttpGet("GetSettings")] public ActionResult GetSettings(string searchdomain) { - Searchdomain searchdomain_; - try - { - searchdomain_ = _domainManager.GetSearchdomain(searchdomain); - } - catch (SearchdomainNotFoundException) - { - _logger.LogError("Unable to retrieve the searchdomain {searchdomain} - it likely does not exist yet", [searchdomain]); - return Ok(new SearchdomainSettingsResults() { Settings = null, Success = false, Message = "Searchdomain not found" }); - } - catch (Exception ex) - { - _logger.LogError("Unable to retrieve the searchdomain {searchdomain} - {ex.Message} - {ex.StackTrace}", [searchdomain, ex.Message, ex.StackTrace]); - return Ok(new SearchdomainSettingsResults() { Settings = null, Success = false, Message = ex.Message }); - } + (Searchdomain? searchdomain_, int? httpStatusCode, string? message) = SearchdomainHelper.TryGetSearchdomain(_domainManager, searchdomain, _logger); + if (searchdomain_ is null || httpStatusCode is not null) return StatusCode(httpStatusCode ?? 500, new SearchdomainUpdateResults(){Success = false, Message = message}); SearchdomainSettings settings = searchdomain_.settings; return Ok(new SearchdomainSettingsResults() { Settings = settings, Success = true }); } @@ -240,21 +168,8 @@ public class SearchdomainController : ControllerBase [HttpGet("GetSearchCacheSize")] public ActionResult GetSearchCacheSize(string searchdomain) { - Searchdomain searchdomain_; - try - { - searchdomain_ = _domainManager.GetSearchdomain(searchdomain); - } - catch (SearchdomainNotFoundException) - { - _logger.LogError("Unable to retrieve the searchdomain {searchdomain} - it likely does not exist yet", [searchdomain]); - return Ok(new SearchdomainSearchCacheSizeResults() { SearchCacheSizeBytes = null, Success = false, Message = "Searchdomain not found" }); - } - catch (Exception ex) - { - _logger.LogError("Unable to retrieve the searchdomain {searchdomain} - {ex.Message} - {ex.StackTrace}", [searchdomain, ex.Message, ex.StackTrace]); - return Ok(new SearchdomainSearchCacheSizeResults() { SearchCacheSizeBytes = null, Success = false, Message = ex.Message }); - } + (Searchdomain? searchdomain_, int? httpStatusCode, string? message) = SearchdomainHelper.TryGetSearchdomain(_domainManager, searchdomain, _logger); + if (searchdomain_ is null || httpStatusCode is not null) return StatusCode(httpStatusCode ?? 500, new SearchdomainUpdateResults(){Success = false, Message = message}); Dictionary searchCache = searchdomain_.searchCache; long sizeInBytes = 0; foreach (var entry in searchCache) @@ -269,40 +184,17 @@ public class SearchdomainController : ControllerBase [HttpGet("ClearSearchCache")] public ActionResult InvalidateSearchCache(string searchdomain) { - try - { - Searchdomain searchdomain_ = _domainManager.GetSearchdomain(searchdomain); - searchdomain_.InvalidateSearchCache(); - } catch (SearchdomainNotFoundException) - { - _logger.LogError("Unable to invalidate search cache for searchdomain {searchdomain} - not found", [searchdomain]); - return Ok(new SearchdomainInvalidateCacheResults() { Success = false, Message = $"Unable to invalidate search cache for searchdomain {searchdomain} - not found" }); - } catch (Exception ex) - { - _logger.LogError("Unable to invalidate search cache for searchdomain {searchdomain} - Exception: {ex.Message} - {ex.StackTrace}", [searchdomain, ex.Message, ex.StackTrace]); - return Ok(new SearchdomainInvalidateCacheResults() { Success = false, Message = $"Unable to invalidate search cache for searchdomain {searchdomain}" }); - } + (Searchdomain? searchdomain_, int? httpStatusCode, string? message) = SearchdomainHelper.TryGetSearchdomain(_domainManager, searchdomain, _logger); + if (searchdomain_ is null || httpStatusCode is not null) return StatusCode(httpStatusCode ?? 500, new SearchdomainUpdateResults(){Success = false, Message = message}); + searchdomain_.InvalidateSearchCache(); return Ok(new SearchdomainInvalidateCacheResults(){Success = true}); } [HttpGet("GetDatabaseSize")] public ActionResult GetDatabaseSize(string searchdomain) { - Searchdomain searchdomain_; - try - { - searchdomain_ = _domainManager.GetSearchdomain(searchdomain); - } - catch (SearchdomainNotFoundException) - { - _logger.LogError("Unable to retrieve the searchdomain {searchdomain} - it likely does not exist yet", [searchdomain]); - return Ok(new SearchdomainGetDatabaseSizeResult() { SearchdomainDatabaseSizeBytes = null, Success = false, Message = "Searchdomain not found" }); - } - catch (Exception ex) - { - _logger.LogError("Unable to retrieve the searchdomain {searchdomain} - {ex.Message} - {ex.StackTrace}", [searchdomain, ex.Message, ex.StackTrace]); - return Ok(new SearchdomainGetDatabaseSizeResult() { SearchdomainDatabaseSizeBytes = null, Success = false, Message = ex.Message }); - } + (Searchdomain? searchdomain_, int? httpStatusCode, string? message) = SearchdomainHelper.TryGetSearchdomain(_domainManager, searchdomain, _logger); + if (searchdomain_ is null || httpStatusCode is not null) return StatusCode(httpStatusCode ?? 500, new SearchdomainUpdateResults(){Success = false, Message = message}); long sizeInBytes = DatabaseHelper.GetSearchdomainDatabaseSize(searchdomain_.helper, searchdomain); return Ok(new SearchdomainGetDatabaseSizeResult() { SearchdomainDatabaseSizeBytes = sizeInBytes, Success = true }); } diff --git a/src/Server/Helper/SearchdomainHelper.cs b/src/Server/Helper/SearchdomainHelper.cs index 96727cf..3409ba4 100644 --- a/src/Server/Helper/SearchdomainHelper.cs +++ b/src/Server/Helper/SearchdomainHelper.cs @@ -269,4 +269,21 @@ public class SearchdomainHelper(ILogger logger, DatabaseHelp var similarityMethod = new SimilarityMethod(jsonDatapoint.SimilarityMethod, logger) ?? throw new SimilarityMethodNotFoundException(jsonDatapoint.SimilarityMethod); return new Datapoint(jsonDatapoint.Name, probMethod_embedding, similarityMethod, hash, [.. embeddings.Select(kv => (kv.Key, kv.Value))]); } + + public static (Searchdomain?, int?, string?) TryGetSearchdomain(SearchdomainManager searchdomainManager, string searchdomain, ILogger logger) + { + try + { + Searchdomain searchdomain_ = searchdomainManager.GetSearchdomain(searchdomain); + return (searchdomain_, null, null); + } catch (SearchdomainNotFoundException) + { + logger.LogError("Unable to update searchdomain {searchdomain} - not found", [searchdomain]); + return (null, 500, $"Unable to update searchdomain {searchdomain} - not found"); + } catch (Exception ex) + { + logger.LogError("Unable to update searchdomain {searchdomain} - Exception: {ex.Message} - {ex.StackTrace}", [searchdomain, ex.Message, ex.StackTrace]); + return (null, 404, $"Unable to update searchdomain {searchdomain}"); + } + } } \ No newline at end of file From a358eaea86267f8ca84c2793d51d7f2d0c34f7f0 Mon Sep 17 00:00:00 2001 From: LD-Reborn Date: Sat, 27 Dec 2025 17:25:12 +0100 Subject: [PATCH 4/5] Moved query action from EntityController to SearchdomainController --- src/Server/Controllers/EntityController.cs | 58 ++----------------- .../Controllers/SearchdomainController.cs | 15 +++++ 2 files changed, 20 insertions(+), 53 deletions(-) diff --git a/src/Server/Controllers/EntityController.cs b/src/Server/Controllers/EntityController.cs index 71a03a3..2698f6a 100644 --- a/src/Server/Controllers/EntityController.cs +++ b/src/Server/Controllers/EntityController.cs @@ -24,32 +24,6 @@ public class EntityController : ControllerBase _databaseHelper = databaseHelper; } - [HttpGet("Query")] - public ActionResult Query(string searchdomain, string query, int? topN, bool returnAttributes = false) - { - Searchdomain searchdomain_; - try - { - searchdomain_ = _domainManager.GetSearchdomain(searchdomain); - } catch (SearchdomainNotFoundException) - { - _logger.LogError("Unable to retrieve the searchdomain {searchdomain} - it likely does not exist yet", [searchdomain]); - return Ok(new EntityQueryResults() {Results = [], Success = false, Message = "Searchdomain not found" }); - } catch (Exception ex) - { - _logger.LogError("Unable to retrieve the searchdomain {searchdomain} - {ex.Message} - {ex.StackTrace}", [searchdomain, ex.Message, ex.StackTrace]); - return Ok(new EntityQueryResults() {Results = [], Success = false, Message = "Unable to retrieve the searchdomain - it likely exists, but some other error happened." }); - } - List<(float, string)> results = searchdomain_.Search(query, topN); - List queryResults = [.. results.Select(r => new EntityQueryResult - { - Name = r.Item2, - Value = r.Item1, - Attributes = returnAttributes ? (searchdomain_.entityCache.FirstOrDefault(x => x.name == r.Item2)?.attributes ?? null) : null - })]; - return Ok(new EntityQueryResults(){Results = queryResults, Success = true }); - } - [HttpPost("Index")] public ActionResult Index([FromBody] List? jsonEntities) { @@ -95,21 +69,10 @@ public class EntityController : ControllerBase if (returnEmbeddings && !returnModels) { _logger.LogError("Invalid request for {searchdomain} - embeddings return requested but without models - not possible!", [searchdomain]); - return Ok(new EntityListResults() {Results = [], Success = false, Message = "Invalid request" }); - } - Searchdomain searchdomain_; - try - { - searchdomain_ = _domainManager.GetSearchdomain(searchdomain); - } catch (SearchdomainNotFoundException) - { - _logger.LogError("Unable to retrieve the searchdomain {searchdomain} - it likely does not exist yet", [searchdomain]); - return Ok(new EntityListResults() {Results = [], Success = false, Message = "Searchdomain not found" }); - } catch (Exception ex) - { - _logger.LogError("Unable to retrieve the searchdomain {searchdomain} - {ex.Message} - {ex.StackTrace}", [searchdomain, ex.Message, ex.StackTrace]); - return Ok(new EntityListResults() {Results = [], Success = false, Message = "Unable to retrieve the searchdomain - it likely exists, but some other error happened." }); + return BadRequest(new EntityListResults() {Results = [], Success = false, Message = "Invalid request" }); } + (Searchdomain? searchdomain_, int? httpStatusCode, string? message) = SearchdomainHelper.TryGetSearchdomain(_domainManager, searchdomain, _logger); + if (searchdomain_ is null || httpStatusCode is not null) return StatusCode(httpStatusCode ?? 500, new SearchdomainUpdateResults(){Success = false, Message = message}); EntityListResults entityListResults = new() {Results = [], Success = true}; foreach (Entity entity in searchdomain_.entityCache) { @@ -150,19 +113,8 @@ public class EntityController : ControllerBase [HttpGet("Delete")] public ActionResult Delete(string searchdomain, string entityName) { - Searchdomain searchdomain_; - try - { - searchdomain_ = _domainManager.GetSearchdomain(searchdomain); - } catch (SearchdomainNotFoundException) - { - _logger.LogError("Unable to retrieve the searchdomain {searchdomain} - it likely does not exist yet", [searchdomain]); - return Ok(new EntityDeleteResults() {Success = false, Message = "Searchdomain not found" }); - } catch (Exception ex) - { - _logger.LogError("Unable to retrieve the searchdomain {searchdomain} - {ex.Message} - {ex.StackTrace}", [searchdomain, ex.Message, ex.StackTrace]); - return Ok(new EntityDeleteResults() {Success = false, Message = "Unable to retrieve the searchdomain - it likely exists, but some other error happened." }); - } + (Searchdomain? searchdomain_, int? httpStatusCode, string? message) = SearchdomainHelper.TryGetSearchdomain(_domainManager, searchdomain, _logger); + if (searchdomain_ is null || httpStatusCode is not null) return StatusCode(httpStatusCode ?? 500, new SearchdomainUpdateResults(){Success = false, Message = message}); Entity? entity_ = SearchdomainHelper.CacheGetEntity(searchdomain_.entityCache, entityName); if (entity_ is null) diff --git a/src/Server/Controllers/SearchdomainController.cs b/src/Server/Controllers/SearchdomainController.cs index a89c0b1..66d9aef 100644 --- a/src/Server/Controllers/SearchdomainController.cs +++ b/src/Server/Controllers/SearchdomainController.cs @@ -99,6 +99,21 @@ public class SearchdomainController : ControllerBase return Ok(new SearchdomainUpdateResults(){Success = true}); } + [HttpGet("Query")] + public ActionResult Query(string searchdomain, string query, int? topN, bool returnAttributes = false) + { + (Searchdomain? searchdomain_, int? httpStatusCode, string? message) = SearchdomainHelper.TryGetSearchdomain(_domainManager, searchdomain, _logger); + if (searchdomain_ is null || httpStatusCode is not null) return StatusCode(httpStatusCode ?? 500, new SearchdomainUpdateResults(){Success = false, Message = message}); + List<(float, string)> results = searchdomain_.Search(query, topN); + List queryResults = [.. results.Select(r => new EntityQueryResult + { + Name = r.Item2, + Value = r.Item1, + Attributes = returnAttributes ? (searchdomain_.entityCache.FirstOrDefault(x => x.name == r.Item2)?.attributes ?? null) : null + })]; + return Ok(new EntityQueryResults(){Results = queryResults, Success = true }); + } + [HttpPost("UpdateSettings")] public ActionResult UpdateSettings(string searchdomain, [FromBody] SearchdomainSettings request) { From f3a46651532bbf2e4f17d53785bf991c41deb912 Mon Sep 17 00:00:00 2001 From: LD-Reborn Date: Sat, 27 Dec 2025 17:25:12 +0100 Subject: [PATCH 5/5] Moved query action from EntityController to SearchdomainController --- src/Client/Client.cs | 2 +- src/Server/Controllers/EntityController.cs | 58 ++----------------- .../Controllers/SearchdomainController.cs | 15 +++++ 3 files changed, 21 insertions(+), 54 deletions(-) diff --git a/src/Client/Client.cs b/src/Client/Client.cs index 61c7dd1..66ab162 100644 --- a/src/Client/Client.cs +++ b/src/Client/Client.cs @@ -89,7 +89,7 @@ public class Client public async Task EntityQueryAsync(string searchdomain, string query) { - return await GetUrlAndProcessJson(GetUrl($"{baseUri}/Entity", "Query", apiKey, new Dictionary() + return await GetUrlAndProcessJson(GetUrl($"{baseUri}/Searchdomain", "Query", apiKey, new Dictionary() { {"searchdomain", searchdomain}, {"query", query} diff --git a/src/Server/Controllers/EntityController.cs b/src/Server/Controllers/EntityController.cs index 71a03a3..2698f6a 100644 --- a/src/Server/Controllers/EntityController.cs +++ b/src/Server/Controllers/EntityController.cs @@ -24,32 +24,6 @@ public class EntityController : ControllerBase _databaseHelper = databaseHelper; } - [HttpGet("Query")] - public ActionResult Query(string searchdomain, string query, int? topN, bool returnAttributes = false) - { - Searchdomain searchdomain_; - try - { - searchdomain_ = _domainManager.GetSearchdomain(searchdomain); - } catch (SearchdomainNotFoundException) - { - _logger.LogError("Unable to retrieve the searchdomain {searchdomain} - it likely does not exist yet", [searchdomain]); - return Ok(new EntityQueryResults() {Results = [], Success = false, Message = "Searchdomain not found" }); - } catch (Exception ex) - { - _logger.LogError("Unable to retrieve the searchdomain {searchdomain} - {ex.Message} - {ex.StackTrace}", [searchdomain, ex.Message, ex.StackTrace]); - return Ok(new EntityQueryResults() {Results = [], Success = false, Message = "Unable to retrieve the searchdomain - it likely exists, but some other error happened." }); - } - List<(float, string)> results = searchdomain_.Search(query, topN); - List queryResults = [.. results.Select(r => new EntityQueryResult - { - Name = r.Item2, - Value = r.Item1, - Attributes = returnAttributes ? (searchdomain_.entityCache.FirstOrDefault(x => x.name == r.Item2)?.attributes ?? null) : null - })]; - return Ok(new EntityQueryResults(){Results = queryResults, Success = true }); - } - [HttpPost("Index")] public ActionResult Index([FromBody] List? jsonEntities) { @@ -95,21 +69,10 @@ public class EntityController : ControllerBase if (returnEmbeddings && !returnModels) { _logger.LogError("Invalid request for {searchdomain} - embeddings return requested but without models - not possible!", [searchdomain]); - return Ok(new EntityListResults() {Results = [], Success = false, Message = "Invalid request" }); - } - Searchdomain searchdomain_; - try - { - searchdomain_ = _domainManager.GetSearchdomain(searchdomain); - } catch (SearchdomainNotFoundException) - { - _logger.LogError("Unable to retrieve the searchdomain {searchdomain} - it likely does not exist yet", [searchdomain]); - return Ok(new EntityListResults() {Results = [], Success = false, Message = "Searchdomain not found" }); - } catch (Exception ex) - { - _logger.LogError("Unable to retrieve the searchdomain {searchdomain} - {ex.Message} - {ex.StackTrace}", [searchdomain, ex.Message, ex.StackTrace]); - return Ok(new EntityListResults() {Results = [], Success = false, Message = "Unable to retrieve the searchdomain - it likely exists, but some other error happened." }); + return BadRequest(new EntityListResults() {Results = [], Success = false, Message = "Invalid request" }); } + (Searchdomain? searchdomain_, int? httpStatusCode, string? message) = SearchdomainHelper.TryGetSearchdomain(_domainManager, searchdomain, _logger); + if (searchdomain_ is null || httpStatusCode is not null) return StatusCode(httpStatusCode ?? 500, new SearchdomainUpdateResults(){Success = false, Message = message}); EntityListResults entityListResults = new() {Results = [], Success = true}; foreach (Entity entity in searchdomain_.entityCache) { @@ -150,19 +113,8 @@ public class EntityController : ControllerBase [HttpGet("Delete")] public ActionResult Delete(string searchdomain, string entityName) { - Searchdomain searchdomain_; - try - { - searchdomain_ = _domainManager.GetSearchdomain(searchdomain); - } catch (SearchdomainNotFoundException) - { - _logger.LogError("Unable to retrieve the searchdomain {searchdomain} - it likely does not exist yet", [searchdomain]); - return Ok(new EntityDeleteResults() {Success = false, Message = "Searchdomain not found" }); - } catch (Exception ex) - { - _logger.LogError("Unable to retrieve the searchdomain {searchdomain} - {ex.Message} - {ex.StackTrace}", [searchdomain, ex.Message, ex.StackTrace]); - return Ok(new EntityDeleteResults() {Success = false, Message = "Unable to retrieve the searchdomain - it likely exists, but some other error happened." }); - } + (Searchdomain? searchdomain_, int? httpStatusCode, string? message) = SearchdomainHelper.TryGetSearchdomain(_domainManager, searchdomain, _logger); + if (searchdomain_ is null || httpStatusCode is not null) return StatusCode(httpStatusCode ?? 500, new SearchdomainUpdateResults(){Success = false, Message = message}); Entity? entity_ = SearchdomainHelper.CacheGetEntity(searchdomain_.entityCache, entityName); if (entity_ is null) diff --git a/src/Server/Controllers/SearchdomainController.cs b/src/Server/Controllers/SearchdomainController.cs index a89c0b1..66d9aef 100644 --- a/src/Server/Controllers/SearchdomainController.cs +++ b/src/Server/Controllers/SearchdomainController.cs @@ -99,6 +99,21 @@ public class SearchdomainController : ControllerBase return Ok(new SearchdomainUpdateResults(){Success = true}); } + [HttpGet("Query")] + public ActionResult Query(string searchdomain, string query, int? topN, bool returnAttributes = false) + { + (Searchdomain? searchdomain_, int? httpStatusCode, string? message) = SearchdomainHelper.TryGetSearchdomain(_domainManager, searchdomain, _logger); + if (searchdomain_ is null || httpStatusCode is not null) return StatusCode(httpStatusCode ?? 500, new SearchdomainUpdateResults(){Success = false, Message = message}); + List<(float, string)> results = searchdomain_.Search(query, topN); + List queryResults = [.. results.Select(r => new EntityQueryResult + { + Name = r.Item2, + Value = r.Item1, + Attributes = returnAttributes ? (searchdomain_.entityCache.FirstOrDefault(x => x.name == r.Item2)?.attributes ?? null) : null + })]; + return Ok(new EntityQueryResults(){Results = queryResults, Success = true }); + } + [HttpPost("UpdateSettings")] public ActionResult UpdateSettings(string searchdomain, [FromBody] SearchdomainSettings request) {