/// <summary> /// 記事表示用のVMを生成するメソッド /// </summary> /// <param name="articleId"></param> /// <returns></returns> private async Task<ViewArticleViewModel> getArticleViewModel(string articleId) { ApplicationDbContext context = HttpContext.GetOwinContext().Get<ApplicationDbContext>(); var article = await context.Articles.FindAsync(articleId); if (article == null) return null; await context.Entry(article).Collection(c=>c.Tags).LoadAsync(); BlobStorageConnection bConnection = new BlobStorageConnection(); TableStorageConnection tConnection = new TableStorageConnection(); ArticleBodyTableManager manager=new ArticleBodyTableManager(bConnection); ArticleThumbnailManager thumbnail=new ArticleThumbnailManager(bConnection); LabelTableManager ltm=new LabelTableManager(tConnection); ArticleCommentTableManager actm=new ArticleCommentTableManager(tConnection); var author = await HttpContext.GetOwinContext() .GetUserManager<ApplicationUserManager>() .FindByNameAsync(article.AuthorID); if (article.IsDraft) { if (article.AuthorID != User.Identity.Name) return null; } else { article.PageView++; } await context.SaveChangesAsync(); IGravatarLoader gLoader = new BasicGravatarLoader(author.Email); int commentCount = 0; string commentsAsJson = actm.GetCommentsAsJson(articleId, out commentCount); int count = 0; return new ViewArticleViewModel() { ArticleId = articleId, Author=author.NickName, Author_ID=author.UniqueId, Author_IconTag=gLoader.GetIconTag(50), PageView=article.PageView, Title = article.Title, Content =await manager.GetArticleBody(article.ArticleModelId), LabelInfo=ltm.GetLabelsJson(articleId), Tags =await getArticleTagModels(article), LabelCount = article.LabelCount, Article_Date = article.CreationTime.ToShortDateString(), Article_UpDate = article.UpdateTime.ToShortDateString(), UseThumbnail= thumbnail.CheckThumbnailExist(articleId), CommentInfo=commentsAsJson, CommentCount=commentCount, RelatedArticles=getRelatedArticles(context,0,0,article,3), AuthorsArticles=getUserArticles(context,0,0,article.AuthorID,out count,takeCount: 3,exceptId:article.ArticleModelId), IsPreview=false }; }
public static List<SearchResultArticle> getRelatedArticles(ApplicationDbContext context, int order, int skip, ArticleModel article, int count) { ArticleThumbnailManager thumbnailManager = new ArticleThumbnailManager(new BlobStorageConnection()); //直接の関係の記事を探す IQueryable<ArticleModel> query = context.Articles.Where(f => f.RelatedArticleId.Equals(article.ArticleModelId)); query = ChangeOrder(order, query); query = query.Skip(skip); List<SearchResultArticle> articles = new List<SearchResultArticle>(); foreach (var source in query.Take(count)) { articles.Add(new SearchResultArticle() { ArticleId = source.ArticleModelId, LabelCount = source.LabelCount, PageView = source.PageView, Title = source.Title, Article_UpDate = source.UpdateTime.ToShortDateString(), ThumbnailTag = thumbnailManager.GenerateThumnailTag(source.ArticleModelId) }); } int remain = count - article.LabelCount; if(article.LabelCount<count) {//取り出すカウントに満たない場合は間接的関連記事でうめる context.Articles.Where(f => f.ThemeId.Equals(article.ThemeId)&&!f.RelatedArticleId.Equals(article.ArticleModelId)); query = ChangeOrder(order, query); query = query.Skip(skip); foreach (var source in query.Take(remain)) { articles.Add(new SearchResultArticle() { ArticleId = source.ArticleModelId, LabelCount = source.LabelCount, PageView = source.PageView, Title = source.Title, Article_UpDate = source.UpdateTime.ToShortDateString(), ThumbnailTag = thumbnailManager.GenerateThumnailTag(source.ArticleModelId) }); } } return articles; }
public async Task<ActionResult> Index(EditViewModel vm) { ApplicationDbContext context = HttpContext.GetOwinContext().Get<ApplicationDbContext>(); var connection = new BlobStorageConnection(); ArticleBodyTableManager abtm = new ArticleBodyTableManager(connection); ArticleMarkupTableManager amtm = new ArticleMarkupTableManager(connection); ArticleThumbnailManager atm=new ArticleThumbnailManager(connection); if (vm.Mode.Equals("new")) { if (!ArticleController.IsValidTitle(vm.Title, context).IsOK) return RedirectToAction("Page404", "Home"); var article = ArticleModel.GenerateArticle(vm.Title, User.Identity.Name); if (String.IsNullOrWhiteSpace(vm.RelatedArticle)) {//関連記事がない場合、新しいテーマとして判断する if (String.IsNullOrWhiteSpace(vm.TopicId)) { article.ThemeId = IdGenerator.getId(10); } else { article.ThemeId = vm.TopicId; } article.RelatedArticleId = null; } else { var relatedArticle =await context.Articles.FindAsync(vm.RelatedArticle); article.ThemeId = relatedArticle.ThemeId; article.RelatedArticleId = vm.RelatedArticle; } //タグの処理 var tags = System.Web.Helpers.Json.Decode<string[]>(vm.Tag); if (tags.Length > 5||vm.Markup.Length<=400||tags.Any(t=>t.Length>=15)) return RedirectToAction("Page404", "Home"); foreach (var tag in tags) { ArticleTagModel tagModel = context.Tags.Where(f => f.TagName.Equals(tag)).SingleOrDefault(); bool needToAdd = tagModel == null; tagModel = tagModel ?? ArticleTagModel.GenerateTag(tag); if (needToAdd) { context.Tags.Add(tagModel); } else { await context.Entry(tagModel).Collection(c => c.Articles).LoadAsync(); } tagModel.Articles.Add(article); article.Tags.Add(tagModel); } //記事の追加 context.Articles.Add(article); await context.SaveChangesAsync(); await abtm.AddArticle(article.ArticleModelId, vm.Body); await amtm.AddMarkupAsync(article.ArticleModelId, vm.Markup); await atm.UploadThumbnail(article.ArticleModelId, vm.Thumbnail); return Redirect("~/" + article.ArticleModelId); }else if (vm.Mode.Equals("edit")) { if (!User.IsInRole("Administrator")) return RedirectToAction("Page404", "Home"); LabelTableManager lm=new LabelTableManager(new TableStorageConnection()); ArticleModel articleModel = await context.Articles.FindAsync(vm.Id); if (!articleModel.AuthorID.Equals(User.Identity.Name)) return View("Page403"); await context.Entry(articleModel).Collection(a => a.Tags).LoadAsync(); articleModel.Tags.Clear(); var tags = System.Web.Helpers.Json.Decode<string[]>(vm.Tag); if (tags.Length > 5) return RedirectToAction("Page404", "Home"); foreach (var tag in tags) { ArticleTagModel tagModel = context.Tags.Where(f => f.TagName.Equals(tag)).SingleOrDefault(); bool needToAdd = tagModel == null; tagModel = tagModel ?? ArticleTagModel.GenerateTag(tag); if (needToAdd) { context.Tags.Add(tagModel); } else { await context.Entry(tagModel).Collection(c => c.Articles).LoadAsync(); } tagModel.Articles.Add(articleModel); articleModel.Tags.Add(tagModel); } articleModel.LabelCount = 0; await context.SaveChangesAsync(); await abtm.AddArticle(articleModel.ArticleModelId, vm.Body); await amtm.AddMarkupAsync(articleModel.ArticleModelId, vm.Markup); await atm.UploadThumbnail(articleModel.ArticleModelId, vm.Thumbnail); await lm.RemoveArticleLabelAsync(vm.Id); return Redirect("~/" + vm.Id); }else if (vm.Mode.Equals("append")) { LabelTableManager lm = new LabelTableManager(new TableStorageConnection()); ArticleModel articleModel = await context.Articles.FindAsync(vm.Id); if (!articleModel.AuthorID.Equals(User.Identity.Name)) return View("Page403"); await context.Entry(articleModel).Collection(a => a.Tags).LoadAsync(); articleModel.Tags.Clear(); var tags = System.Web.Helpers.Json.Decode<string[]>(vm.Tag); if (tags.Length > 5) return RedirectToAction("Page404", "Home"); foreach (var tag in tags) { ArticleTagModel tagModel = context.Tags.Where(f => f.TagName.Equals(tag)).SingleOrDefault(); bool needToAdd = tagModel == null; tagModel = tagModel ?? ArticleTagModel.GenerateTag(tag); if (needToAdd) { context.Tags.Add(tagModel); } else { await context.Entry(tagModel).Collection(c => c.Articles).LoadAsync(); } tagModel.Articles.Add(articleModel); articleModel.Tags.Add(tagModel); } articleModel.LabelCount = 0; await context.SaveChangesAsync(); await abtm.AppendArticle(articleModel.ArticleModelId, vm.Body); await amtm.AppendMarkupAsync(articleModel.ArticleModelId, vm.Markup); await atm.UploadThumbnail(articleModel.ArticleModelId, vm.Thumbnail); return Redirect("~/" + vm.Id); } else { return View("Page403"); } }
//TODO パフォーマンス改善 public static List<SearchResultArticle> getUserArticles(ApplicationDbContext context, int order, int skip, string userId, out int count, int takeCount = 10,string exceptId="") { ArticleThumbnailManager thumbnailManager=new ArticleThumbnailManager(new BlobStorageConnection()); IQueryable<ArticleModel> query = String.IsNullOrWhiteSpace(exceptId) ? context.Articles.Where(f => f.AuthorID.Equals(userId)) : context.Articles.Where(f => f.AuthorID.Equals(userId) && !f.ArticleModelId.Equals(exceptId)); count = query.Count(); query = ChangeOrder(order, query); query = query.Skip(skip); List<SearchResultArticle> articles = new List<SearchResultArticle>(); foreach (var source in query.Take(takeCount)) { articles.Add(new SearchResultArticle() { ArticleId = source.ArticleModelId, LabelCount = source.LabelCount, PageView = source.PageView, Title = source.Title, Article_UpDate = source.UpdateTime.ToShortDateString(), ThumbnailTag=thumbnailManager.GenerateThumnailTag(source.ArticleModelId) }); } return articles; }
public async Task<ActionResult> Search(string searchText,int skip=0,int order=0) { if(searchText==null)return View(new SearchResultViewModel() {Articles = new SearchResultArticle[0],Order = order,Skip = skip}); string[] queries=searchText.Split(' '); var context = Request.GetOwinContext().Get<ApplicationDbContext>(); string first = queries[0]; var result=context.Articles.Where((f) => f.Title.Contains(first)&&!f.IsDraft); for (int index = 1; index < Math.Min(4,queries.Length); index++) { var query = queries[index]; result=result.Where(f => f.Title.Contains(query)); } int count = await result.CountAsync(); result = ChangeOrder(order, result); result = result.Skip(skip); SearchResultViewModel vm=new SearchResultViewModel(); ArticleThumbnailManager thumbnailManager=new ArticleThumbnailManager(new BlobStorageConnection()); List<SearchResultArticle> articles=new List<SearchResultArticle>(); foreach (var source in result.Take(20)) { articles.Add(new SearchResultArticle() { ArticleId = source.ArticleModelId, LabelCount = source.LabelCount, PageView = source.PageView, Title = source.Title, Article_UpDate = source.UpdateTime.ToShortDateString(), ThumbnailTag = thumbnailManager.GenerateThumnailTag(source.ArticleModelId) }); } vm.Articles = articles.ToArray(); if (vm.Articles.Length == 0) { vm.SearchResultText = string.Format("「{0}」に関する検索結果は見つかりませんでした。", searchText); } else { vm.SearchResultText = string.Format("「{0}」に関する検索結果:{1}件中{2}~{3}件", searchText,count,skip+1,Math.Min(skip+21,count)); } vm.SearchText = searchText; vm.Order = order; vm.Skip = skip; vm.Count = count; return View(vm); }