public static async Task <BookmarkView> Insert(Bookmark bookmark, bool isNotAllowNet) { if (bookmark == null) { throw new ArgumentNullException(nameof(bookmark)); } using (DbHelper db = new DbHelper()) { // 处理URL bookmark.Url = bookmark.Url.Trim(); if (await BookmarkRepository.IsExistUrl(db, bookmark.CatalogId, bookmark.Url)) { throw new Exception("该目录已存在此URL,请勿重复增加"); } // 处理title if (string.IsNullOrWhiteSpace(bookmark.Title)) { if (!isNotAllowNet) { bookmark.Title = await NetHelper.GetTitle(bookmark.Url); } } bookmark.Title = bookmark.Title.Replace(System.Environment.NewLine, " "); // 处理Site string host = NetHelper.GetHost(bookmark.Url); Site site = null; bool isNewSite = false; if (await SiteRepository.IsExist(db, host)) { site = await SiteRepository.GetSiteWithHostAsync(db, host); isNewSite = false; } else { byte[] icon = null; if (isNotAllowNet) { icon = ObjectHelper.ImageToByte(BookmarkManager.Properties.Resources.Hyperlink); } else { icon = NetHelper.GetFavicon(bookmark.Url); } site = new Site { Id = Guid.NewGuid().ToString(), Host = host, Icon = icon }; isNewSite = true; } bookmark.SiteId = site.Id; // 修改数据库 using (var trans = db.BeginTransaction()) { try { if (isNewSite) { await SiteRepository.Insert(db, site); } await BookmarkRepository.Insert(db, bookmark); trans.Commit(); } catch (Exception) { trans.Rollback(); throw; } } return(await BookmarkViewRepository.GetById(db, bookmark.Id)); } }