コード例 #1
0
        /// <inheritdoc />
        public async Task <bool> TryUpdateAsync(ulong channelId, Action <GuildChannelMutationData> updateAction)
        {
            if (updateAction == null)
            {
                throw new ArgumentNullException(nameof(updateAction));
            }

            var entity = await ModixContext.GuildChannels
                         .Where(x => x.ChannelId == channelId)
                         .FirstOrDefaultAsync();

            if (entity == null)
            {
                return(false);
            }

            var data = GuildChannelMutationData.FromEntity(entity);

            updateAction.Invoke(data);
            data.ApplyTo(entity);

            ModixContext.UpdateProperty(entity, x => x.Name);

            await ModixContext.SaveChangesAsync();

            return(true);
        }
コード例 #2
0
        /// <inheritdoc />
        public async Task <bool> UpdateAsync(ulong guildId, Action <ModerationConfigMutationData> updateAction)
        {
            if (updateAction == null)
            {
                throw new ArgumentNullException(nameof(updateAction));
            }

            var entity = await ModixContext.ModerationConfigs
                         .Where(x => x.GuildId == (long)guildId)
                         .FirstOrDefaultAsync();

            if (entity == null)
            {
                return(false);
            }

            var data = ModerationConfigMutationData.FromEntity(entity);

            updateAction.Invoke(data);
            data.ApplyTo(entity);

            ModixContext.UpdateProperty(entity, x => x.MuteRoleId);

            await ModixContext.SaveChangesAsync();

            return(true);
        }
コード例 #3
0
        /// <inheritdoc />
        public async Task <bool> TryUpdateAsync(ulong userId, ulong guildId, Action <GuildUserMutationData> updateAction)
        {
            if (updateAction == null)
            {
                throw new ArgumentNullException(nameof(updateAction));
            }

            var entity = await ModixContext.GuildUsers
                         .Where(x => x.UserId == userId)
                         .Where(x => x.GuildId == guildId)
                         .Include(x => x.User)
                         .FirstOrDefaultAsync();

            if (entity == null)
            {
                return(false);
            }

            var data = GuildUserMutationData.FromEntity(entity);

            updateAction.Invoke(data);
            data.ApplyTo(entity);

            ModixContext.UpdateProperty(entity.User, x => x.Username);
            ModixContext.UpdateProperty(entity.User, x => x.Discriminator);
            ModixContext.UpdateProperty(entity, x => x.Nickname);
            ModixContext.UpdateProperty(entity, x => x.LastSeen);

            await ModixContext.SaveChangesAsync();

            return(true);
        }
コード例 #4
0
        /// <inheritdoc />
        public async Task <bool> UpdateAsync(long infractionId, Action <InfractionMutationData> updateAction)
        {
            if (updateAction == null)
            {
                throw new ArgumentNullException(nameof(updateAction));
            }

            var entity = await ModixContext.Infractions
                         .Where(x => x.Id == infractionId)
                         .FirstOrDefaultAsync();

            if (entity == null)
            {
                return(false);
            }

            var data = InfractionMutationData.FromEntity(entity);

            updateAction.Invoke(data);
            data.ApplyTo(entity);

            ModixContext.UpdateProperty(entity, x => x.RescindActionId);

            await ModixContext.SaveChangesAsync();

            return(true);
        }
コード例 #5
0
ファイル: UserRepository.cs プロジェクト: RubyNova/MODiX
        /// <inheritdoc />
        public async Task CreateOrUpdateAsync(ulong userId, Action <UserMutationData> updateAction)
        {
            if (updateAction == null)
            {
                throw new ArgumentNullException(nameof(updateAction));
            }

            var longUserId = (long)userId;

            var createLock = await _createLock.LockAsync();

            var entity = await ModixContext.Users
                         .SingleOrDefaultAsync(x => x.Id == longUserId);

            if (entity != null)
            {
                createLock.Dispose();
                createLock = null;
            }
            else
            {
                entity = new UserEntity()
                {
                    Id        = longUserId,
                    FirstSeen = DateTimeOffset.Now
                };

                await ModixContext.Users.AddAsync(entity);
            }

            var mutation = UserMutationData.FromEntity(entity);

            updateAction.Invoke(mutation);
            mutation.ApplyTo(entity);

            entity.LastSeen = DateTimeOffset.Now;

            if (createLock == null)
            {
                ModixContext.UpdateProperty(entity, x => x.Username);
                ModixContext.UpdateProperty(entity, x => x.Discriminator);
                ModixContext.UpdateProperty(entity, x => x.Nickname);
                ModixContext.UpdateProperty(entity, x => x.LastSeen);
            }

            await ModixContext.SaveChangesAsync();

            if (createLock != null)
            {
                createLock.Dispose();
            }
        }
コード例 #6
0
        /// <inheritdoc />
        public async Task<bool> TryUpdateAsync(ulong roleId, Action<GuildRoleMutationData> updateAction)
        {
            if (updateAction == null)
                throw new ArgumentNullException(nameof(updateAction));

            var entity = await ModixContext.GuildRoles
                .Where(x => x.RoleId == roleId)
                .FirstOrDefaultAsync();

            if (entity == null)
                return false;

            var data = GuildRoleMutationData.FromEntity(entity);
            updateAction.Invoke(data);
            data.ApplyTo(entity);

            ModixContext.UpdateProperty(entity, x => x.Name);
            ModixContext.UpdateProperty(entity, x => x.Position);

            await ModixContext.SaveChangesAsync();

            return true;
        }