-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathAccountData.cs
66 lines (57 loc) · 3.11 KB
/
AccountData.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using Microsoft.Azure.Cosmos.Linq;
using System.Text;
namespace Cdr.Banking.Business.Data;
public partial class AccountData
{
private static readonly QueryArgsConfig _config = QueryArgsConfig.Create()
.WithFilter(filter => filter
.AddReferenceDataField<OpenStatus>(nameof(Model.Account.OpenStatus), c => c.WithValue(os => os == OpenStatus.All ? throw new FormatException("Value not valid for filtering.") : os))
.AddReferenceDataField<ProductCategory>(nameof(Model.Account.ProductCategory))
.AddField<bool>(nameof(Model.Account.IsOwned)));
/// <summary>
/// Initializes a new instance of the <see cref="AccountData"/> class setting the required internal configurations.
/// </summary>
partial void AccountDataCtor()
{
_getAccountsOnQuery = GetAccountsOnQuery; // Wire up the plug-in to enable filtering.
_getAccountsQueryOnQuery = (q, args) => q.Where(_config, args).OrderBy(x => x.Id); // Wire up the OData-like query syntax.
}
/// <summary>
/// Perform the query filering for the GetAccounts.
/// </summary>
private IQueryable<Model.Account> GetAccountsOnQuery(IQueryable<Model.Account> query, AccountArgs? args)
{
var q = query.OrderBy(x => x.Id).AsQueryable();
if (args == null || args.IsInitial)
return q;
// Where an argument value has been specified then add as a filter - the WhereWhen and WhereWith are enabled by CoreEx.
q = q.WhereWhen(!(args.OpenStatus == null) && args.OpenStatus != OpenStatus.All, x => x.OpenStatus == args.OpenStatus!.Code);
q = q.WhereWith(args?.ProductCategory, x => x.ProductCategory == args!.ProductCategory!.Code);
// With checking IsOwned a simple false check cannot be performed with Cosmos; assume "not IsDefined" is equivalent to false also.
if (args!.IsOwned == null)
return q;
if (args.IsOwned == true)
return q.Where(x => x.IsOwned == true);
else
return q.Where(x => !x.IsOwned.IsDefined() || !x.IsOwned);
}
/// <summary>
/// Gets the balance for the specified account.
/// </summary>
private Task<Result<Balance?>> GetBalanceOnImplementationAsync(string? accountId)
{
// Use the 'Account' model and select for the specified id to access the balance property.
return Result.GoAsync(_cosmos.Accounts.Model.Query(q => q.Where(x => x.Id == accountId)).SelectFirstOrDefaultWithResultAsync())
.WhenAs(a => a is not null, a =>
{
var bal = _cosmos.Mapper.Map<Model.Balance, Balance>(a!.Balance);
return bal.Adjust(b => b.Id = a.Id);
});
}
/// <summary>
/// Gets the statement (file) for the specified account.
/// </summary>
private Task<Result<FileContentResult?>> GetStatementOnImplementationAsync(string? accountId)
=> Result.GoAsync(GetDetailAsync(accountId))
.WhenAs(d => d is not null, d => new FileContentResult(Encoding.UTF8.GetBytes($"Statement for Account '{d.AccountNumber}'."), "text/plain") { FileDownloadName = $"{accountId}.statement.txt" });
}