| | 1 | | using StronglyTypedIds; |
| | 2 | | using System.Diagnostics; |
| | 3 | |
|
| | 4 | | namespace Syki.Back.Commands; |
| | 5 | |
|
| | 6 | | public class Command |
| | 7 | | { |
| 5376 | 8 | | public CommandId Id { get; set; } |
| 2688 | 9 | | public Guid InstitutionId { get; set; } |
| 16128 | 10 | | public string Type { get; set; } |
| 5376 | 11 | | public string Data { get; set; } |
| 2688 | 12 | | public CommandStatus Status { get; set; } |
| 2688 | 13 | | public DateTime CreatedAt { get; set; } |
| 2688 | 14 | | public int Duration { get; set; } |
| 2688 | 15 | | public DateTime? ProcessedAt { get; set; } |
| 0 | 16 | | public Guid? ProcessorId { get; set; } |
| 2688 | 17 | | public string? Error { get; set; } |
| | 18 | |
|
| | 19 | | /// <summary> |
| | 20 | | /// Id do evento que gerou o comando |
| | 21 | | /// </summary> |
| 2688 | 22 | | public DomainEventId? EventId { get; set; } |
| | 23 | |
|
| | 24 | | /// <summary> |
| | 25 | | /// Id do comando que gerou o comando |
| | 26 | | /// Utilizado quando um comando gera outro em seu handler |
| | 27 | | /// </summary> |
| 2688 | 28 | | public CommandId? ParentId { get; set; } |
| | 29 | |
|
| | 30 | | /// <summary> |
| | 31 | | /// Id do comando com erro que gerou o comando atual |
| | 32 | | /// Utilizado quando o comando original está com erro e é reprocessado |
| | 33 | | /// O comando atual é uma cópia do original (imutabilidade) |
| | 34 | | /// </summary> |
| 2688 | 35 | | public CommandId? OriginalId { get; set; } |
| | 36 | |
|
| | 37 | | /// <summary> |
| | 38 | | /// Id do lote que contém o comando |
| | 39 | | /// </summary> |
| 2688 | 40 | | public CommandBatchId? BatchId { get; set; } |
| | 41 | |
|
| 5376 | 42 | | public DateTime? NotBefore { get; set; } |
| | 43 | |
|
| 5376 | 44 | | public string? ActivityId { get; set; } |
| | 45 | |
|
| 5376 | 46 | | public Command() { } |
| | 47 | |
|
| 2688 | 48 | | public Command( |
| 2688 | 49 | | Guid institutionId, |
| 2688 | 50 | | object data, |
| 2688 | 51 | | DomainEventId? eventId = null, |
| 2688 | 52 | | CommandId? parentId = null, |
| 2688 | 53 | | CommandId? originalId = null, |
| 2688 | 54 | | CommandBatchId? batchId = null, |
| 2688 | 55 | | int? delaySeconds = null, |
| 2688 | 56 | | string? activityId = null |
| 2688 | 57 | | ) { |
| 2688 | 58 | | Id = CommandId.CreateNew(); |
| 2688 | 59 | | InstitutionId = institutionId; |
| 2688 | 60 | | Type = data.GetType().ToString(); |
| 2688 | 61 | | Data = data.Serialize(); |
| 2688 | 62 | | CreatedAt = DateTime.UtcNow; |
| 2688 | 63 | | EventId = eventId; |
| 2688 | 64 | | ParentId = parentId; |
| 2688 | 65 | | OriginalId = originalId; |
| 2688 | 66 | | BatchId = batchId; |
| 2688 | 67 | | ActivityId = activityId; |
| 2688 | 68 | | NotBefore = delaySeconds != null ? DateTime.UtcNow.AddSeconds(delaySeconds.Value) : null; |
| 2688 | 69 | | } |
| | 70 | |
|
| | 71 | | public void SetAwaiting() |
| | 72 | | { |
| 0 | 73 | | Status = CommandStatus.Awaiting; |
| 0 | 74 | | } |
| | 75 | |
|
| | 76 | | public void Processed(double duration) |
| | 77 | | { |
| 2688 | 78 | | ProcessedAt = DateTime.UtcNow; |
| 2688 | 79 | | Duration = Convert.ToInt32(duration); |
| 2688 | 80 | | Status = Error.HasValue() ? CommandStatus.Error : CommandStatus.Success; |
| 2688 | 81 | | } |
| | 82 | |
|
| | 83 | | public ActivityContext GetParentContext() |
| | 84 | | { |
| 2688 | 85 | | ActivityContext.TryParse(ActivityId, null, out var parsedContext); |
| 2688 | 86 | | return parsedContext; |
| | 87 | | } |
| | 88 | | } |
| | 89 | |
|
| | 90 | | [StronglyTypedId] |
| | 91 | | public partial struct CommandId |
| | 92 | | { |
| | 93 | | public static CommandId CreateNew() |
| | 94 | | { |
| | 95 | | return new CommandId(Guid.CreateVersion7()); |
| | 96 | | } |
| | 97 | |
|
| | 98 | | public class CommandIdEfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter<Co |
| | 99 | | { |
| | 100 | | public CommandIdEfCoreValueConverter() : this(null) { } |
| | 101 | |
|
| | 102 | | public CommandIdEfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints |
| | 103 | | : base( |
| | 104 | | id => id.Value, |
| | 105 | | value => new CommandId(value), |
| | 106 | | mappingHints |
| | 107 | | ) |
| | 108 | | { } |
| | 109 | | } |
| | 110 | | } |