Skip to content

Commit

Permalink
fix: Reference type conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
edgarmesquita committed May 31, 2024
1 parent e7acf88 commit 7ed415f
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/Api.Crud.Client/Api.Crud.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<RootNamespace>eQuantic.Core.Api.Crud.Client</RootNamespace>
<AssemblyName>eQuantic.Core.Api.Crud.Client</AssemblyName>
<AssemblyTitle>eQuantic.Core.Api.Crud.Client</AssemblyTitle>
<AssemblyVersion>1.7.0.0</AssemblyVersion>
<FileVersion>1.7.0.0</FileVersion>
<Version>1.7.0.0</Version>
<AssemblyVersion>1.7.1.0</AssemblyVersion>
<FileVersion>1.7.1.0</FileVersion>
<Version>1.7.1.0</Version>
<TargetFrameworks>net7.0;net8.0</TargetFrameworks>

<PackageId>eQuantic.Core.Api.Crud.Client</PackageId>
Expand Down
6 changes: 3 additions & 3 deletions src/Api.Crud/Api.Crud.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<AssemblyName>eQuantic.Core.Api.Crud</AssemblyName>
<RootNamespace>eQuantic.Core.Api.Crud</RootNamespace>
<AssemblyTitle>eQuantic.Core.Api.Crud</AssemblyTitle>
<AssemblyVersion>1.7.0.0</AssemblyVersion>
<FileVersion>1.7.0.0</FileVersion>
<Version>1.7.0.0</Version>
<AssemblyVersion>1.7.1.0</AssemblyVersion>
<FileVersion>1.7.1.0</FileVersion>
<Version>1.7.1.0</Version>
<TargetFrameworks>net7.0;net8.0</TargetFrameworks>

<PackageId>eQuantic.Core.Api.Crud</PackageId>
Expand Down
54 changes: 51 additions & 3 deletions src/Api.Crud/Extensions/HttpContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,58 @@ namespace eQuantic.Core.Api.Crud.Extensions;

internal static class HttpContextExtensions
{
public static TReferenceKey GetReference<TReferenceKey>(this HttpContext context, EndpointOptions options)
public static TReferenceKey? GetReference<TReferenceKey>(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}");
}
}
7 changes: 7 additions & 0 deletions src/Api.Crud/Handlers/CrudEndpointHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -57,6 +58,8 @@ public async Task<CreatedAtRoute<TKey>> ReferencedCreate<TReferenceKey>(
[FromServices]TService service)
{
var referenceId = context.GetReference<TReferenceKey>(_options.Create);
if (referenceId == null)
throw new InvalidEntityReferenceException<TReferenceKey>();
var createRequest = new CreateRequest<TRequest, TReferenceKey>(referenceId, request);
return await Create(createRequest, service, referenceId);
}
Expand Down Expand Up @@ -113,6 +116,8 @@ public async Task<Results<Ok, BadRequest>> ReferencedUpdate<TReferenceKey>(
[FromServices] TService service)
{
var referenceId = context.GetReference<TReferenceKey>(_options.Update);
if (referenceId == null)
throw new InvalidEntityReferenceException<TReferenceKey>();
var updateRequest = new UpdateRequest<TRequest, TKey, TReferenceKey>(referenceId, id, request);
return await Update(updateRequest, service);
}
Expand Down Expand Up @@ -185,6 +190,8 @@ public async Task<Results<Ok, BadRequest>> ReferencedDelete<TReferenceKey>(
[FromServices] TService service)
{
var referenceId = context.GetReference<TReferenceKey>(_options.Delete);
if (referenceId == null)
throw new InvalidEntityReferenceException<TReferenceKey>();
var request = new ItemRequest<TKey, TReferenceKey>(referenceId, id);
return await Delete(request, service);
}
Expand Down
6 changes: 6 additions & 0 deletions src/Api.Crud/Handlers/ReaderEndpointHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -40,6 +41,9 @@ public async Task<Results<Ok<TEntity>, NotFound>> GetReferencedById<TReferenceKe
[FromServices]TService service)
{
var referenceId = context.GetReference<TReferenceKey>(_options.Get);
if (referenceId == null)
throw new InvalidEntityReferenceException<TReferenceKey>();

var request = new ItemRequest<TKey, TReferenceKey>(referenceId, id);
return await GetById(request, service);
}
Expand Down Expand Up @@ -115,6 +119,8 @@ public async Task<Ok<PagedListResult<TEntity>>> GetReferencedPagedList<TReferenc
[FromServices]TService service)
{
var referenceId = context.GetReference<TReferenceKey>(_options.List);
if (referenceId == null)
throw new InvalidEntityReferenceException<TReferenceKey>();
var request = new PagedListRequest<TEntity,TReferenceKey>(referenceId, pageIndex, pageSize, filterBy, orderBy);
return await GetPagedList(request, service);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Application.Crud/Application.Crud.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<RootNamespace>eQuantic.Core.Application.Crud</RootNamespace>
<AssemblyName>eQuantic.Core.Application.Crud</AssemblyName>
<AssemblyTitle>eQuantic.Core.Application.Crud</AssemblyTitle>
<AssemblyVersion>1.7.0.0</AssemblyVersion>
<FileVersion>1.7.0.0</FileVersion>
<Version>1.7.0.0</Version>
<AssemblyVersion>1.7.1.0</AssemblyVersion>
<FileVersion>1.7.1.0</FileVersion>
<Version>1.7.1.0</Version>
<TargetFrameworks>netstandard2.1;net7.0;net8.0</TargetFrameworks>

<PackageId>eQuantic.Core.Application.Crud</PackageId>
Expand Down

0 comments on commit 7ed415f

Please sign in to comment.