public static InMemoryRepository<ChatMessage> GetMessageRepository(int messages = 20)
        {
            var repository = new InMemoryRepository<ChatMessage>();

            for (int i = 0; i < messages; i++)
            {
                var message = new ChatMessage();
                message.Id = i;
                message.Message = "Test message" + i;
                message.Sender = new User { UserName = "******" + i, LastName = "User lastName" + i };
                message.Receiver = new User { UserName = "******" + i + 1, LastName = "User lastName" + i + 1 };
                message.SentOn = DateTime.Now;
                repository.Add(message);
            }

            return repository;
        }
Beispiel #2
0
        public void Add(string message, string sender, string receiver)
        {
            var currentUser = this.users
                .All()
                .FirstOrDefault(user => user.UserName == sender);

            var receiverUser = this.users
                .All()
                .FirstOrDefault(user => user.UserName == receiver);

            var newMessage = new ChatMessage()
            {
                Message = message,
                SentOn = DateTime.Now,
            };
            newMessage.Sender = currentUser;
            newMessage.Receiver = receiverUser;

            this.messages.Add(newMessage);
            this.messages.SaveChanges();
        }