Пример #1
0
        /// <summary>
        /// 保存配置实例。
        /// </summary>
        /// <param name="site">当前配置实例。</param>
        /// <returns>返回数据结果。</returns>
        public virtual async Task <DataResult> SaveAsync(SiteBase site)
        {
            var adapter = site.ToStore();

            if (adapter.SiteId > 0)
            {
                return(DataResult.FromResult(await _sdb.UpdateAsync(adapter), DataAction.Updated));
            }
            if (await IsDuplicatedAsync(site))
            {
                return(DataResult.FromResult(await _sdb.UpdateAsync(x => x.SiteKey == adapter.SiteKey, new { adapter.SiteName, adapter.UpdatedDate, adapter.SettingValue }), DataAction.Updated));
            }
            return(DataResult.FromResult(await _sdb.BeginTransactionAsync(async db =>
            {
                if (!await db.CreateAsync(adapter))
                {
                    return false;
                }
                site.SiteId = adapter.SiteId;
                foreach (var handler in _handlers)
                {
                    if (!await handler.OnCreateAsync(db, site))
                    {
                        return false;
                    }
                }
                return true;
            }), DataAction.Created));
        }
Пример #2
0
        /// <summary>
        /// 保存配置实例。
        /// </summary>
        /// <param name="site">当前配置实例。</param>
        /// <returns>返回数据结果。</returns>
        public virtual DataResult Save(SiteBase site)
        {
            var adapter = site.ToStore();

            if (adapter.SiteId > 0)
            {
                return(DataResult.FromResult(_sdb.Update(adapter), DataAction.Updated));
            }
            if (IsDuplicated(site))
            {
                return(DataResult.FromResult(_sdb.Update(x => x.SiteKey == adapter.SiteKey, new { adapter.SiteName, adapter.UpdatedDate, adapter.SettingValue }), DataAction.Updated));
            }
            return(DataResult.FromResult(_sdb.BeginTransaction(db =>
            {
                if (!db.Create(adapter))
                {
                    return false;
                }
                site.SiteId = adapter.SiteId;
                foreach (var handler in _handlers)
                {
                    if (!handler.OnCreate(db, site))
                    {
                        return false;
                    }
                }
                return true;
            }), DataAction.Created));
        }