示例#1
0
        public static async Task AddGuildRole(ulong roleId, ulong guildId, DiscordRole.ActionType action)
        {
            using (var DbContext = new SQLiteDatabaseContext())
            {
                var query = DbContext.DiscordRolesDb.Where(x => x.RoleId == roleId && x.GuildId == guildId);
                if (query.Count() < 1)
                {
                    var role = new DiscordRole
                    {
                        RoleId  = roleId,
                        GuildId = guildId,
                        Action  = action
                    };
                    DbContext.DiscordRolesDb.Add(role);

                    await DbContext.SaveChangesAsync();
                }
                else
                {
                    var role = query.FirstOrDefault();
                    if (role.Action != action)
                    {
                        role.Action = action;

                        DbContext.DiscordRolesDb.Update(role);

                        await DbContext.SaveChangesAsync();
                    }
                }
            }
        }
        public static async Task AddGuildUser(ulong UserId, ulong GuildId, DiscordGuildUser.PermissionLevels permission)
        {
            using (var DbContext = new SQLiteDatabaseContext())
            {
                var query = DbContext.DiscordUsersDB.Where(x => x.UserId == UserId && x.GuildId == GuildId);
                if (query.Count() < 1)
                {
                    var user = new DiscordGuildUser
                    {
                        UserId          = UserId,
                        GuildId         = GuildId,
                        PermissionLevel = permission
                    };
                    DbContext.DiscordUsersDB.Add(user);

                    await DbContext.SaveChangesAsync();
                }
                else
                {
                    var user = query.FirstOrDefault();
                    if (user.PermissionLevel != permission)
                    {
                        user.PermissionLevel = permission;

                        DbContext.DiscordUsersDB.Update(user);

                        await DbContext.SaveChangesAsync();
                    }
                }
            }
        }
        /// <summary>
        /// Adds new pending verification form record
        /// </summary>
        /// <param name="form">Verification form</param>
        /// <returns></returns>
        public static async Task AddPendingVerificationForm(VerificationForm form)
        {
            using (var DbContext = new SQLiteDatabaseContext())
            {
                var query = DbContext.VerificationDB.Where(x =>
                                                           x.GuildId == form.GuildId &&
                                                           x.Verified == form.Verified &&
                                                           x.IsApproved == null
                                                           );

                if (query.Count() < 1)
                {
                    var submit = new VerificationForm
                    {
                        GuildId   = form.GuildId,
                        Verified  = form.Verified,
                        Verifier  = form.Verifier,
                        IssuedUtc = DateTime.UtcNow
                    };

                    DbContext.VerificationDB.Add(submit);

                    await DbContext.SaveChangesAsync();
                }
                else
                {
                    throw new VerificationFormExistsException();
                }
            }
        }
        public override async Task <bool> Update(Assistant entity)
        {
            await using var ctx = new SQLiteDatabaseContext();

            ctx.Update(entity);

            return(await ctx.SaveChangesAsync() > 0);
        }
示例#5
0
        // TODO: Clean this up and use an algorithm that is scalable for child items
        public async Task UpdateAsync(Notebook item)
        {
            try
            {
                SQLiteDatabaseContext context = database.Context;
                context.SaveChanges();

                Notebook existingNotebook = context.Notebooks
                                            .Where(n => n.ID == item.ID)
                                            .Include(n => n.Stacks)
                                            .SingleOrDefault();

                if (existingNotebook == null)
                {
                    throw new Exception($"The notebook with ID { item.ID } could not be found");
                }

                context.Entry(existingNotebook).CurrentValues.SetValues(item);

                // Delete children
                foreach (Stack existingStack in existingNotebook.Stacks)
                {
                    if (!item.Stacks.Any(s => s.ID == existingStack.ID))
                    {
                        context.Stacks.Remove(existingStack);
                    }
                }

                // Update and insert children
                foreach (Stack stack in item.Stacks)
                {
                    Stack existingStack = existingNotebook.Stacks
                                          .Where(s => s.ID == stack.ID && s.ID != default)
                                          .SingleOrDefault();

                    if (existingStack == null)
                    {
                        // Insert child
                        existingNotebook.Stacks.Add(stack);
                    }
                    else
                    {
                        // Update child
                        context.Entry(existingStack).CurrentValues.SetValues(stack);
                    }

                    await UpdateStackAsync(stack);
                }

                await context.SaveChangesAsync();
            }
            catch (Exception e)
            {
                InformationDispatcher.Default.Dispatch(e);  // TODO: Remove
                throw new DataSourceException("Could not update notebook", e);
            }
        }
        /// <summary>
        /// Adds new Verification form record if a pending record does not exist or updates the existing pending record
        /// </summary>
        /// <param name="form">Verification Form record</param>
        /// <returns></returns>
        public static async Task AddFullVerificationForm(VerificationForm form)
        {
            using (var DbContext = new SQLiteDatabaseContext())
            {
                var query = DbContext.VerificationDB.Where(x =>
                                                           x.GuildId == form.GuildId &&
                                                           x.Verified == form.Verified &&
                                                           x.IsApproved == null
                                                           );

                if (query.Count() < 1)
                {
                    if (form.IssuedUtc == null)
                    {
                        form.IssuedUtc = DateTime.UtcNow;
                    }

                    if (form.ApprovedUtc == null)
                    {
                        form.ApprovedUtc = DateTime.UtcNow;
                    }

                    DbContext.VerificationDB.Add(form);

                    await DbContext.SaveChangesAsync();
                }
                else
                {
                    var qForm = query.First();

                    if (form.IsApproved != null)
                    {
                        qForm.IsApproved  = form.IsApproved;
                        qForm.Approver    = form.Approver;
                        qForm.ApprovedUtc = form.ApprovedUtc ?? DateTime.UtcNow;

                        DbContext.VerificationDB.Update(qForm);

                        await DbContext.SaveChangesAsync();
                    }
                }
            }
        }
示例#7
0
        private async Task UpdateStackAsync(Stack item)
        {
            try
            {
                SQLiteDatabaseContext context = database.Context;

                Stack existingStack = context.Stacks
                                      .Where(s => s.ID == item.ID)
                                      .Include(s => s.Fields)
                                      .SingleOrDefault();

                if (existingStack == null)
                {
                    return;
                }

                context.Entry(existingStack).CurrentValues.SetValues(item);

                // Delete children
                foreach (CustomField existingField in existingStack.Fields)
                {
                    if (!item.Fields.Any(f => f.ID == existingField.ID))
                    {
                        context.Fields.Remove(existingField);
                    }
                }

                // Update and insert children
                foreach (CustomField field in item.Fields)
                {
                    CustomField existingField = existingStack.Fields
                                                .Where(f => f.ID == field.ID && f.ID != default)
                                                .SingleOrDefault();

                    if (existingField == null)
                    {
                        // Insert child
                        existingStack.Fields.Add(field);
                    }
                    else
                    {
                        // Update child
                        context.Entry(existingField).CurrentValues.SetValues(field);
                    }
                }

                await context.SaveChangesAsync();
            }
            catch (Exception e)
            {
                InformationDispatcher.Default.Dispatch(e);  // TODO: Remove
                throw new DataSourceException("Could not update stack", e);
            }
        }
        public static async Task RemoveGuildUser(DiscordGuildUser user)
        {
            using (var DbContext = new SQLiteDatabaseContext())
            {
                DbContext.DiscordUsersDB.RemoveRange(
                    DbContext.DiscordUsersDB.Where(x =>
                                                   x.UserId == user.UserId &&
                                                   x.GuildId == user.GuildId
                                                   ));

                await DbContext.SaveChangesAsync();
            }
        }
示例#9
0
        public static async Task RemoveGuildRole(ulong roleId, ulong guildId)
        {
            using (var DbContext = new SQLiteDatabaseContext())
            {
                DbContext.DiscordRolesDb.RemoveRange(
                    DbContext.DiscordRolesDb.Where(x =>
                                                   x.RoleId == roleId &&
                                                   x.GuildId == guildId
                                                   ));

                await DbContext.SaveChangesAsync();
            }
        }
示例#10
0
        public static async Task RemoveGuildRole(DiscordRole role)
        {
            if (role is null)
            {
                throw new ArgumentNullException(nameof(role));
            }

            using (var DbContext = new SQLiteDatabaseContext())
            {
                DbContext.DiscordRolesDb.RemoveRange(
                    DbContext.DiscordRolesDb.Where(x =>
                                                   x.RoleId == role.RoleId &&
                                                   x.GuildId == role.GuildId
                                                   ));

                await DbContext.SaveChangesAsync();
            }
        }