/// <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 IHttpActionResult AttachLabel(AttachLabelRequestModel req) { LabelTableManager ltb=new LabelTableManager(new TableStorageConnection()); bool isSucceed=ltb.IncrementLabel(req.ArticleId, req.ParagraphId, HttpContext.Current.Request.UserHostAddress,req.LabelType,req.DebugMode); if (isSucceed) { var context = Request.GetOwinContext().Get<ApplicationDbContext>(); ArticleModel model =context.Articles.Find(req.ArticleId); model.LabelCount++; context.SaveChanges(); } return Json(new {isSucceed = isSucceed}); }
public ArticleSummaryResponse(ArticleModel model,LabelTableManager ltm) { this.Title = model.Title; this.LabelCount = model.LabelCount; this.LabelInfo = ltm.GetLabelsJson(model.ArticleModelId); }
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"); } }