Exemple #1
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            var name = GetInput("What is your name?");

            messages.AddRange(await _chatMessageRepository.GetAllMessages());

            PrintTable();

            while (!stoppingToken.IsCancellationRequested)
            {
                var newMessage = GetInput("Write your message:");

                if (!newMessage.IsNullOrEmpty())
                {
                    await _chatMessageRepository.AddNewMessage(new ChatMessage()
                    {
                        From    = name,
                        Message = newMessage,
                        Time    = DateTime.UtcNow
                    });
                }

                if (messages.IsNullOrEmpty())
                {
                    messages.AddRange(await _chatMessageRepository.GetAllMessages());
                }
                else
                {
                    messages.AddRange(await _chatMessageRepository.GetAllMessagesSince(messages.LastOrDefault().Time));
                }

                PrintTable();
            }
        }