示例#1
0
        /// <summary>
        /// 刪除指定使用者帳戶所有資料
        /// </summary>
        /// <param name="context">資料庫內容</param>
        /// <param name="id">使用者帳戶</param>
        /// <returns>非同步操作</returns>
        public static async Task Delete(HHStorageContext context, string id)
        {
            if (id == null)
            {
                throw new NotNullException("使用者帳戶名稱不該為null");
            }
            if (id.ToLower() == "admin")
            {
                throw new OperationException("系統管理者帳戶admin不可刪除");
            }
            var targetUser = context.User.SingleOrDefault(x => x.Id == id);

            if (targetUser == null)
            {
                throw new NotFoundException("找不到指定的使用者");
            }

            // 刪除檔案
            foreach (var file in context.File.Where(x => x.UserId == id))
            {
                await EF.File.Delete(context, file.Id);
            }
            context.RemoveRange(context.WebHook.Where(x => x.UserId == id));
            context.RemoveRange(context.Origin.Where(x => x.UserId == id));
            context.RemoveRange(context.Repository.Where(x => x.UserId == id));

            await context.SaveChangesAsync();
        }
示例#2
0
        /// <summary>
        /// 刪除儲存體
        /// </summary>
        /// <param name="context">資料庫內容</param>
        /// <param name="id">儲存庫唯一識別號</param>
        /// <returns>非同步操作</returns>
        public static async Task Delete(HHStorageContext context, Guid id)
        {
            var repository = context.Repository.SingleOrDefault(x => x.Id == id);

            if (repository == null)
            {
                throw new NotFoundException("找不到指定的儲存庫");
            }

            foreach (var file in context.File.Where(x => x.RepositoryId == id))
            {
                await EF.File.Delete(context, file.Id);
            }

            context.RemoveRange(context.WebHook.Where(x => x.RepositoryId == id));
            context.RemoveRange(context.Origin.Where(x => x.RepositoryId == id));
            context.Repository.Remove(repository);

            await context.SaveChangesAsync();
        }