diff --git a/Pilz.Net/Api/Client/BaseChildItemClient.cs b/Pilz.Net/Api/Client/BaseChildItemClient.cs index f3a5b89..d966a70 100644 --- a/Pilz.Net/Api/Client/BaseChildItemClient.cs +++ b/Pilz.Net/Api/Client/BaseChildItemClient.cs @@ -1,17 +1,17 @@ using Pilz.Data; using Pilz.Net.Api.Messages; -using Pilz.Net.Extensions; namespace Pilz.Net.Api.Client; public abstract class BaseChildItemClient(IApiClient client) : BaseClient(client), IBaseChildItemClient where T : IDataObject { + public override string ApiEndpoint => ApiEndpointParent + ApiEndpointChild; + public abstract string ApiEndpointParent { get; } + public abstract string ApiEndpointChild { get; } + public virtual async Task> GetAll(int parentId) { - return (await client.SendRequest.Items>(ApiEndpoint, HttpMethod.Get, new ApiParameterCollection - { - ["parent"] = parentId, - })).EnsureOk().Items; + return (await client.SendRequest.Items>($"{ApiEndpointParent}/{parentId}{ApiEndpointChild}", HttpMethod.Get)).EnsureOk().Items; } public override async Task Save(T item) @@ -26,12 +26,8 @@ public abstract class BaseChildItemClient(IApiClient client) : BaseClient( if (item.Id != 0) return await Save(item); return (await client.SendRequest.Item>( - ApiEndpoint, + $"{ApiEndpointParent}/{parentId}{ApiEndpointChild}", HttpMethod.Post, - GenerateUpdateMessage(item, true), - new ApiParameterCollection - { - ["parent"] = parentId, - })).EnsureOk().Item; + GenerateUpdateMessage(item, true))).EnsureOk().Item; } } diff --git a/Pilz.Net/Api/Server/BaseChildItemHandler.cs b/Pilz.Net/Api/Server/BaseChildItemHandler.cs index f108cf3..e92ac6b 100644 --- a/Pilz.Net/Api/Server/BaseChildItemHandler.cs +++ b/Pilz.Net/Api/Server/BaseChildItemHandler.cs @@ -19,6 +19,19 @@ public abstract class BaseChildItemHandler(IApiSer public TParent? Parent => parent ??= getParent(); } + /// + /// Gets the base route (endpoint) for the most API calls. + /// By default this defines the combo of and . + /// + public override string Route => RouteParent + RouteChild; + /// + /// Gets the base route of the parent entity. + /// + public abstract string RouteParent { get; } + /// + /// Gets the realtive base route of the child entity. + /// + public abstract string RouteChild { get; } protected virtual bool RegisterGetAll => true; protected virtual bool RegisterPost => true; @@ -27,18 +40,18 @@ public abstract class BaseChildItemHandler(IApiSer base.Initialize(server); var t = GetType(); if (RegisterGetAll) - server.RegisterHandler(t.GetMethod(nameof(GetAll))!.CreateDelegate(this), new(Route, "GET"), Debugger.IsAttached); + server.RegisterHandler(t.GetMethod(nameof(GetAll))!.CreateDelegate(this), new(RouteParent + "/{pid}" + RouteChild, "GET"), Debugger.IsAttached); if (RegisterPost) - server.RegisterHandler(t.GetMethod(nameof(Post))!.CreateDelegate(this), new(Route, "POST"), Debugger.IsAttached); + server.RegisterHandler(t.GetMethod(nameof(Post))!.CreateDelegate(this), new(RouteParent + "/{pid}" + RouteChild, "POST"), Debugger.IsAttached); } - public virtual ApiResult GetAll(int parent) + public virtual ApiResult GetAll(int pid) { IQueryable list; - if (parent != 0) + if (pid != 0) { - if (!server.Manager.Find(parent, out TParent? parentEntity)) + if (!server.Manager.Find(pid, out TParent? parentEntity)) return ApiResult.NotFound(); list = GetChilds(parentEntity).AsQueryable(); } @@ -48,9 +61,9 @@ public abstract class BaseChildItemHandler(IApiSer return SortEntities(list).ToList().Select(ToClient).ToItemsResult(); } - public virtual ApiResult Post(int parent, TUpdateMsg msg, ApiRequestInfo req) + public virtual ApiResult Post(int pid, TUpdateMsg msg, ApiRequestInfo req) { - if (!server.Manager.Find(parent, out TParent? parentEntity)) + if (!server.Manager.Find(pid, out TParent? parentEntity)) return ApiResult.NotFound(); var entity = CreateNewEntity(msg, parentEntity); var update = new ChildEntityUpdate(entity, () => parentEntity, msg, req); @@ -81,7 +94,7 @@ public abstract class BaseChildItemHandler(IApiSer protected override ApiResult? OnSave(EntityUpdate update) { - if (update.Entity.Id == 0 && update is ChildEntityUpdate updateEx && updateEx.Parent != null) + if (update.Entity.Id == 0 && update is ChildEntityUpdate { Parent: not null } updateEx) { server.Manager.Save(updateEx.Parent, true); return null; diff --git a/Pilz.Net/Api/Server/BaseHandler.cs b/Pilz.Net/Api/Server/BaseHandler.cs index 441e98a..828464d 100644 --- a/Pilz.Net/Api/Server/BaseHandler.cs +++ b/Pilz.Net/Api/Server/BaseHandler.cs @@ -17,6 +17,9 @@ public abstract class BaseHandler(IApiServer server) public ApiRequestInfo Request { get; } = reqest; } + /// + /// Gets the base route (endpoint) for the most API calls. + /// public abstract string Route { get; } protected virtual bool RegisterGet => true; protected virtual bool RegisterPut => true;