private static void DeleteCharacterSlot(ServerDbContext db, NetUserId userId, int slot)
 {
     db.Preference
     .Single(p => p.UserId == userId.UserId)
     .Profiles
     .RemoveAll(h => h.Slot == slot);
 }
        private static async Task DeleteCharacterSlot(ServerDbContext db, NetUserId userId, int slot)
        {
            var profile = await db.Profile.Include(p => p.Preference)
                          .Where(p => p.Preference.UserId == userId.UserId && p.Slot == slot)
                          .SingleOrDefaultAsync();

            db.Profile.Remove(profile);
        }
        private static async Task SetSelectedCharacterSlotAsync(NetUserId userId, int newSlot, ServerDbContext db)
        {
            var prefs = await db.Preference.SingleAsync(p => p.UserId == userId.UserId);

            prefs.SelectedCharacterSlot = newSlot;
        }
        private async Task <IQueryable <AdminLog> > GetAdminLogsQuery(ServerDbContext db, LogFilter?filter = null)
        {
            IQueryable <AdminLog> query = db.AdminLog;

            if (filter == null)
            {
                return(query.OrderBy(log => log.Date));
            }

            if (filter.Round != null)
            {
                query = query.Where(log => log.RoundId == filter.Round);
            }

            if (filter.Search != null)
            {
                query = query.Where(log => log.Message.Contains(filter.Search));
            }

            if (filter.Types != null)
            {
                query = query.Where(log => filter.Types.Contains(log.Type));
            }

            if (filter.Impacts != null)
            {
                query = query.Where(log => filter.Impacts.Contains(log.Impact));
            }

            if (filter.Before != null)
            {
                query = query.Where(log => log.Date < filter.Before);
            }

            if (filter.After != null)
            {
                query = query.Where(log => log.Date > filter.After);
            }

            if (filter.AnyPlayers != null)
            {
                var players = await db.AdminLogPlayer
                              .Where(player => filter.AnyPlayers.Contains(player.PlayerUserId))
                              .ToListAsync();

                if (players.Count > 0)
                {
                    query = from log in query
                            join player in db.AdminLogPlayer on log.Id equals player.LogId
                            where filter.AnyPlayers.Contains(player.Player.UserId)
                            select log;
                }
            }

            if (filter.AllPlayers != null)
            {
                // TODO ADMIN LOGGING
            }

            query = query.Distinct();

            if (filter.LastLogId != null)
            {
                query = filter.DateOrder switch
                {
                    DateOrder.Ascending => query.Where(log => log.Id < filter.LastLogId),
                    DateOrder.Descending => query.Where(log => log.Id > filter.LastLogId),
                    _ => throw new ArgumentOutOfRangeException(nameof(filter),
                                                               $"Unknown {nameof(DateOrder)} value {filter.DateOrder}")
                };
            }

            query = filter.DateOrder switch
            {
                DateOrder.Ascending => query.OrderBy(log => log.Date),
                DateOrder.Descending => query.OrderByDescending(log => log.Date),
                _ => throw new ArgumentOutOfRangeException(nameof(filter),
                                                           $"Unknown {nameof(DateOrder)} value {filter.DateOrder}")
            };

            if (filter.Limit != null)
            {
                query = query.Take(filter.Limit.Value);
            }

            return(query);
        }