|
12 | 12 | using Stl.Fusion;
|
13 | 13 | using Stl.Fusion.Authentication;
|
14 | 14 |
|
15 |
| -namespace Samples.Blazor.Abstractions |
| 15 | +namespace Samples.Blazor.Abstractions; |
| 16 | + |
| 17 | +public class LongKeyedEntity : IHasId<long> |
16 | 18 | {
|
17 |
| - public class LongKeyedEntity : IHasId<long> |
18 |
| - { |
19 |
| - [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] |
20 |
| - public long Id { get; set; } |
21 |
| - } |
| 19 | + [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] |
| 20 | + public long Id { get; set; } |
| 21 | +} |
22 | 22 |
|
23 |
| - [Index(nameof(UserId))] |
24 |
| - [Index(nameof(CreatedAt))] |
25 |
| - public class ChatMessage : LongKeyedEntity |
26 |
| - { |
27 |
| - public long UserId { get; set; } |
28 |
| - public DateTime CreatedAt { get; set; } |
29 |
| - [Required, MaxLength(4000)] |
30 |
| - public string Text { get; set; } = ""; |
31 |
| - } |
| 23 | +[Index(nameof(UserId))] |
| 24 | +[Index(nameof(CreatedAt))] |
| 25 | +public class ChatMessage : LongKeyedEntity |
| 26 | +{ |
| 27 | + public long UserId { get; set; } |
| 28 | + public DateTime CreatedAt { get; set; } |
| 29 | + [Required, MaxLength(4000)] |
| 30 | + public string Text { get; set; } = ""; |
| 31 | +} |
32 | 32 |
|
33 |
| - public class ChatPage |
34 |
| - { |
35 |
| - [JsonIgnore] |
36 |
| - public long? MinMessageId { get; } |
37 |
| - [JsonIgnore] |
38 |
| - public long? MaxMessageId { get; } |
39 |
| - // Must be sorted by ChatMessage.Id |
40 |
| - public List<ChatMessage> Messages { get; } |
41 |
| - public Dictionary<long, ChatUser> Users { get; } |
| 33 | +public class ChatPage |
| 34 | +{ |
| 35 | + [JsonIgnore] |
| 36 | + public long? MinMessageId { get; } |
| 37 | + [JsonIgnore] |
| 38 | + public long? MaxMessageId { get; } |
| 39 | + // Must be sorted by ChatMessage.Id |
| 40 | + public List<ChatMessage> Messages { get; } |
| 41 | + public Dictionary<long, ChatUser> Users { get; } |
42 | 42 |
|
43 |
| - public ChatPage() |
44 |
| - : this(new List<ChatMessage>(), new Dictionary<long, ChatUser>()) { } |
45 |
| - [JsonConstructor] |
46 |
| - public ChatPage(List<ChatMessage> messages, Dictionary<long, ChatUser> users) |
47 |
| - { |
48 |
| - Messages = messages; |
49 |
| - Users = users; |
50 |
| - if (messages.Count > 0) { |
51 |
| - MinMessageId = messages.Min(m => m.Id); |
52 |
| - MaxMessageId = messages.Max(m => m.Id); |
53 |
| - } |
| 43 | + public ChatPage() |
| 44 | + : this(new List<ChatMessage>(), new Dictionary<long, ChatUser>()) { } |
| 45 | + [JsonConstructor] |
| 46 | + public ChatPage(List<ChatMessage> messages, Dictionary<long, ChatUser> users) |
| 47 | + { |
| 48 | + Messages = messages; |
| 49 | + Users = users; |
| 50 | + if (messages.Count > 0) { |
| 51 | + MinMessageId = messages.Min(m => m.Id); |
| 52 | + MaxMessageId = messages.Max(m => m.Id); |
54 | 53 | }
|
55 | 54 | }
|
| 55 | +} |
56 | 56 |
|
57 |
| - // Not an entity! |
58 |
| - public class ChatUser : LongKeyedEntity |
59 |
| - { |
60 |
| - public static ChatUser None => new() { Id = -1, Name = "No user found" }; |
| 57 | +// Not an entity! |
| 58 | +public class ChatUser : LongKeyedEntity |
| 59 | +{ |
| 60 | + public static ChatUser None => new() { Id = -1, Name = "No user found" }; |
61 | 61 |
|
62 |
| - [Required, MaxLength(120)] |
63 |
| - public string Name { get; set; } = ""; |
64 |
| - public bool IsValid => Id >= 0; |
65 |
| - } |
| 62 | + [Required, MaxLength(120)] |
| 63 | + public string Name { get; set; } = ""; |
| 64 | + public bool IsValid => Id >= 0; |
| 65 | +} |
66 | 66 |
|
67 |
| - public interface IChatService |
| 67 | +public interface IChatService |
| 68 | +{ |
| 69 | + public record PostCommand(string Text, Session Session) : ISessionCommand<ChatMessage> |
68 | 70 | {
|
69 |
| - public record PostCommand(string Text, Session Session) : ISessionCommand<ChatMessage> |
70 |
| - { |
71 |
| - // Default constructor is needed for JSON deserialization |
72 |
| - public PostCommand() : this(null!, Session.Null) { } |
73 |
| - } |
| 71 | + // Default constructor is needed for JSON deserialization |
| 72 | + public PostCommand() : this(null!, Session.Null) { } |
| 73 | + } |
74 | 74 |
|
75 |
| - // Commands |
76 |
| - [CommandHandler] |
77 |
| - Task<ChatMessage> Post(PostCommand command, CancellationToken cancellationToken = default); |
| 75 | + // Commands |
| 76 | + [CommandHandler] |
| 77 | + Task<ChatMessage> Post(PostCommand command, CancellationToken cancellationToken = default); |
78 | 78 |
|
79 |
| - // Queries |
80 |
| - [ComputeMethod(KeepAliveTime = 11)] |
81 |
| - Task<ChatUser> GetCurrentUser(Session session, CancellationToken cancellationToken = default); |
82 |
| - [ComputeMethod(KeepAliveTime = 1)] |
83 |
| - Task<ChatUser> GetUser(long id, CancellationToken cancellationToken = default); |
84 |
| - [ComputeMethod(KeepAliveTime = 61)] |
85 |
| - Task<long> GetUserCount(CancellationToken cancellationToken = default); |
86 |
| - [ComputeMethod(KeepAliveTime = 61)] |
87 |
| - Task<long> GetActiveUserCount(CancellationToken cancellationToken = default); |
88 |
| - [ComputeMethod(KeepAliveTime = 11)] |
89 |
| - Task<ChatPage> GetChatTail(int length, CancellationToken cancellationToken = default); |
90 |
| - [ComputeMethod(KeepAliveTime = 1)] |
91 |
| - Task<ChatPage> GetChatPage(long minMessageId, long maxMessageId, CancellationToken cancellationToken = default); |
92 |
| - } |
| 79 | + // Queries |
| 80 | + [ComputeMethod(KeepAliveTime = 11)] |
| 81 | + Task<ChatUser> GetCurrentUser(Session session, CancellationToken cancellationToken = default); |
| 82 | + [ComputeMethod(KeepAliveTime = 1)] |
| 83 | + Task<ChatUser> GetUser(long id, CancellationToken cancellationToken = default); |
| 84 | + [ComputeMethod(KeepAliveTime = 61)] |
| 85 | + Task<long> GetUserCount(CancellationToken cancellationToken = default); |
| 86 | + [ComputeMethod(KeepAliveTime = 61)] |
| 87 | + Task<long> GetActiveUserCount(CancellationToken cancellationToken = default); |
| 88 | + [ComputeMethod(KeepAliveTime = 11)] |
| 89 | + Task<ChatPage> GetChatTail(int length, CancellationToken cancellationToken = default); |
| 90 | + [ComputeMethod(KeepAliveTime = 1)] |
| 91 | + Task<ChatPage> GetChatPage(long minMessageId, long maxMessageId, CancellationToken cancellationToken = default); |
93 | 92 | }
|
0 commit comments