Beispiel #1
0
        /// <summary>
        ///     Returns the most popular command of all time, with how many uses it has.
        /// </summary>
        /// <returns></returns>
        public static Dictionary <string, int> GetMostPopularCommandAsync()
        {
            var dic = new Dictionary <string, int>();

            using (var db = new KaguyaDb())
            {
                IQueryable <CommandHistory> command = from h in db.GetTable <CommandHistory>() select h;
                IQueryable <IGrouping <string, CommandHistory> > descending = command.GroupBy(x => x.Command);
                descending = descending.OrderByDescending(x => x.Count());
                string name = descending.FirstOrDefault()?.Key;

                if (String.IsNullOrWhiteSpace(name))
                {
                    return(new Dictionary <string, int>());
                }

                IQueryable <int> count = from c in db.GetTable <CommandHistory>()
                                         group c by c.Command
                                         into grp
                                         orderby grp.Count() descending
                                         select grp.Count();

                dic.Add(name, count.Select(x => x).First());

                return(dic);
            }
        }
Beispiel #2
0
        /// <summary>
        ///     Returns all of the <see cref="T" /> objects from the database that match the given <see cref="Predicate{T}" />,
        ///     limited by the <see cref="limit" /> parameter. The <see cref="selector" /> will determine what variable to order
        ///     the results by. If the <see cref="selector" /> is provided, the results will be ordered based on the
        ///     <see cref="orderByDescending" /> parameter.
        /// </summary>
        /// <typeparam name="T">The type of object to return results of.</typeparam>
        /// <typeparam name="TKey">The <see cref="TKey" /> to order our results by, assuming we want them ordered.</typeparam>
        /// <param name="predicate"></param>
        /// <param name="limit"></param>
        /// <param name="selector"></param>
        /// <param name="orderByDescending"></param>
        /// <returns></returns>
        public static async Task <List <T> > GetLimitAsync <T>(uint limit,
                                                               Expression <Func <T, bool> > predicate  = null,
                                                               Expression <Func <T, object> > selector = null,
                                                               bool orderByDescending = false) where T : class, IKaguyaQueryable <T>
        {
            // ReSharper disable once PossibleInvalidOperationException
            using (var db = new KaguyaDb())
            {
                IQueryable <T> baseQuery = db.GetTable <T>().AsQueryable();

                baseQuery = predicate == null
                    ? baseQuery
                    : baseQuery.Where(predicate);

                if (selector != null)
                {
                    baseQuery = orderByDescending
                        ? baseQuery.OrderByDescending(selector)
                        : baseQuery.OrderBy(selector);

                    return(await baseQuery.Take((int)limit).ToListAsync());
                }

                if (orderByDescending)
                {
                    throw new InvalidOperationException("Unable to apply descendant ordering with a null selector parameter.");
                }

                return(await baseQuery.Take((int)limit).ToListAsync());
            }
        }
Beispiel #3
0
 /// <summary>
 /// Returns the most recent set of statistics from the database.
 /// </summary>
 /// <returns></returns>
 public static async Task <KaguyaStatistics> GetMostRecentStatsAsync()
 {
     using (var db = new KaguyaDb())
     {
         return(await(from t in db.GetTable <KaguyaStatistics>().OrderByDescending(x => x.TimeStamp)
                      select t).FirstOrDefaultAsync());
     }
 }
Beispiel #4
0
 /// <summary>
 ///     Returns the first or default value from the database of a type that matches this <see cref="Predicate{T}" />.
 /// </summary>
 /// <typeparam name="T">The type of <see cref="IKaguyaQueryable{T}" /> object to return</typeparam>
 /// <param name="predicate">
 ///     An expression that the returned object must match.
 ///     <code>
 /// await GetFirstMatchAsync{User}(x => x.UserId == SomeId);
 /// </code>
 /// </param>
 /// <returns></returns>
 public static async Task <T> GetFirstMatchAsync <T>(Expression <Func <T, bool> > predicate) where T :
 class, IKaguyaQueryable <T>
 {
     using (var db = new KaguyaDb())
     {
         return(await(from t in db.GetTable <T>().Where(predicate)
                      select t).FirstOrDefaultAsync());
     }
 }
Beispiel #5
0
 /// <summary>
 ///     Returns a <see cref="List{T}" /> that match the given predicate.
 /// </summary>
 /// <typeparam name="T">The type of <see cref="List{T}" /> to return.</typeparam>
 /// <param name="predicate">A condition that each returned object in the <see cref="List{T}" /> must match.</param>
 /// <param name="limit">
 ///     The amount of objects to return. If this value is below zero, the function will
 ///     return all objects that match the <see cref="Predicate{T}" />
 /// </param>
 /// <returns></returns>
 public static async Task <List <T> > GetAllAsync <T>(Expression <Func <T, bool> > predicate) where T :
 class, IKaguyaQueryable <T>
 {
     using (var db = new KaguyaDb())
     {
         return(await(from t in db.GetTable <T>().Where(predicate)
                      select t).ToListAsync());
     }
 }
Beispiel #6
0
 /// <summary>
 ///     Returns the FirstOrDefault <see cref="T" /> that matches the provided <see cref="userId" /> and <see cref="serverId" />
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="userId"></param>
 /// <param name="serverId"></param>
 /// <returns></returns>
 public static async Task <T> GetForServerAndUserAsync <T>(ulong userId, ulong serverId) where T :
 class, IKaguyaQueryable <T>, IUserSearchable <T>, IServerSearchable <T>
 {
     using (var db = new KaguyaDb())
     {
         return(await(from t in db.GetTable <T>()
                      where t.UserId == userId && t.ServerId == serverId
                      select t).FirstOrDefaultAsync());
     }
 }
Beispiel #7
0
 /// <summary>
 ///     Deletes all of the <see cref="IServerSearchable{T}" /> objects from the database
 ///     where the serverId matches the provided <see cref="serverId" />
 /// </summary>
 /// <typeparam name="T">The <see cref="IServerSearchable{T}" /> to remove from the database.</typeparam>
 /// <param name="serverId">The id of the server to clear the objects from.</param>
 /// <returns></returns>
 public static async Task DeleteAllForServerAsync <T>(ulong serverId) where T :
 class, IKaguyaQueryable <T>, IServerSearchable <T>
 {
     using (var db = new KaguyaDb())
     {
         await(from t in db.GetTable <T>()
               where t.ServerId == serverId
               select t).DeleteAsync();
     }
 }
Beispiel #8
0
 public static async Task <List <T> > GetAllForUserAsync <T>(ulong userId, Expression <Func <T, bool> > predicate) where T :
 class, IKaguyaQueryable <T>, IUserSearchable <T>
 {
     using (var db = new KaguyaDb())
     {
         return(await(from t in db.GetTable <T>().Where(predicate)
                      where t.UserId == userId
                      select t).ToListAsync());
     }
 }
Beispiel #9
0
 /// <summary>
 ///     Returns a <see cref="List{T}" /> of objects that match the given <see cref="serverId" />.
 /// </summary>
 /// <typeparam name="T">The <see cref="IServerSearchable{T}" /> that we want to get for this <see cref="serverId" />.</typeparam>
 /// <param name="serverId">The Id of the server we are searching for.</param>
 /// <returns></returns>
 public static async Task <List <T> > GetAllForServerAsync <T>(ulong serverId) where T :
 class,
 IKaguyaQueryable <T>,
 IServerSearchable <T>
 {
     using (var db = new KaguyaDb())
     {
         return(await(from t in db.GetTable <T>()
                      where t.ServerId == serverId
                      select t).ToListAsync());
     }
 }
Beispiel #10
0
        /// <summary>
        ///     Inserts the object into the database. If it already exists, an exception will be thrown.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="arg">The object to insert, assuming it doesn't already exist.</param>
        /// <param name="throwExceptionIfPresent">Whether to throw an exception if the object was found in the database already.</param>
        /// <returns></returns>
        public static async Task InsertIfNotExistsAsync <T>(T arg, bool throwExceptionIfPresent = true) where T : class, IKaguyaQueryable <T>
        {
            using (var db = new KaguyaDb())
            {
                if (await db.GetTable <T>().AnyAsync(x => x.Equals(arg)))
                {
                    if (throwExceptionIfPresent)
                    {
                        throw new Exception("Item already exists in the database.");
                    }
                }

                await db.InsertAsync(arg);
            }
        }
Beispiel #11
0
        public static async Task DeleteAllAsync <T>() where T : class, IKaguyaQueryable <T>
        {
            using (var db = new KaguyaDb())
            {
                List <T> records = await db.GetTable <T>().ToListAsync();

                if (records.Count > 0)
                {
                    foreach (T record in records)
                    {
                        await db.DeleteAsync(record);
                    }
                }

                await ConsoleLogger.LogAsync($"Deleted all records of type {typeof(T)}", LogLvl.WARN);
            }
        }
Beispiel #12
0
 /// <summary>
 ///     Returns the number of objects that exist of type <see cref="T" /> matching the given <see cref="Predicate{T}" />
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="predicate"></param>
 /// <returns></returns>
 public static async Task <int> GetCountAsync <T>(Expression <Func <T, bool> > predicate)
     where T : class, IKaguyaQueryable <T>
 {
     using (var db = new KaguyaDb())
         return(await db.GetTable <T>().Where(predicate).CountAsync());
 }
Beispiel #13
0
 /// <summary>
 ///     Returns the number of objects that exist of type <see cref="T" />
 /// </summary>
 /// <typeparam name="T">The type of object to return the number of occurances of.</typeparam>
 /// <returns></returns>
 public static async Task <int> GetCountAsync <T>() where T : class, IKaguyaQueryable <T>
 {
     using (var db = new KaguyaDb())
         return(await db.GetTable <T>().CountAsync());
 }
Beispiel #14
0
 public static async Task <bool> ItemExistsAsync <T>(IEnumerable <T> args, Expression <Func <T, bool> > predicate) where T :
 class, IKaguyaQueryable <T>
 {
     using (var db = new KaguyaDb())
         return(await db.GetTable <T>().Where(predicate).AnyAsync(x => args.Contains(x)));
 }
Beispiel #15
0
 /// <summary>
 ///     Returns a <see cref="bool" /> that determines whether the provided <see cref="arg" /> exists in the database.
 ///     If it does, this function will return true.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="arg">The object that we are checking to see exists in the database.</param>
 /// <returns></returns>
 public static async Task <bool> ItemExistsAsync <T>(T arg) where T :
 class, IKaguyaQueryable <T>
 {
     using (var db = new KaguyaDb())
         return(await db.GetTable <T>().AnyAsync(x => x.Equals(arg)));
 }
Beispiel #16
0
 /// <summary>
 ///     Returns ALL objects of type <see cref="T" /> that exist in the database.
 /// </summary>
 /// <typeparam name="T">The type of object to retreive ALL items of.</typeparam>
 /// <returns></returns>
 public static async Task <List <T> > GetAllAsync <T>() where T : class, IKaguyaQueryable <T>
 {
     using (var db = new KaguyaDb())
         return(await db.GetTable <T>().ToListAsync());
 }