Skip to content

Commit

Permalink
[Joker.OData] - OData 8.x migration
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasfabian committed Feb 3, 2024
1 parent 236fa92 commit 2d75cbb
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 80 deletions.
44 changes: 23 additions & 21 deletions Joker.OData/Controllers/ODataControllerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.AspNetCore.OData;
using Microsoft.AspNetCore.OData.Deltas;
using Microsoft.AspNetCore.OData.Extensions;
using Microsoft.OData.UriParser;

namespace Joker.OData.Controllers
{
Expand All @@ -34,7 +35,7 @@ protected ODataControllerBase(IRepository<TEntity> repository)

#region Post

public async Task<IActionResult> Post([FromBody]TEntity entity)
public async Task<IActionResult> Post([FromBody] TEntity entity)
{
var validationResult = await ValidatePostAsync(entity);

Expand All @@ -45,7 +46,7 @@ public async Task<IActionResult> Post([FromBody]TEntity entity)

if (actionResult != null)
return actionResult;

return Created(entity);
}

Expand All @@ -71,14 +72,14 @@ protected virtual async Task<IActionResult> OnPostAsync(TEntity entity)
public async Task<IActionResult> Patch(object key, Delta<TEntity> delta)
{
var keys = GetKeysFromPath();

var entityToUpdate = repository.GetAll().FirstOrDefault(CreateKeysPredicate(keys));

if (entityToUpdate == null)
return NotFound();

delta.Patch(entityToUpdate);

var validationResult = await ValidatePatchAsync(delta, entityToUpdate);

if (validationResult != null)
Expand All @@ -91,7 +92,7 @@ public async Task<IActionResult> Patch(object key, Delta<TEntity> delta)

return Updated(entityToUpdate);
}

protected virtual async Task<IActionResult> OnPatchAsync(Delta<TEntity> delta, TEntity patchedEntity)
{
repository.Update(patchedEntity);
Expand All @@ -111,15 +112,15 @@ protected virtual Task<IActionResult> ValidatePatchAsync(Delta<TEntity> delta, T
#region Put

[HttpPut]
public async Task<IActionResult> Put(object key, [FromBody]TEntity entity)
public async Task<IActionResult> Put(object key, [FromBody] TEntity entity)
{
var validationResult = await ValidatePutAsync(entity);

if (validationResult != null)
return validationResult;

var actionResult = await OnPutAsync(entity);

if (actionResult != null)
return actionResult;

Expand Down Expand Up @@ -153,7 +154,7 @@ public async Task<IActionResult> Delete(object key, ODataOptions oDataOptions)
public async Task<IActionResult> Delete()
{
var keys = GetKeysFromPath();

var validationResult = await ValidateDeleteAsync(keys);

if (validationResult != null)
Expand All @@ -168,7 +169,7 @@ public async Task<IActionResult> Delete()
}

protected virtual async Task<IActionResult> OnDeleteAsync(params object[] keys)
{
{
repository.Remove(keys);

await repository.SaveChangesAsync();
Expand All @@ -191,11 +192,11 @@ protected virtual dynamic TryGetDbSet(Type entityType)
}

#endregion

#region CreateRef

[AcceptVerbs("POST", "PUT")]
public async Task<IActionResult> CreateRef(string navigationProperty, [FromBody] Uri link)
public async Task<IActionResult> CreateRef(object key, string navigationProperty, [FromBody] Uri link)
{
return await OnCreateRef(navigationProperty, link);
}
Expand All @@ -209,16 +210,17 @@ protected virtual async Task<IActionResult> OnCreateRef(string navigationPropert
if (entity == null)
return NotFound($"{nameof(TEntity)}: {keys}");

var odataPath = Request.ODataFeature().Path;
var edmModel = Request.GetModel();

var baseAddress = Request.ODataFeature().BaseAddress;
var oDataUriParser = new ODataUriParser(edmModel, new Uri(baseAddress), link);
var odataPath = oDataUriParser.ParsePath();

var relatedObjectKeys = GetKeysFromPath(odataPath);

var type = typeof(TEntity).GetProperty(navigationProperty).PropertyType;

var navigationPropertyType = type.GetCollectionGenericType();

if (navigationPropertyType == null)
navigationPropertyType = type;
var navigationPropertyType = type.GetCollectionGenericType() ?? type;

dynamic relatedRepository = TryGetDbSet(navigationPropertyType);
dynamic relatedEntity = relatedRepository.Find(relatedObjectKeys);
Expand All @@ -234,7 +236,7 @@ protected virtual async Task<IActionResult> OnCreateRef(string navigationPropert

await repository.SaveChangesAsync();

return StatusCode((int) HttpStatusCode.NoContent);
return StatusCode((int)HttpStatusCode.NoContent);
}

#endregion
Expand All @@ -248,14 +250,14 @@ public async Task<IActionResult> DeleteRef(object key, string navigationProperty
}

protected virtual async Task<IActionResult> OnDeleteRef(string navigationProperty)
{
{
var keys = GetKeysFromPath();
var keyPredicate = CreateKeysPredicate(keys);
var entity = repository.GetAllIncluding(navigationProperty).Where(keyPredicate).FirstOrDefault();

if (entity == null)
return NotFound($"{nameof(TEntity)}: {keys}");

var type = typeof(TEntity).GetProperty(navigationProperty).PropertyType;

var navigationPropertyType = type.GetCollectionGenericType();
Expand All @@ -265,7 +267,7 @@ protected virtual async Task<IActionResult> OnDeleteRef(string navigationPropert

if (typeof(ICollection).IsAssignableFrom(type))
{
var relatedObjectKeys = GetAllKeysFromPath().Last().Select(c => c.Value).ToArray();
var relatedObjectKeys = GetAllKeysFromPath();

dynamic relatedRepository = TryGetDbSet(navigationPropertyType);
dynamic relatedEntity = relatedRepository.Find(relatedObjectKeys);
Expand Down
26 changes: 9 additions & 17 deletions Joker.OData/Controllers/ReadOnlyODataController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.AspNetCore.OData.Query.Validator;
using Microsoft.AspNetCore.OData.Results;
using Microsoft.AspNetCore.OData.Routing.Controllers;
using Microsoft.OData.Edm;
using Microsoft.OData.UriParser;

namespace Joker.OData.Controllers
Expand Down Expand Up @@ -86,13 +87,6 @@ protected virtual Expression<Func<TEntity, bool>> CreateKeysPredicate(params obj

#endregion

private bool HasKeyInODataPath()
{
var odataPath = Request.ODataFeature().Path;

return odataPath.OfType<KeySegment>().Any();
}

#region GetKeysFromPath

protected object[] GetKeysFromPath()
Expand All @@ -115,17 +109,15 @@ protected object[] GetKeysFromPath(ODataPath odataPath)
return value;
}

protected IEnumerable<IEnumerable<KeyValuePair<string, object>>> GetAllKeysFromPath()
protected object[] GetAllKeysFromPath()
{
var odataPath = Request.ODataFeature().Path;

var keySegment = odataPath.OfType<KeySegment>();
if (keySegment == null)
throw new InvalidOperationException("The link does not contain a key.");

var keys = keySegment.Select(c => c.Keys);

return keys;
var link = Request.Query["$id"];
var oDataFeature = Request.ODataFeature();
var uriBuilder = new UriBuilder(Request.Scheme, Request.Host.Host, Request.Host.Port ?? -1); //TODO: get relativeUri differently
var baseAddress = uriBuilder.Uri + oDataFeature.RoutePrefix;
var oDataUriParser = new ODataUriParser(Request.GetModel(), new Uri(baseAddress), new Uri(link));
var odataPath = oDataUriParser.ParsePath();
return GetKeysFromPath(odataPath);
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.AspNetCore.OData.Routing.Conventions;
using Microsoft.OData.Edm;
using Microsoft.OData.ModelBuilder;

namespace Joker.OData.Routing.Conventions
{
Expand Down
19 changes: 0 additions & 19 deletions Joker.OData/Startup/ODataStartup.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.OData;
Expand Down Expand Up @@ -93,24 +92,6 @@ protected override void OnRegisterMiddleWares(IApplicationBuilder app)

protected override void OnConfigureOData(IApplicationBuilder app)
{
// app.UseEndpoints(endpoints =>
// {
// endpoints.EnableDependencyInjection();
//
// endpoints.Select().Expand().Filter().OrderBy().MaxTop(null).Count();

// var edmModel = app.ApplicationServices.GetService<IEdmModel>() ?? EdmModel;

// if (ODataStartupSettings.EnableODataBatchHandler)
// {
// endpoints.EnableContinueOnErrorHeader();

// endpoints.MapODataRoute(ODataStartupSettings.ODataRouteName, ODataStartupSettings.ODataRoutePrefix, edmModel,
// CreateODataBatchHandler());
// }
// else
// endpoints.MapODataRoute(ODataStartupSettings.ODataRouteName, ODataStartupSettings.ODataRoutePrefix, edmModel);
// });
}

#endregion
Expand Down
15 changes: 3 additions & 12 deletions Joker.OData/Startup/ODataStartupBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,19 @@
using Microsoft.AspNetCore.OData;
namespace Joker.OData.Startup
{
public abstract class ODataStartupBase : StartupBase
public abstract class ODataStartupBase(IWebHostEnvironment env) : StartupBase(env)
{
#region Fields

internal readonly ODataStartupSettings ODataStartupSettings = new ODataStartupSettings();

#endregion

#region Constructors

protected ODataStartupBase(IWebHostEnvironment env)
: base(env)
{
}
internal readonly ODataStartupSettings ODataStartupSettings = new();

#endregion

#region Properties

private IEdmModel edmModel;

public IEdmModel EdmModel => edmModel ?? (edmModel = CreateEdmModel());
public IEdmModel EdmModel => edmModel ??= CreateEdmModel();

#endregion

Expand Down
9 changes: 0 additions & 9 deletions Joker.OData/Startup/ODataStartupSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ public class ODataStartupSettings
{
public bool EnableODataBatchHandler { get; private set; } = true;

// public string ODataRouteName { get; private set; } = "odata";

public string ODataRoutePrefix { get; private set; } = "odata";

public ODataStartupSettings DisableODataBatchHandler()
Expand All @@ -15,13 +13,6 @@ public ODataStartupSettings DisableODataBatchHandler()
return this;
}

// public ODataStartupSettings SetODataRouteName(string value)
// {
// ODataRouteName = value;
//
// return this;
// }

public ODataStartupSettings SetODataRoutePrefix(string value)
{
ODataRoutePrefix = value;
Expand Down

0 comments on commit 2d75cbb

Please sign in to comment.