Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 24 additions & 15 deletions src/Gemstone.Web/APIController/ReadOnlyModelController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,20 @@

private readonly AdoDataConnection m_connection;

private ConnectionCache()
private ConnectionCache(ReadOnlyModelController<T> controller)
{
m_connection = new AdoDataConnection(Settings.Default);
Table = new TableOperations<T>(m_connection);
Table = controller.CreateTableOperation(m_connection);
}

public void Dispose()
{
m_connection.Dispose();
}

public static ConnectionCache Create(double expiration)
public static ConnectionCache Create(ReadOnlyModelController<T> controller, double expiration)
{
ConnectionCache cache = new();
ConnectionCache cache = new(controller);

MemoryCache<ConnectionCache>.GetOrAdd(cache.Token, expiration, () => cache, Dispose);

Expand Down Expand Up @@ -175,7 +175,7 @@
[HttpGet, Route("Open/{filterExpression}/{parameters}/{expiration:double?}")]
public Task<IActionResult> Open(string? filterExpression, object?[] parameters, double? expiration, CancellationToken cancellationToken)
{
ConnectionCache cache = ConnectionCache.Create(expiration ?? 1.0D);
ConnectionCache cache = ConnectionCache.Create(this, expiration ?? 1.0D);

cache.Records = cache.Table.QueryRecordsWhereAsync(filterExpression, cancellationToken, parameters).GetAsyncEnumerator(cancellationToken);

Expand Down Expand Up @@ -237,7 +237,7 @@
public virtual async Task<IActionResult> Get(string? parentID, int page, CancellationToken cancellationToken)
{
await using AdoDataConnection connection = CreateConnection();
TableOperations<T> tableOperations = new(connection);
TableOperations<T> tableOperations = CreateTableOperation(connection);
RecordFilter<T>? filter = null;

if (ParentKey != string.Empty && parentID is not null)
Expand Down Expand Up @@ -267,7 +267,7 @@
public virtual async Task<IActionResult> Get(string sort, bool ascending, int page, CancellationToken cancellationToken)
{
await using AdoDataConnection connection = CreateConnection();
TableOperations<T> tableOperations = new(connection);
TableOperations<T> tableOperations = CreateTableOperation(connection);
RecordFilter<T>? filter = null;

IAsyncEnumerable<T> result = tableOperations.QueryRecordsAsync(sort, ascending, page, PageSize, cancellationToken, filter);
Expand All @@ -288,7 +288,7 @@
public virtual async Task<IActionResult> Get(string parentID, string sort, bool ascending, int page, CancellationToken cancellationToken)
{
await using AdoDataConnection connection = CreateConnection();
TableOperations<T> tableOperations = new(connection);
TableOperations<T> tableOperations = CreateTableOperation(connection);
RecordFilter<T> filter = new()
{
FieldName = ParentKey,
Expand All @@ -306,12 +306,12 @@
/// </summary>
/// <param name="id">The PrimaryKey value of the Model to be returned.</param>
/// <param name="cancellationToken">Propagates notification that operations should be canceled.</param>
/// <returns>An <see cref="IActionResult"/> containing a <see cref="T"/> or <see cref="Exception"/>.</returns>

Check warning on line 309 in src/Gemstone.Web/APIController/ReadOnlyModelController.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

XML comment has cref attribute 'T' that refers to a type parameter
[HttpGet, Route("One/{id}")]
public virtual async Task<IActionResult> GetOne(string id, CancellationToken cancellationToken)
{
await using AdoDataConnection connection = CreateConnection();
TableOperations<T> tableOperations = new(connection);
TableOperations<T> tableOperations = CreateTableOperation(connection);
T? result = await tableOperations.QueryRecordAsync(new RecordRestriction($"{PrimaryKeyField} = {{0}}", id), cancellationToken).ConfigureAwait(false);

return result is null ?
Expand All @@ -332,7 +332,7 @@
public virtual async Task<IActionResult> Search([FromBody] SearchPost<T> postData, int page, string? parentID, CancellationToken cancellationToken)
{
await using AdoDataConnection connection = CreateConnection();
TableOperations<T> tableOperations = new(connection);
TableOperations<T> tableOperations = CreateTableOperation(connection);
RecordFilter<T>[] filters = postData.Searches.ToArray();

if (ParentKey != string.Empty && parentID is not null)
Expand All @@ -359,10 +359,10 @@
/// <returns>A <see cref="PageInfo"/> object containing the pagination information or <see cref="Exception"/>.</returns>
[HttpPost, Route("PageInfo/{parentID?}")]
[ResourceAccess(ResourceAccessType.Read)]
public virtual async Task<IActionResult> GetPageInfo(SearchPost<T> postData, string? parentID, CancellationToken cancellationToken)
public virtual async Task<IActionResult> GetPageInfo([FromBody] SearchPost<T> postData, string? parentID, CancellationToken cancellationToken)
{
await using AdoDataConnection connection = CreateConnection();
TableOperations<T> tableOperations = new(connection);
TableOperations<T> tableOperations = CreateTableOperation(connection);
RecordFilter<T>[] filters = postData.Searches.ToArray();

if (ParentKey != string.Empty && parentID is not null)
Expand Down Expand Up @@ -396,7 +396,7 @@
public virtual async Task<IActionResult> GetPageInfo(string? parentID, CancellationToken cancellationToken)
{
await using AdoDataConnection connection = CreateConnection();
TableOperations<T> tableOperations = new(connection);
TableOperations<T> tableOperations = CreateTableOperation(connection);
RecordFilter<T>[] filters = [];

if (ParentKey != string.Empty && parentID is not null)
Expand All @@ -423,12 +423,12 @@
/// Gets a new record.
/// </summary>
/// <param name="cancellationToken">Propagates notification that operations should be canceled.</param>
/// <returns>A <see cref="T"/> object or <see cref="Exception"/>.</returns>

Check warning on line 426 in src/Gemstone.Web/APIController/ReadOnlyModelController.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

XML comment has cref attribute 'T' that refers to a type parameter
[HttpGet, Route("New")]
public virtual async Task<IActionResult> New(CancellationToken cancellationToken)
{
await using AdoDataConnection connection = CreateConnection();
TableOperations<T> tableOperations = new(connection);
TableOperations<T> tableOperations = CreateTableOperation(connection);

T? result = tableOperations.NewRecord();
return Ok(result);
Expand All @@ -450,7 +450,7 @@

// Create a connection and table operations instance
await using AdoDataConnection connection = CreateConnection();
TableOperations<T> tableOperations = new(connection);
TableOperations<T> tableOperations = CreateTableOperation(connection);
string tableName = tableOperations.TableName;
string sql = $"SELECT MAX([{fieldName}]) FROM [{tableName}]";

Expand All @@ -467,5 +467,14 @@
{
return new AdoDataConnection(Settings.Default);
}

/// <summary>
/// Creates any <see cref="TableOperations{T}"/> needed by the controller.
/// </summary>
/// <returns>A new <see cref="TableOperations{T}"/>.</returns>
protected virtual TableOperations<T> CreateTableOperation(AdoDataConnection connection)
{
return new TableOperations<T>(connection);
}
}
}
Loading