-
Notifications
You must be signed in to change notification settings - Fork 2
/
FluentContextPostOnlyEntity.cs
69 lines (58 loc) · 2.63 KB
/
FluentContextPostOnlyEntity.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
67
68
69
// Licensed to Finnovation Labs Limited under one or more agreements.
// Finnovation Labs Limited licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using FinnovationLabs.OpenBanking.Library.Connector.Models.Validation;
using FinnovationLabs.OpenBanking.Library.Connector.Persistence;
using FluentValidation.Results;
namespace FinnovationLabs.OpenBanking.Library.Connector.Fluent
{
internal class
FluentContextPostOnlyEntity<TPostOnly, TPublicRequest, TPublicResponse>
: IFluentContextPostOnlyEntity<TPublicRequest, TPublicResponse>
where TPostOnly : class, IPostOnlyWithPublicInterface<TPostOnly, TPublicRequest, TPublicResponse>, new()
where TPublicRequest : class // required by IPostOnlyWithPublicInterface
where TPublicResponse : class // required by IPostOnlyWithPublicInterface
{
protected readonly ISharedContext _context;
internal FluentContextPostOnlyEntity(ISharedContext context)
{
_context = context;
}
public async Task<FluentResponse<TPublicResponse>> PostAsync(TPublicRequest publicRequest, string? createdBy)
{
publicRequest.ArgNotNull(nameof(publicRequest));
IList<FluentResponseMessage> validationErrors = Validate(publicRequest);
if (validationErrors.Count > 0)
{
return new FluentResponse<TPublicResponse>(messages: validationErrors, data: null);
}
try
{
TPublicResponse resp = await new TPostOnly().PostEntityAsyncWrapper(
arg1: _context,
arg2: publicRequest,
arg3: createdBy);
return new FluentResponse<TPublicResponse>(resp);
}
catch (AggregateException ex)
{
_context.Instrumentation.Exception(ex);
return new FluentResponse<TPublicResponse>(messages: ex.CreateErrorMessages(), data: null);
}
catch (Exception ex)
{
_context.Instrumentation.Exception(ex);
return new FluentResponse<TPublicResponse>(message: ex.CreateErrorMessage(), data: null);
}
}
private static IList<FluentResponseMessage> Validate(TPublicRequest requestObject)
{
ValidationResult validationResult =
new TPostOnly().ValidatePublicRequestWrapper(requestObject);
return validationResult.GetOpenBankingResponses();
}
}
}