| | | 1 | | using StronglyTypedIds; |
| | | 2 | | |
| | | 3 | | namespace Syki.Back.Commands; |
| | | 4 | | |
| | | 5 | | public class CommandBatch |
| | | 6 | | { |
| | | 7 | | public CommandBatchId Id { get; set; } |
| | | 8 | | public Guid InstitutionId { get; set; } |
| | | 9 | | public CommandBatchType Type { get; set; } |
| | | 10 | | public CommandBatchStatus Status { get; set; } |
| | | 11 | | public DateTime CreatedAt { get; set; } |
| | | 12 | | public DateTime? ProcessedAt { get; set; } |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Id do evento que gerou o lote |
| | | 16 | | /// </summary> |
| | | 17 | | public DomainEventId? EventId { get; set; } |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Id do comando que gerou o lote |
| | | 21 | | /// </summary> |
| | | 22 | | public CommandId? SourceCommandId { get; set; } |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Id do comando que será executado quando o lote for processado com sucesso |
| | | 26 | | /// </summary> |
| | | 27 | | public CommandId? NextCommandId { get; set; } |
| | | 28 | | |
| | | 29 | | public int Size { get; set; } |
| | | 30 | | |
| | | 31 | | private CommandBatch() { } |
| | | 32 | | |
| | | 33 | | public static CommandBatch New( |
| | | 34 | | Guid institutionId, |
| | | 35 | | CommandBatchType type, |
| | | 36 | | DomainEventId? eventId = null, |
| | | 37 | | CommandId? sourceCommandId = null) |
| | | 38 | | { |
| | | 39 | | return new() |
| | | 40 | | { |
| | | 41 | | Type = type, |
| | | 42 | | Id = CommandBatchId.CreateNew(), |
| | | 43 | | CreatedAt = DateTime.UtcNow, |
| | | 44 | | InstitutionId = institutionId, |
| | | 45 | | Status = CommandBatchStatus.Pending, |
| | | 46 | | EventId = eventId, |
| | | 47 | | SourceCommandId = sourceCommandId, |
| | | 48 | | }; |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | public void ContinueWith(Command command) |
| | | 52 | | { |
| | | 53 | | NextCommandId = command.Id; |
| | | 54 | | command.SetAwaiting(); |
| | | 55 | | } |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | [StronglyTypedId] |
| | | 59 | | public partial struct CommandBatchId |
| | | 60 | | { |
| | | 61 | | public static CommandBatchId CreateNew() |
| | | 62 | | { |
| | 0 | 63 | | return new CommandBatchId(Guid.CreateVersion7()); |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | public class CommandBatchIdEfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConvert |
| | | 67 | | { |
| | 8 | 68 | | public CommandBatchIdEfCoreValueConverter() : this(null) { } |
| | | 69 | | |
| | | 70 | | public CommandBatchIdEfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMapping |
| | 4 | 71 | | : base( |
| | 4 | 72 | | id => id.Value, |
| | 4 | 73 | | value => new CommandBatchId(value), |
| | 4 | 74 | | mappingHints |
| | 4 | 75 | | ) |
| | 4 | 76 | | { } |
| | | 77 | | } |
| | | 78 | | } |