some stauts codes

This commit is contained in:
Pilzinsel64
2025-04-02 09:50:13 +02:00
parent 7a5db49cfb
commit b60f8541f2
3 changed files with 17 additions and 4 deletions

View File

@@ -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<TEntity, TParent, TUpdateMsg>(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<TEntity> GetChilds(TParent parent);

View File

@@ -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<TEntity, TUpdateMsg>(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);
}
}

View File

@@ -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<T>(this T? @this)
{
return @this.ToItemResult(@this is not null ? HttpStatusCode.OK : HttpStatusCode.NoContent);
}
public static ApiResult ToItemResult<T>(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<T>(this IEnumerable<T>? @this)
{
return @this.ToItemsResult(@this is not null && @this.Any() ? HttpStatusCode.OK : HttpStatusCode.NoContent);
}
public static ApiResult ToItemsResult<T>(this IEnumerable<T>? @this, HttpStatusCode statusCode)
{
if (@this == null)
return ApiResult.NotFound();
return ApiResult.Ok(@this.ToItemsMsg());
return new ApiResult(statusCode, @this.ToItemMsg());
}
public static GeneralItemMessages<T>.Item ToItemMsg<T>(this T @this)