Added client library

This commit is contained in:
EzFeDezy
2025-05-10 09:23:28 +02:00
parent 36d126c63f
commit 97afed4a5b
14 changed files with 322 additions and 72 deletions

View File

@@ -1,8 +1,7 @@
using Microsoft.AspNetCore.Mvc;
using embeddingsearch;
using System.Text.Json;
using server.Models;
using Models;
namespace server.Controllers;
[ApiController]
@@ -23,7 +22,14 @@ public class EntityController : ControllerBase
[HttpGet("Query")]
public ActionResult<EntityQueryResults> Query(string searchdomain, string query)
{
Searchdomain searchdomain_ = _domainManager.GetSearchdomain(searchdomain);
Searchdomain searchdomain_;
try
{
searchdomain_ = _domainManager.GetSearchdomain(searchdomain);
} catch (Exception)
{
return Ok(new EntityQueryResults() {Results = []});
}
var results = searchdomain_.Search(query);
List<EntityQueryResult> queryResults = [.. results.Select(r => new EntityQueryResult
{
@@ -36,7 +42,14 @@ public class EntityController : ControllerBase
[HttpGet("Index")]
public ActionResult<EntityIndexResult> Index(string searchdomain, string jsonEntity)
{
Searchdomain searchdomain_ = _domainManager.GetSearchdomain(searchdomain);
Searchdomain searchdomain_;
try
{
searchdomain_ = _domainManager.GetSearchdomain(searchdomain);
} catch (Exception)
{
return Ok(new EntityIndexResult() {Success = false});
}
List<JSONEntity>? jsonEntities = JsonSerializer.Deserialize<List<JSONEntity>?>(jsonEntity);
if (jsonEntities is not null)
{
@@ -44,21 +57,29 @@ public class EntityController : ControllerBase
List<Entity>? entities = searchdomain_.EntitiesFromJSON(jsonEntity);
if (entities is not null)
{
return new EntityIndexResult() {Success = true};
return Ok(new EntityIndexResult() {Success = true});
}
else
{
_logger.LogDebug("Unable to deserialize an entity");
}
}
return new EntityIndexResult() {Success = false};
return Ok(new EntityIndexResult() {Success = false});
}
[HttpGet("List")]
public ActionResult<EntityListResults> List(string searchdomain, bool returnEmbeddings = false)
{
EntityListResults entityListResults = new() {Results = []};
Searchdomain searchdomain_ = _domainManager.GetSearchdomain(searchdomain);
Searchdomain searchdomain_;
try
{
searchdomain_ = _domainManager.GetSearchdomain(searchdomain);
} catch (Exception)
{
return Ok(new EntityListResults() {Results = [], Success = false});
}
EntityListResults entityListResults = new() {Results = [], Success = true};
foreach (Entity entity in searchdomain_.entityCache)
{
List<AttributeResult> attributeResults = [];
@@ -91,19 +112,26 @@ public class EntityController : ControllerBase
};
entityListResults.Results.Add(entityListResult);
}
return entityListResults;
return Ok(entityListResults);
}
[HttpGet("Delete")]
public ActionResult<EntityDeleteResults> Delete(string searchdomain, string entity) // TODO test this
public ActionResult<EntityDeleteResults> Delete(string searchdomain, string entityName) // TODO test this
{
Searchdomain searchdomain_ = _domainManager.GetSearchdomain(searchdomain);
Entity? entity_ = searchdomain_.GetEntity(entity);
Searchdomain searchdomain_;
try
{
searchdomain_ = _domainManager.GetSearchdomain(searchdomain);
} catch (Exception)
{
return Ok(new EntityDeleteResults() {Success = false});
}
Entity? entity_ = searchdomain_.GetEntity(entityName);
if (entity_ is null)
{
return new EntityDeleteResults() {Success = false};
return Ok(new EntityDeleteResults() {Success = false});
}
searchdomain_.DatabaseRemoveEntity(entity);
return new EntityDeleteResults() {Success = true};
searchdomain_.RemoveEntity(entityName);
return Ok(new EntityDeleteResults() {Success = true});
}
}

View File

@@ -1,6 +1,6 @@
using Microsoft.AspNetCore.Mvc;
using embeddingsearch;
using server.Models;
using Models;
namespace server.Controllers;
@@ -22,13 +22,23 @@ public class SearchdomainController : ControllerBase
[HttpGet("List")]
public ActionResult<SearchdomainListResults> List()
{
return Ok(_domainManager.ListSearchdomains());
var results = _domainManager.ListSearchdomains()
?? throw new Exception("Unable to list searchdomains");
SearchdomainListResults searchdomainListResults = new() {Searchdomains = results};
return Ok(searchdomainListResults);
}
[HttpGet("Create")]
public ActionResult<SearchdomainCreateResults> Create(string searchdomain, string settings = "{}")
{
return Ok(new SearchdomainCreateResults(){Id = _domainManager.CreateSearchdomain(searchdomain, settings)});
try
{
int id = _domainManager.CreateSearchdomain(searchdomain, settings);
return Ok(new SearchdomainCreateResults(){Id = id, Success = true});
} catch (Exception)
{
return Ok(new SearchdomainCreateResults(){Id = null, Success = false});
}
}
[HttpGet("Delete")]
@@ -40,8 +50,9 @@ public class SearchdomainController : ControllerBase
{
success = true;
deletedEntries = _domainManager.DeleteSearchdomain(searchdomain);
} catch (Exception)
} catch (Exception ex)
{
Console.WriteLine(ex);
success = false;
deletedEntries = 0;
}
@@ -51,14 +62,20 @@ public class SearchdomainController : ControllerBase
[HttpGet("Update")]
public ActionResult<SearchdomainUpdateResults> Update(string searchdomain, string newName, string settings = "{}")
{
Searchdomain searchdomain_ = _domainManager.GetSearchdomain(searchdomain);
Dictionary<string, dynamic> parameters = new()
try
{
{"name", newName},
{"settings", settings},
{"id", searchdomain_.id}
};
searchdomain_.ExecuteSQLNonQuery("UPDATE searchdomain set name = @name, settings = @settings WHERE id = @id", parameters);
Searchdomain searchdomain_ = _domainManager.GetSearchdomain(searchdomain);
Dictionary<string, dynamic> parameters = new()
{
{"name", newName},
{"settings", settings},
{"id", searchdomain_.id}
};
searchdomain_.ExecuteSQLNonQuery("UPDATE searchdomain set name = @name, settings = @settings WHERE id = @id", parameters);
} catch (Exception)
{
return Ok(new SearchdomainUpdateResults(){Success = false});
}
return Ok(new SearchdomainUpdateResults(){Success = true});
}
}