Skip to content

Commit

Permalink
Added BaseODataController
Browse files Browse the repository at this point in the history
  • Loading branch information
gordon-matt committed Apr 2, 2022
1 parent b210490 commit 10df0f3
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Demo.Extenso.AspNetCore.Mvc.OData.Controllers.Api
{
public class PersonApiController : GenericODataController<Person, int>
public class PersonApiController : BaseODataController<Person, int>
{
public PersonApiController(IRepository<Person> repository)
: base(repository)
Expand Down
4 changes: 2 additions & 2 deletions Demos/Demo.Data.InfoSchema/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private void cmbDatabase_SelectedIndexChanged(object sender, EventArgs e)
break;

case DataSource.MySql:
lbTables.DataSource = (connection as MySqlConnection).GetTableNames();
lbTables.DataSource = (connection as MySqlConnection).GetTableNames(includeViews: true);
lbTables.Select();
break;

Expand All @@ -105,7 +105,7 @@ private void cmbSchema_SelectedIndexChanged(object sender, EventArgs e)
switch (SelectedDataSource)
{
case DataSource.SqlServer: lbTables.DataSource = (connection as SqlConnection).GetTableNames(includeViews: true, SelectedSchema); break;
case DataSource.MySql: lbTables.DataSource = (connection as MySqlConnection).GetTableNames(); break;
case DataSource.MySql: lbTables.DataSource = (connection as MySqlConnection).GetTableNames(includeViews: true); break;
case DataSource.PostgreSql: lbTables.DataSource = (connection as NpgsqlConnection).GetTableNames(includeViews: true, SelectedSchema); break;
default: break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Demo.Extenso.AspNetCore.Blazor.OData.Controllers.Api
{
public class PersonApiController : GenericODataController<Person, int>
public class PersonApiController : BaseODataController<Person, int>
{
public PersonApiController(IRepository<Person> repository)
: base(repository)
Expand Down
2 changes: 1 addition & 1 deletion Extenso.AspNetCore.OData/Extenso.AspNetCore.OData.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<NeutralLanguage>en-US</NeutralLanguage>
<PackageLicenseUrl>https://raw.githubusercontent.com/gordon-matt/Extenso/master/LICENSE.txt</PackageLicenseUrl>
<PackageProjectUrl>https://github.com/gordon-matt/Extenso</PackageProjectUrl>
<Version>6.0.1</Version>
<Version>6.0.2</Version>
<Description></Description>
</PropertyGroup>

Expand Down
45 changes: 42 additions & 3 deletions Extenso.AspNetCore.OData/GenericODataController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.AspNetCore.OData.Deltas;
using Microsoft.AspNetCore.OData.Formatter;
using Microsoft.AspNetCore.OData.Query;
using Microsoft.AspNetCore.OData.Results;
using Microsoft.AspNetCore.OData.Routing.Controllers;
using Microsoft.EntityFrameworkCore;

Expand All @@ -17,6 +18,7 @@ namespace Extenso.AspNetCore.OData
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <typeparam name="TKey"></typeparam>
[Obsolete("Use BaseODataController instead")]
public abstract class GenericODataController<TEntity, TKey> : ODataController, IDisposable
where TEntity : class
{
Expand Down Expand Up @@ -90,7 +92,7 @@ public virtual async Task<IActionResult> Get(ODataQueryOptions<TEntity> options)
// NOTE: Change due to: https://github.com/OData/WebApi/issues/1235
var connection = GetDisposableConnection();
var query = connection.Query();
query = ApplyMandatoryFilter(query);
query = await ApplyMandatoryFilterAsync(query);
var results = options.ApplyTo(query, IgnoreQueryOptions);
return Ok(results);
}
Expand Down Expand Up @@ -303,10 +305,10 @@ protected virtual bool EntityExists(TKey key)
/// </summary>
/// <param name="query">The System.Linq.IQueryable`1 upon which to apply the filter.</param>
/// <returns>A System.Linq.IQueryable`1 that may have had filters applied.</returns>
protected virtual IQueryable<TEntity> ApplyMandatoryFilter(IQueryable<TEntity> query)
protected virtual async Task<IQueryable<TEntity>> ApplyMandatoryFilterAsync(IQueryable<TEntity> query)
{
// Do nothing, by default
return query;
return await Task.FromResult(query);
}

/// <summary>
Expand Down Expand Up @@ -421,4 +423,41 @@ public void Dispose()

#endregion IDisposable Support
}

/// <summary>
/// A generic, abstract CRUD controller for OData, with support for checking policy based permissions for users.
/// Get(TKey) in BaseODataController allows for OData query options, such as $expand, whereas GenericODataController does not
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <typeparam name="TKey"></typeparam>
public abstract class BaseODataController<TEntity, TKey> : GenericODataController<TEntity, TKey>
where TEntity : BaseEntity<TKey>
{
protected BaseODataController(IRepository<TEntity> repository)
: base(repository)
{
}

[EnableQuery]
public override async Task<IActionResult> Get([FromODataUri] TKey key)
{
var connection = GetDisposableConnection();
var query = connection.Query(x => x.Id.Equals(key));
query = await ApplyMandatoryFilterAsync(query);
var result = SingleResult.Create(query);

var entity = result.Queryable.FirstOrDefault();
if (entity == null)
{
return NotFound();
}

if (!await CanViewEntity(entity))
{
return Unauthorized();
}

return Ok(result);
}
}
}

0 comments on commit 10df0f3

Please sign in to comment.