-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathWalletEndpoints.cs
More file actions
79 lines (66 loc) · 2.6 KB
/
WalletEndpoints.cs
File metadata and controls
79 lines (66 loc) · 2.6 KB
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
70
71
72
73
74
75
76
77
78
79
using Microsoft.AspNetCore.Mvc;
using Train.Solver.Common.Enums;
using Train.Solver.Common.Extensions;
using Train.Solver.Data.Abstractions.Models;
using Train.Solver.Data.Abstractions.Repositories;
using Train.Solver.Infrastructure.Abstractions;
using Train.Solver.Infrastructure.Abstractions.Models;
using Train.Solver.Infrastructure.Extensions;
namespace Train.Solver.AdminAPI.Endpoints;
public static class WalletEndpoints
{
public static RouteGroupBuilder MapWalletEndpoints(this RouteGroupBuilder group)
{
group.MapGet("/wallets", GetAllAsync)
.Produces<IEnumerable<WalletDto>>();
group.MapPost("/wallets", CreateAsync)
.Produces(StatusCodes.Status200OK)
.Produces(StatusCodes.Status400BadRequest);
group.MapPut("/wallets/{networkType}/{address}", UpdateAsync)
.Produces(StatusCodes.Status200OK)
.Produces(StatusCodes.Status404NotFound);
return group;
}
private static async Task<IResult> GetAllAsync(
IWalletRepository repository,
[FromQuery] NetworkType[]? types)
{
var wallets = await repository.GetAllAsync(types.IsNullOrEmpty() ? null : types);
return Results.Ok(wallets.Select(x => x.ToDto()));
}
private static async Task<IResult> CreateAsync(
IWalletRepository repository,
ISignerAgentRepository signerAgentRepository,
IPrivateKeyProvider privateKeyProvider,
[FromBody] CreateWalletRequest request)
{
var signerAgent = await signerAgentRepository.GetAsync(request.SignerAgent);
if (signerAgent == null)
{
return Results.BadRequest("Invalid signer agent");
}
if (!signerAgent.SupportedTypes.Contains(request.NetworkType))
{
return Results.BadRequest("Invalid network type");
}
var generatedAddress = await privateKeyProvider.GenerateAsync(signerAgent.Url, request.NetworkType);
var wallet = await repository.CreateAsync(generatedAddress, request);
return wallet is null
? Results.BadRequest("Could not create wallet")
: Results.Ok();
}
private static async Task<IResult> UpdateAsync(
IWalletRepository repository,
NetworkType networkType,
string address,
[FromBody] UpdateWalletRequest request)
{
var wallet = await repository.UpdateAsync(
networkType,
address,
request);
return wallet is null
? Results.NotFound($"Trusted wallet '{address}' not found on network '{networkType}'")
: Results.Ok();
}
}