diff --git a/Pilz.Net/Api/Server/BaseChildItemHandler.cs b/Pilz.Net/Api/Server/BaseChildItemHandler.cs index 6fdfad2..c2fbb19 100644 --- a/Pilz.Net/Api/Server/BaseChildItemHandler.cs +++ b/Pilz.Net/Api/Server/BaseChildItemHandler.cs @@ -2,6 +2,7 @@ using Pilz.Extensions.Reflection; using Pilz.Net.Extensions; using System.Diagnostics; +using System.Net; namespace Pilz.Net.Api.Server; @@ -45,7 +46,7 @@ public abstract class BaseChildItemHandler(IApiSer return result; GetChilds(parentEntity).Add(entity); server.Manager.Save(parentEntity, true); - return ToClient(entity).ToItemResult(); + return ToClient(entity).ToItemResult(HttpStatusCode.Created); } public abstract IList GetChilds(TParent parent); diff --git a/Pilz.Net/Api/Server/BaseItemHandler.cs b/Pilz.Net/Api/Server/BaseItemHandler.cs index 3af9c2a..4adf6ff 100644 --- a/Pilz.Net/Api/Server/BaseItemHandler.cs +++ b/Pilz.Net/Api/Server/BaseItemHandler.cs @@ -2,6 +2,7 @@ using Pilz.Extensions.Reflection; using Pilz.Net.Extensions; using System.Diagnostics; +using System.Net; namespace Pilz.Net.Api.Server; @@ -34,6 +35,6 @@ public abstract class BaseItemHandler(IApiServer server) if (UpdateEntity(entity, msg) is ApiResult result) return result; server.Manager.Save(entity, true); - return ToClient(entity).ToItemResult(); + return ToClient(entity).ToItemResult(HttpStatusCode.Created); } } diff --git a/Pilz.Net/Extensions/ApiMessageExtensions.cs b/Pilz.Net/Extensions/ApiMessageExtensions.cs index 7d84dab..a37127a 100644 --- a/Pilz.Net/Extensions/ApiMessageExtensions.cs +++ b/Pilz.Net/Extensions/ApiMessageExtensions.cs @@ -1,22 +1,33 @@ using Pilz.Net.Api; using Pilz.Net.Api.Messages; +using System.Net; namespace Pilz.Net.Extensions; public static class ApiMessageExtensions { public static ApiResult ToItemResult(this T? @this) + { + return @this.ToItemResult(@this is not null ? HttpStatusCode.OK : HttpStatusCode.NoContent); + } + + public static ApiResult ToItemResult(this T? @this, HttpStatusCode statusCode) { if (@this == null) return ApiResult.NotFound(); - return ApiResult.Ok(@this.ToItemMsg()); + return new ApiResult(statusCode, @this.ToItemMsg()); } public static ApiResult ToItemsResult(this IEnumerable? @this) + { + return @this.ToItemsResult(@this is not null && @this.Any() ? HttpStatusCode.OK : HttpStatusCode.NoContent); + } + + public static ApiResult ToItemsResult(this IEnumerable? @this, HttpStatusCode statusCode) { if (@this == null) return ApiResult.NotFound(); - return ApiResult.Ok(@this.ToItemsMsg()); + return new ApiResult(statusCode, @this.ToItemMsg()); } public static GeneralItemMessages.Item ToItemMsg(this T @this)