示例#1
0
        public async Task <UpdatedReminderResponse> UpdateReminder(UpdatedReminderRequest request)
        {
            var result = new UpdatedReminderResponse {
                Result = false
            };

            if (request == null)
            {
                return(result);
            }
            var reminder = await _context.UserHomeWorkReminder.FirstAsync(uhr => uhr.Id == request.Id);

            if (reminder == null)
            {
                return(result);
            }
            reminder.IsSend       = request.IsSend ? 1 : 0;
            reminder.DateTimeSend = request.DateTimeSend.Ticks;
            try
            {
                _context.Update <UserHomeWorkReminder>(reminder);
                await _context.SaveChangesAsync();

                result.Result = true;
            }
            catch (Exception ex)
            {
                _logger.LogException(ex);
            }
            return(result);
        }
示例#2
0
        private async Task SendComicsAsync(Subscription subscriptionType)
        {
            using (var db = new BotDBContext())
            {
                var clients = (from c in db.Clients
                               join sub in db.Subscriptions on c.Subscription equals sub.Id
                               where sub.SubsctiptionType == (int)subscriptionType
                               select c.ChatId
                               ).Distinct();

                MessageToSend message = (subscriptionType == Subscription.Oglaf) ? GetOglafPicture() : GetXKCDPicture();

                foreach (var client in clients)
                {
                    var lastPostedKey = (from cli in db.Clients
                                         join sub in db.Subscriptions on cli.Subscription equals sub.Id
                                         where cli.ChatId == client && sub.SubsctiptionType == (int)subscriptionType
                                         orderby sub.Id descending
                                         select new
                    {
                        LTK = sub.LastPostedKey,
                        SUBID = sub.Id
                    }
                                         ).First();

                    if (message.Title.Equals(lastPostedKey.LTK))
                    {
                        continue;
                    }

                    DatabaseInteractions.Subscription subToUpdate = db.Subscriptions.Where(x => x.Id == lastPostedKey.SUBID).First();
                    string newHash = message.Title;
                    subToUpdate.LastPostedKey = newHash;
                    db.Update(subToUpdate);
                    //db.Subscriptions.Where(x => x.Id == lastPostedKey.SUBID).First().LastPostedKey = message.Title.GetHashCode().ToString();

                    try
                    {
                        await _bot.SendTextMessageAsync(client, message.Title.ToUpper());

                        await _bot.SendTextMessageAsync(client, message.SubTitle);

                        await _bot.SendPhotoAsync(client, new InputOnlineFile(message.Image));
                    }
                    catch (ChatNotFoundException e)
                    {
                        TraceError.Info(e.Message);
                        var clientsRecords = db.Clients.Where(c => c.ChatId == client).ToList();
                        TraceError.Info("Client Recs to remove: " + string.Join(",", clientsRecords.Select(c => c.ChatId)));
                        var subscriptionsToRemove = db.Subscriptions.Where(x => clientsRecords.Select(o => o.Subscription).Contains(x.Id));
                        TraceError.Info("Subscription Recs to remove: " + string.Join(",", subscriptionsToRemove.Select(s => s.SubsctiptionType.ToString())));
                        db.Subscriptions.RemoveRange(subscriptionsToRemove);
                        db.Clients.RemoveRange(clientsRecords);
                    }
                }
                await db.SaveChangesAsync();
            }
        }
示例#3
0
        public async Task AddWatch([Remainder] string playersName = null)
        {
            if (playersName == null)
            {
                await ReplyAsync(Strings.PlayerNameRequired);

                return;
            }
            var playerRep = new PlayerRepository();
            await playerRep.GetPlayerByName(playersName);

            if (playerRep.Player == null)
            {
                await ReplyAsync(Strings.PlayerNotFound);

                return;
            }

            using (var context = new BotDBContext())
            {
                var user = await context.Users.FirstOrDefaultAsync(u => u.DiscordId == Context.User.Id);

                if (user == null) //It is a new User
                {
                    var newUsersPlayer = new Model.UsersPlayers()
                    {
                        User = new Model.User()
                        {
                            DiscordId = Context.User.Id, Name = Context.User.Username
                        },
                        Players = new List <Model.Player>
                        {
                            new Model.Player()
                            {
                                PubgID = playerRep.Player.Id
                            }
                        }
                    };

                    await context.UsersPlayers.AddAsync(newUsersPlayer);

                    await context.SaveChangesAsync();
                    await ReplyAsync(Strings.PlayerAddedToWatch);
                }
                else
                {
                    var usersPlayers = await context.UsersPlayers.Where(u => u.UserID == user.ID).Include(p => p.Players).FirstOrDefaultAsync();

                    if (usersPlayers == null)
                    {
                        usersPlayers = new Model.UsersPlayers()
                        {
                            User = user, Players = new List <Model.Player>()
                        };
                    }
                    if (usersPlayers != null)
                    {
                        var playersList = usersPlayers.Players.ToList();
                        if (playersList.FirstOrDefault(a => a.PubgID == playerRep.Player.Id) == null)
                        {
                            playersList.Add(new Model.Player()
                            {
                                PubgID = playerRep.Player.Id
                            });
                            usersPlayers.Players = playersList;
                            context.Update(usersPlayers);
                            await context.SaveChangesAsync();
                            await ReplyAsync(Strings.PlayerAddedToWatch);
                        }
                        else
                        {
                            await ReplyAsync(Strings.PlayerIsAlreadySaved);
                        }
                    }
                }
            }
        }