/// <summary> /// Delete a PostTagEntity by compound key /// </summary> /// <param name="postTag">Compound Key</param> public void RemoveByCompound(PostTagEntity postTag) { var entity = _blogContext .PostTags .FirstOrDefault(x => x.PostId == postTag.PostId && x.TagId == postTag.TagId); _blogContext.PostTags.Remove(entity); _blogContext.SaveChanges(); }
/// <summary> /// Add a new PostTagEntity record /// </summary> /// <param name="postTag">Compound Key</param> /// <returns>Qualified PostTagEntity</returns> public PostTagEntity Add(PostTagEntity postTag) { _blogContext .PostTags .Add(postTag); _blogContext.SaveChanges(); return(postTag); }
public static void Init(TestContext tctx) { _opts = new DbContextOptionsBuilder() .UseNpgsql($"Server={TestConstants.Server};Port=5432;Database={TestConstants.GuidString};User Id=user1;Password=password1;") .Options; _testTag = TestConstants.GuidString; _testTitle = TestConstants.GuidString; _testBody = TestConstants.GuidString; using (var ctx = new BlogContext(_opts)) { ctx.Database.EnsureCreated(); var tag1 = new TagEntity { Text = _testTag }; //Add a tag ctx.Tags.Add(tag1); ctx.SaveChanges(); var body = new PostBodyEntity { Markdown = _testBody }; //add body ctx.Bodies.Add(body); ctx.SaveChanges(); var header = new PostHeaderEntity { BodyId = body.Id, Title = _testTitle }; //add header ctx.Headers.Add(header); ctx.SaveChanges(); var join = new PostTagEntity { TagId = tag1.Id, PostId = header.Id }; ctx.PostTags.Add(join); ctx.SaveChanges(); } }
public static TagReadModel ToReadModel(this PostTagEntity tagEntity) { var tagName = tagEntity.Tag.Name; var tagSlug = tagEntity.Tag.Slug; return(new TagReadModel { Name = tagName, Slugs = new List <SlugReadModel> { new SlugReadModel { Path = tagSlug, IsDefault = true } } }); }
public async Task <TagEntity> AddTagToPostAsync(int postId, TagEntity tag) { var post = _newsContext .Posts .Include(t => t.PostTags) .FirstOrDefault(p => p.PostId == postId); var newTag = new PostTagEntity() { Post = post, Tag = tag }; post.PostTags.Add(newTag); await _newsContext.SaveChangesAsync(); return(newTag.Tag); }