예제 #1
0
파일: Delete.cshtml.cs 프로젝트: ymita/app
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            Post = await _context.Posts.FindAsync(id);

            if (Post != null)
            {
                _context.Posts.Remove(Post);
                await _context.SaveChangesAsync();
            }

            return(RedirectToPage("./Index"));
        }
예제 #2
0
파일: Edit.cshtml.cs 프로젝트: ymita/app
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for
        // more details see https://aka.ms/RazorPagesCRUD.
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            var userId = _userManager.GetUserId(User);

            Post.OwnerId = userId;

            var isDraft = Request.Form["draft"].Count == 1 ? true : false;

            Post.IsDraft = isDraft;

            _context.Attach(Post).State = EntityState.Modified;


            var selectedTagsInView = TagsInView.Where(x => x.IsSelected == true).ToList();

            string sql      = "SELECT Id, PostId, TagId from dbo.Posts_Tags_XREF WHERE PostId in (SELECT Id from dbo.Posts WHERE OwnerId = '" + userId + "') AND PostId = " + Post.Id;
            var    postTags = this._context.PostsTagsCrossReferences.FromSqlRaw(sql).ToList();

            // ユーザーが既に利用しているが、まだこの Post には紐付いていないタグは追加対象とする
            foreach (var item in selectedTagsInView)
            {
                Tag tag = new Tag()
                {
                    Id = item.Id, TagName = item.TagName
                };
                if (!postTags.Any(x => x.TagId == item.Id))
                {
                    var ptxref = new PostTagCrossReference()
                    {
                        PostId = Post.Id, TagId = item.Id
                    };
                    this._context.PostsTagsCrossReferences.Add(ptxref);
                }
            }

            // データベース上で PostsTagsRelation テーブルで Post に紐付いているタグは削除する
            var unselectedTags = TagsInView.Where(x => x.IsSelected == false).ToList();

            foreach (var item in unselectedTags)
            {
                Tag tag = new Tag()
                {
                    Id = item.Id, TagName = item.TagName
                };
                if (postTags.Any(x => x.TagId == tag.Id))
                {
                    var ptxref = this._context.PostsTagsCrossReferences.Where(x => x.TagId == tag.Id).FirstOrDefault();
                    this._context.PostsTagsCrossReferences.Remove(ptxref);
                }
            }
            // TO DO: どの Post とも紐付いていないタグは Tags テーブルから削除する => コミットする。
            //StringBuilder sb = new StringBuilder();
            //sb.Append("DELETE FROM dbo.Tags ");
            //sb.Append("WHERE Id IN( ");
            //sb.Append("    SELECT Id from dbo.Tags");
            //sb.Append("    WHERE Id NOT IN(");
            //sb.Append("        SELECT TagId from dbo.Posts_Tags_XREF");
            //sb.Append("        WHERE PostId IN (");
            //sb.Append("            SELECT Id from dbo.Posts");
            //sb.Append("            ");
            //sb.Append("            WHERE OwnerId = '" + userId +"'");
            //sb.Append("        )");
            //sb.Append("    )");
            //sb.Append(")");
            //this._context.Database.ExecuteSqlRaw(sb.ToString());

            // TO DO: //Request.Form.ToList()
            var newlyCreatedTags = Request.Form.Where(x => x.Key.Contains("newTag")).ToList();

            if (newlyCreatedTags.Count > 0)
            {
                int newlyCreatedTagsCount = int.Parse(newlyCreatedTags.Last().Key.Split("_")[1]);

                for (int i = 0; i <= newlyCreatedTagsCount; i++)
                {
                    var newlyCreatedTagPair = newlyCreatedTags.Where(x => x.Key.StartsWith("newTag_" + i)).ToList();
                    if (newlyCreatedTagPair.Count == 2)
                    {
                        if (
                            newlyCreatedTagPair[0].Key == "newTag_" + i + "_IsSelected"
                            &&
                            newlyCreatedTagPair[1].Key == "newTag_" + i + "_TagName"
                            )
                        {
                            // newlyCreatedTagPair[1].Value を新しいタグとして登録する。
                            System.Diagnostics.Debug.WriteLine(newlyCreatedTagPair[1].Value);
                            var tagName = newlyCreatedTagPair[1].Value.ToString();

                            Tag tag = new Tag()
                            {
                                TagName = tagName
                            };
                            var res = this._context.Tags.Add(tag);

                            await _context.SaveChangesAsync();

                            var ptxref = new PostTagCrossReference()
                            {
                                PostId = Post.Id, TagId = res.Entity.Id
                            };
                            this._context.PostsTagsCrossReferences.Add(ptxref);
                        }
                    }
                }
            }

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PostExists(Post.Id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            // TagsInView の更新
            this.TagsInView = await this.getTagsInView();

            return(RedirectToPage("./Edit", new { id = this.Post.Id }));
            //return Page();
        }