private async Task <List <(int, int)> > GetTotalWordsAsync(long chatId, TelegramDbContext context)
        {
            var chat = await context.Chats.FirstOrDefaultAsync(x => x.TelegramId == chatId);

            if (chat == null)
            {
                _logger.LogInformation("Got message from a new chat with id {0}", chatId);
                _logger.LogInformation("Adding chat {0} to database...", chatId);

                chat = new Database.Models.Chat {
                    TelegramId = chatId
                };
                await context.Chats.AddAsync(chat);

                await context.SaveChangesAsync();
            }

            var chatUsers = await context.ChatUsers
                            .Include(x => x.User)
                            .Include(x => x.ChatUserWords)
                            .Where(x => x.ChatId == chat.Id)
                            .Select(x => new { User = x.User.TelegramId, TotalWords = x.ChatUserWords.Where(cuw => cuw.ChatUserId == x.Id).Count() })
                            .ToListAsync();

            return(chatUsers.Select(x => (x.User, x.TotalWords)).ToList());
        }
Beispiel #2
0
        private async Task <Database.Models.ChatUser> GetChatUserAsync(Database.Models.Chat chat, Database.Models.User user, TelegramDbContext context)
        {
            var chatUser = await context.ChatUsers.FirstOrDefaultAsync(x => x.ChatId == chat.Id && x.UserId == user.Id);

            if (chatUser == null)
            {
                _logger.LogInformation("Creating connection between user {0} and chat {1}...", user.TelegramId, chat.TelegramId);

                chatUser = new Database.Models.ChatUser {
                    User = user, Chat = chat
                };
                await context.ChatUsers.AddAsync(chatUser);

                await context.SaveChangesAsync();
            }

            return(chatUser);
        }
Beispiel #3
0
        private async Task <Database.Models.Chat> GetChatAsync(long chatId, TelegramDbContext context)
        {
            var chat = await context.Chats.FirstOrDefaultAsync(x => x.TelegramId == chatId);

            if (chat == null)
            {
                _logger.LogInformation("Got message from a new chat with id {0}", chatId);
                _logger.LogInformation("Adding chat {0} to database...", chatId);

                chat = new Database.Models.Chat {
                    TelegramId = chatId
                };
                await context.Chats.AddAsync(chat);

                await context.SaveChangesAsync();
            }

            return(chat);
        }
Beispiel #4
0
        private async Task <Database.Models.User> GetUserAsync(int userId, TelegramDbContext context)
        {
            var user = await context.Users.FirstOrDefaultAsync(x => x.TelegramId == userId);

            if (user == null)
            {
                _logger.LogInformation("Got message from a new user with id {0}", userId);
                _logger.LogInformation("Adding user {0} to database...", userId);

                user = new Database.Models.User {
                    TelegramId = userId
                };
                await context.Users.AddAsync(user);

                await context.SaveChangesAsync();
            }

            return(user);
        }
Beispiel #5
0
        private async Task SetLastUpdateAsync(Update telegramLastUpdate, Database.Models.Update lastUpdate, TelegramDbContext context)
        {
            var newLastUpdateId = telegramLastUpdate.Id + 1;

            _logger.LogInformation("Setting last update id to {0}...", newLastUpdateId);

            if (lastUpdate == null)
            {
                lastUpdate = new Database.Models.Update {
                    LastUpdateId = newLastUpdateId
                };
                await context.Updates.AddAsync(lastUpdate);
            }
            else
            {
                lastUpdate.LastUpdateId = newLastUpdateId;
            }

            await context.SaveChangesAsync();
        }