-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShipController.cs
55 lines (46 loc) · 1.88 KB
/
ShipController.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
using System.Linq;
using System.Threading.Tasks;
using CoreDdd.Commands;
using CoreDdd.Queries;
using CoreDddSampleWebAppCommon.Commands;
using CoreDddSampleWebAppCommon.Dtos;
using CoreDddSampleWebAppCommon.Queries;
using Microsoft.AspNetCore.Mvc;
namespace CoreDddSampleAspNetCoreWebApp.Controllers
{
public class ShipController : Controller
{
private readonly ICommandExecutor _commandExecutor;
private readonly IQueryExecutor _queryExecutor;
public ShipController(
ICommandExecutor commandExecutor,
IQueryExecutor queryExecutor
)
{
_commandExecutor = commandExecutor;
_queryExecutor = queryExecutor;
}
public async Task<string> CreateNewShip(string shipName, decimal tonnage)
{
var generatedShipId = 0;
_commandExecutor.CommandExecuted += args => generatedShipId = (int)args.Args;
await _commandExecutor.ExecuteAsync(new CreateNewShipCommand { ShipName = shipName, Tonnage = tonnage });
return $"A new ship was created. ship id: {generatedShipId}";
}
public async Task<string> GetShipsByName(string shipName)
{
var shipDtos = (await _queryExecutor.ExecuteAsync<GetShipsByNameQuery, ShipDto>(new GetShipsByNameQuery { ShipName = shipName })).ToList();
var info = $"Number of ships queried: {shipDtos.Count}\n";
foreach (var shipDto in shipDtos)
{
info += $"Id: {shipDto.Id}, ship name: {shipDto.Name}\n";
}
return info;
}
public async Task<string> UpdateShipData(int shipId, string shipName, decimal tonnage)
{
await _commandExecutor.ExecuteAsync(new UpdateShipDataCommand { ShipId = shipId, ShipName = shipName, Tonnage = tonnage });
return "Ship data updated.";
}
}
}