/// <summary> /// 保存当前实例。 /// </summary> /// <param name="site">网站实例对象。</param> /// <returns>返回保存结果。</returns> public virtual DataResult Save(TSite site) { var adapter = SiteAdapter.FromSite(site); if (Context.Any(x => x.SiteKey == adapter.SiteKey && x.Id != adapter.Id)) { return(DataAction.Duplicate); } if (adapter.Id > 0) { return(FromResult(adapter.Id, Context.Update(adapter), DataAction.Updated)); } var result = Context.BeginTransaction(db => { if (!db.Create(adapter)) { return(false); } site.Id = adapter.Id; foreach (var eventHandler in _eventHandlers) { if (!eventHandler.OnCreated(db, site)) { site.Id = 0; return(false); } } return(true); }, 600); return(FromResult(adapter.Id, result, DataAction.Created)); }
/// <summary> /// 保存当前实例。 /// </summary> /// <param name="site">网站实例对象。</param> /// <returns>返回保存结果。</returns> public virtual async Task <DataResult> SaveAsync(TSite site) { var adapter = SiteAdapter.FromSite(site); if (await Context.AnyAsync(x => x.SiteKey == adapter.SiteKey && x.Id != adapter.Id)) { return(DataAction.Duplicate); } if (adapter.Id > 0) { return(FromResult(adapter.Id, await Context.UpdateAsync(adapter), DataAction.Updated)); } var result = await Context.BeginTransactionAsync(async db => { if (!await db.CreateAsync(adapter)) { return(false); } site.Id = adapter.Id; foreach (var eventHandler in _eventHandlers) { if (!await eventHandler.OnCreatedAsync(db, site)) { site.Id = 0; return(false); } } return(true); }, 600); return(FromResult(adapter.Id, result, DataAction.Created)); }
/// <summary> /// 添加默认网站。 /// </summary> /// <param name="siteKey">唯一键。</param> /// <param name="siteName">网站名称。</param> /// <param name="shortName">网站简称。</param> /// <param name="description">描述。</param> /// <param name="domains">域名列表。</param> /// <returns>返回添加结果。</returns> public virtual async Task <bool> CreateDefaultSiteAsync(string siteKey, string siteName, string shortName, string description, IEnumerable <string> domains) { if (await _context.BeginTransactionAsync(async db => { var site = new SiteAdapter { SiteKey = siteKey, SiteName = siteName, ShortName = shortName ?? siteName, Description = description }; if (await db.As <SiteAdapter>().CreateAsync(site)) { foreach (var domain in domains) { var siteDomain = new SiteDomain { Domain = domain, SiteId = site.Id }; await db.CreateAsync(siteDomain); } return(true); } return(false); })) { Refresh(); return(true); } return(false); }