From 7ed415fd5cbbffb059c8474be7f0d294bedf298e Mon Sep 17 00:00:00 2001 From: Edgar Mesquita Date: Fri, 31 May 2024 11:45:57 +0100 Subject: [PATCH] fix: Reference type conversion --- src/Api.Crud.Client/Api.Crud.Client.csproj | 6 +-- src/Api.Crud/Api.Crud.csproj | 6 +-- .../Extensions/HttpContextExtensions.cs | 54 +++++++++++++++++-- src/Api.Crud/Handlers/CrudEndpointHandlers.cs | 7 +++ .../Handlers/ReaderEndpointHandlers.cs | 6 +++ src/Application.Crud/Application.Crud.csproj | 6 +-- 6 files changed, 73 insertions(+), 12 deletions(-) diff --git a/src/Api.Crud.Client/Api.Crud.Client.csproj b/src/Api.Crud.Client/Api.Crud.Client.csproj index 28f94e3..1ad2182 100644 --- a/src/Api.Crud.Client/Api.Crud.Client.csproj +++ b/src/Api.Crud.Client/Api.Crud.Client.csproj @@ -7,9 +7,9 @@ eQuantic.Core.Api.Crud.Client eQuantic.Core.Api.Crud.Client eQuantic.Core.Api.Crud.Client - 1.7.0.0 - 1.7.0.0 - 1.7.0.0 + 1.7.1.0 + 1.7.1.0 + 1.7.1.0 net7.0;net8.0 eQuantic.Core.Api.Crud.Client diff --git a/src/Api.Crud/Api.Crud.csproj b/src/Api.Crud/Api.Crud.csproj index b09a7dc..56abbbc 100644 --- a/src/Api.Crud/Api.Crud.csproj +++ b/src/Api.Crud/Api.Crud.csproj @@ -7,9 +7,9 @@ eQuantic.Core.Api.Crud eQuantic.Core.Api.Crud eQuantic.Core.Api.Crud - 1.7.0.0 - 1.7.0.0 - 1.7.0.0 + 1.7.1.0 + 1.7.1.0 + 1.7.1.0 net7.0;net8.0 eQuantic.Core.Api.Crud diff --git a/src/Api.Crud/Extensions/HttpContextExtensions.cs b/src/Api.Crud/Extensions/HttpContextExtensions.cs index 015e1cd..7c4dc4e 100644 --- a/src/Api.Crud/Extensions/HttpContextExtensions.cs +++ b/src/Api.Crud/Extensions/HttpContextExtensions.cs @@ -5,10 +5,58 @@ namespace eQuantic.Core.Api.Crud.Extensions; internal static class HttpContextExtensions { - public static TReferenceKey GetReference(this HttpContext context, EndpointOptions options) + public static TReferenceKey? GetReference(this HttpContext context, EndpointOptions options) { var referenceName = options.ReferenceType?.GetReferenceName() ?? "referenceId"; - var referenceId = (TReferenceKey)Convert.ChangeType(context.Request.RouteValues[referenceName], typeof(TReferenceKey))!; - return referenceId; + var type = typeof(TReferenceKey); + if (!context.Request.RouteValues.TryGetValue(referenceName, out var value)) + return default; + + if (type == typeof(string)) + { + return (TReferenceKey)(object)value!.ToString()!; + } + if (type == typeof(DateTime)) + { + return (TReferenceKey)(object)Convert.ToDateTime(value); + } + if (type == typeof(DateOnly)) + { + return (TReferenceKey)(object)Convert.ToDateTime(value); + } + if (type == typeof(Guid)) + { + return (TReferenceKey)(object)Guid.Parse(value!.ToString()!); + } + if (type == typeof(short)) + { + return (TReferenceKey)(object)Convert.ToInt16(value); + } + if (type == typeof(int)) + { + return (TReferenceKey)(object)Convert.ToInt32(value); + } + if (type == typeof(long)) + { + return (TReferenceKey)(object)Convert.ToInt64(value); + } + if (type == typeof(float)) + { + return (TReferenceKey)(object)Convert.ToSingle(value); + } + if (type == typeof(double)) + { + return (TReferenceKey)(object)Convert.ToDouble(value); + } + if (type == typeof(decimal)) + { + return (TReferenceKey)(object)Convert.ToDecimal(value); + } + if (type == typeof(bool)) + { + return (TReferenceKey)(object)Convert.ToBoolean(value); + } + + throw new NotSupportedException($"Not supported entity reference type of {type.Name}"); } } \ No newline at end of file diff --git a/src/Api.Crud/Handlers/CrudEndpointHandlers.cs b/src/Api.Crud/Handlers/CrudEndpointHandlers.cs index 34e228e..c0b1d64 100644 --- a/src/Api.Crud/Handlers/CrudEndpointHandlers.cs +++ b/src/Api.Crud/Handlers/CrudEndpointHandlers.cs @@ -2,6 +2,7 @@ using eQuantic.Core.Api.Crud.Options; using eQuantic.Core.Application.Crud.Services; using eQuantic.Core.Domain.Entities.Requests; +using eQuantic.Core.Exceptions; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http.HttpResults; using Microsoft.AspNetCore.Mvc; @@ -57,6 +58,8 @@ public async Task> ReferencedCreate( [FromServices]TService service) { var referenceId = context.GetReference(_options.Create); + if (referenceId == null) + throw new InvalidEntityReferenceException(); var createRequest = new CreateRequest(referenceId, request); return await Create(createRequest, service, referenceId); } @@ -113,6 +116,8 @@ public async Task> ReferencedUpdate( [FromServices] TService service) { var referenceId = context.GetReference(_options.Update); + if (referenceId == null) + throw new InvalidEntityReferenceException(); var updateRequest = new UpdateRequest(referenceId, id, request); return await Update(updateRequest, service); } @@ -185,6 +190,8 @@ public async Task> ReferencedDelete( [FromServices] TService service) { var referenceId = context.GetReference(_options.Delete); + if (referenceId == null) + throw new InvalidEntityReferenceException(); var request = new ItemRequest(referenceId, id); return await Delete(request, service); } diff --git a/src/Api.Crud/Handlers/ReaderEndpointHandlers.cs b/src/Api.Crud/Handlers/ReaderEndpointHandlers.cs index b849e4f..a36b6e1 100644 --- a/src/Api.Crud/Handlers/ReaderEndpointHandlers.cs +++ b/src/Api.Crud/Handlers/ReaderEndpointHandlers.cs @@ -4,6 +4,7 @@ using eQuantic.Core.Application.Crud.Services; using eQuantic.Core.Application.Entities.Results; using eQuantic.Core.Domain.Entities.Requests; +using eQuantic.Core.Exceptions; using eQuantic.Linq.Filter; using eQuantic.Linq.Sorter; using Microsoft.AspNetCore.Http; @@ -40,6 +41,9 @@ public async Task, NotFound>> GetReferencedById(_options.Get); + if (referenceId == null) + throw new InvalidEntityReferenceException(); + var request = new ItemRequest(referenceId, id); return await GetById(request, service); } @@ -115,6 +119,8 @@ public async Task>> GetReferencedPagedList(_options.List); + if (referenceId == null) + throw new InvalidEntityReferenceException(); var request = new PagedListRequest(referenceId, pageIndex, pageSize, filterBy, orderBy); return await GetPagedList(request, service); } diff --git a/src/Application.Crud/Application.Crud.csproj b/src/Application.Crud/Application.Crud.csproj index 4edf3fc..6114b36 100644 --- a/src/Application.Crud/Application.Crud.csproj +++ b/src/Application.Crud/Application.Crud.csproj @@ -7,9 +7,9 @@ eQuantic.Core.Application.Crud eQuantic.Core.Application.Crud eQuantic.Core.Application.Crud - 1.7.0.0 - 1.7.0.0 - 1.7.0.0 + 1.7.1.0 + 1.7.1.0 + 1.7.1.0 netstandard2.1;net7.0;net8.0 eQuantic.Core.Application.Crud