public async Task ModifySort(int id, int typeId, int nextid, int previd) { var userId = GetUserId(); var collection = await loveCollectionAppService.GetCollectionByIdAsync(userId, id); if (nextid == 0 && previd != 0)//最后面 { var tempSort = await loveCollectionAppService.CollectionQuery(userId) .Where(t => t.Id == previd).Select(t => t.Sort).FirstAsync(); collection.Sort = tempSort + 1; } else if (nextid != 0 && previd == 0)//最前面 { var tempSort = await loveCollectionAppService.CollectionQuery(userId) .Where(t => t.Id == nextid).Select(t => t.Sort).FirstAsync(); collection.Sort = tempSort / 2; } else { var sort = await loveCollectionAppService.CollectionQuery(userId) .Where(t => t.Id == nextid || t.Id == previd).SumAsync(t => t.Sort); if (sort == 0) { collection.Sort = 1024; } else { collection.Sort = sort / 2; } } collection.TypeId = typeId; await loveCollectionAppService.SaveChangesAsync(); await loveCollectionAppService.UpdateAllCollectionToRedisAsync(userId); }
/// <summary> /// 导入书签 /// </summary> /// <returns></returns> public async Task <IActionResult> ImportBookmark(IFormFile file) { var userId = GetUserId(); if (userId == 0) { return(Redirect("/")); } using (var stream = file.OpenReadStream()) { byte[] buffer = new byte[stream.Length]; await stream.ReadAsync(buffer, 0, (int)stream.Length); var htmlString = Encoding.UTF8.GetString(buffer); HtmlParser htmlParser = new HtmlParser(); var urls = await loveCollectionAppService.GetCollectionUrlsByUserIdAsync(userId); var tempTypeId = await loveCollectionAppService.GetOrAddTypeIdByUserIdAsync("未分类", userId); #region 类型导入 foreach (var childNode in htmlParser.Parse(htmlString).QuerySelector("DL DL").ChildNodes) { if (childNode is AngleSharp.Dom.IElement) { var element = childNode as AngleSharp.Dom.IElement; var typeName = element.QuerySelectorAll("H3").FirstOrDefault()?.TextContent; var typeId = tempTypeId; if (!string.IsNullOrWhiteSpace(typeName)) { typeId = await loveCollectionAppService.GetOrAddTypeIdByUserIdAsync(typeName, userId); } var collections = element.QuerySelectorAll("A").ToList(); foreach (var collection in collections) { var url = collection.Attributes.FirstOrDefault(f => f.Name == "href")?.Value; url = url.Length >= 500 ? url.Substring(0, 500) : url; if (urls.Contains(url))//忽略 已经存在 或 已经被导入过的链接 { continue; } var value = collection.TextContent; value = value.Length >= 300 ? value.Substring(0, 300) : value; await loveCollectionAppService.SaveCollectionAsync(value, url, typeId, userId); urls.Add(url); } await loveCollectionAppService.SaveChangesAsync(); } } #endregion #region 重新检测漏网之鱼 foreach (var collection in htmlParser.Parse(htmlString).QuerySelectorAll("A")) { var url = collection.Attributes.FirstOrDefault(f => f.Name == "href")?.Value; url = url.Length >= 500 ? url.Substring(0, 500) : url; if (urls.Contains(url))//忽略 已经存在 或 已经被导入过的链接 { continue; } var value = collection.TextContent; value = value.Length >= 300 ? value.Substring(0, 300) : value; await loveCollectionAppService.SaveCollectionAsync(value, url, tempTypeId, userId); urls.Add(url); } await loveCollectionAppService.SaveChangesAsync(); #endregion } await loveCollectionAppService.UpdateAllCollectionToRedisAsync(userId); await loveCollectionAppService.UpdateAllTypeToRedisAsync(userId); return(View()); }