public async Task <IActionResult> AddTagToEntry(int id, [FromBody] Models.Tag tag, [FromServices] ApplicationDbContext db) { var entry = await db.Entries.SingleOrDefaultAsync(i => i.Id == id); if (entry == null) { return(NotFound("Entry not found")); } var dbTag = await db.Tags.SingleOrDefaultAsync(i => i.Name.Equals(tag.Name, StringComparison.InvariantCultureIgnoreCase)); if (dbTag == null) { dbTag = new Data.Tag() { Name = tag.Name }; db.Tags.Add(dbTag); } var ec = await db.EntryTags.SingleOrDefaultAsync(e => e.TagId == dbTag.Id && e.EntryId == entry.Id); if (ec == null) { ec = new EntryTag() { Tag = dbTag, Entry = entry }; db.EntryTags.AddRange(ec); await db.SaveChangesAsync(); } return(Created("", dbTag)); }
public void NullTagToTaxonomy() { Data.Tag tag = null; Models.Taxonomy t = tag; Assert.Null(t); }
public static Tag Map(this Data.Tag e, int count) { Tag result = new Tag() { ID = e.ID, TagName = e.TagName }; result.SessionsCount = count; return(result); }
public void AddWithSlug() { using (var api = new Api(GetDb(), new ContentServiceFactory(services), storage, cache)) { var model = new Data.Tag() { BlogId = BLOG_ID, Title = TAG_6, Slug = TAG_6_SLUG }; api.Tags.Save(model); Assert.Equal(TAG_6_SLUG, model.Slug); } }
public void TagToTaxonomy() { var id = Guid.NewGuid(); Models.Taxonomy t = new Data.Tag { Id = id, Title = "Test", Slug = "test" }; Assert.NotNull(t); Assert.Equal(id, t.Id); Assert.Equal("Test", t.Title); Assert.Equal("test", t.Slug); }
// Refactored into TagRepository //public IList<Tag> GetTags() //{ // using (OCCDB db = new OCCDB()) // { // List<Tag> result = new List<Tag>(); // //var sessions = db.Sessions; // //var tags = db.Tags.OrderBy(t => t.TagName); // var sessionsTags = from t in db.Tags // select new { t.ID, t.TagName, SessionsCount = db.Sessions.Where(s => s.Tag_ID == t.ID).Count() }; // foreach (var tag in sessionsTags) // { // Data.Tag tg = new Data.Tag() { ID = tag.ID, TagName = tag.TagName }; // int count = tag.SessionsCount; // result.Add(tg.Map(count)); // } // return result; // } //} public IList <Tag> GetTagsByEvent(int eventid) { using (OCCDB db = new OCCDB()) { List <Tag> result = new List <Tag>(); //var sessions = db.Sessions; //var tags = db.Tags.OrderBy(t => t.TagName); var sessionsTags = from t in db.Tags select new { t.ID, t.TagName, SessionsCount = db.Sessions.Where(s => s.Tag_ID == t.ID && s.Event_ID == eventid).Count() }; foreach (var tag in sessionsTags) { Data.Tag tg = new Data.Tag() { ID = tag.ID, TagName = tag.TagName }; int count = tag.SessionsCount; result.Add(tg.Map(count)); } return(result); } }
// Refactored into TagRepository //public IList<Tag> GetTags() //{ // using (OCCDB db = new OCCDB()) // { // List<Tag> result = new List<Tag>(); // //var sessions = db.Sessions; // //var tags = db.Tags.OrderBy(t => t.TagName); // var sessionsTags = from t in db.Tags // select new { t.ID, t.TagName, SessionsCount = db.Sessions.Where(s => s.Tag_ID == t.ID).Count() }; // foreach (var tag in sessionsTags) // { // Data.Tag tg = new Data.Tag() { ID = tag.ID, TagName = tag.TagName }; // int count = tag.SessionsCount; // result.Add(tg.Map(count)); // } // return result; // } //} public IList<Tag> GetTagsByEvent(int eventid) { using (OCCDB db = new OCCDB()) { List<Tag> result = new List<Tag>(); //var sessions = db.Sessions; //var tags = db.Tags.OrderBy(t => t.TagName); var sessionsTags = from t in db.Tags select new { t.ID, t.TagName, SessionsCount = db.Sessions.Where(s => s.Tag_ID == t.ID && s.Event_ID == eventid).Count() }; foreach (var tag in sessionsTags) { Data.Tag tg = new Data.Tag() { ID = tag.ID, TagName = tag.TagName }; int count = tag.SessionsCount; result.Add(tg.Map(count)); } return result; } }
public static Models.Tag ToWebTag(this Data.Tag card) { TagConverter converter = new TagConverter(); return(converter.Convert(card)); }
/// <summary> /// Saves the given post model /// </summary> /// <param name="model">The post model</param> public void Save <T>(T model) where T : Models.PostBase { var type = api.PostTypes.GetById(model.TypeId); if (type != null) { // Ensure category if (model.Category.Id == Guid.Empty) { Data.Category category = null; if (!string.IsNullOrWhiteSpace(model.Category.Slug)) { category = api.Categories.GetBySlug(model.BlogId, model.Category.Slug); } if (category == null && !string.IsNullOrWhiteSpace(model.Category.Title)) { category = api.Categories.GetByTitle(model.BlogId, model.Category.Title); } if (category == null) { category = new Data.Category() { Id = Guid.NewGuid(), BlogId = model.BlogId, Title = model.Category.Title }; api.Categories.Save(category); } model.Category.Id = category.Id; } // Ensure tags foreach (var t in model.Tags) { if (t.Id == Guid.Empty) { Data.Tag tag = null; if (!string.IsNullOrWhiteSpace(t.Slug)) { tag = api.Tags.GetBySlug(model.BlogId, t.Slug); } if (tag == null && !string.IsNullOrWhiteSpace(t.Title)) { tag = api.Tags.GetByTitle(model.BlogId, t.Title); } if (tag == null) { tag = new Data.Tag() { Id = Guid.NewGuid(), BlogId = model.BlogId, Title = t.Title }; api.Tags.Save(tag); } t.Id = tag.Id; } } // Ensure that we have a slug if (string.IsNullOrWhiteSpace(model.Slug)) { model.Slug = Utils.GenerateSlug(model.Title); } else { model.Slug = Utils.GenerateSlug(model.Slug); } var post = db.Posts .Include(p => p.Fields) .Include(p => p.Tags) .FirstOrDefault(p => p.Id == model.Id); // If not, create a new post if (post == null) { post = new Post() { Id = model.Id != Guid.Empty ? model.Id : Guid.NewGuid(), Created = DateTime.Now, LastModified = DateTime.Now }; db.Posts.Add(post); model.Id = post.Id; } else { post.LastModified = DateTime.Now; } post = contentService.Transform <T>(model, type, post); // Remove tags var removedTags = new List <PostTag>(); foreach (var tag in post.Tags) { if (!model.Tags.Any(t => t.Id == tag.TagId)) { removedTags.Add(tag); } } foreach (var removed in removedTags) { post.Tags.Remove(removed); } // Add tags foreach (var tag in model.Tags) { if (!post.Tags.Any(t => t.PostId == post.Id && t.TagId == tag.Id)) { post.Tags.Add(new PostTag() { PostId = post.Id, TagId = tag.Id }); } } db.SaveChanges(); if (cache != null) { RemoveFromCache(post); } } }
protected override void Init() { using (var api = new Api(GetDb(), new ContentServiceFactory(services), storage)) { Piranha.App.Init(api); var pageBuilder = new PageTypeBuilder(api) .AddType(typeof(MyPage)); pageBuilder.Build(); var postBuilder = new PostTypeBuilder(api) .AddType(typeof(MyPost)); postBuilder.Build(); // Add site var site1 = new Data.Site() { Id = SITE1_ID, Title = "Page Site", InternalId = "PostSite", IsDefault = true }; api.Sites.Save(site1); var site2 = new Data.Site() { Id = SITE2_ID, Title = "Page Site 2", InternalId = "PostSite2", Hostnames = "www.myothersite.com", IsDefault = false }; api.Sites.Save(site2); // Add pages var page1 = MyPage.Create(api); page1.Id = PAGE1_ID; page1.SiteId = SITE1_ID; page1.Title = "Blog"; page1.Published = DateTime.Now; api.Pages.Save(page1); var page2 = MyPage.Create(api); page2.Id = PAGE2_ID; page2.SiteId = SITE2_ID; page2.Title = "News"; page2.Published = DateTime.Now; api.Pages.Save(page2); // Add categories var category1 = new Data.Category() { Id = CATEGORY1_ID, BlogId = PAGE1_ID, Title = "Default category" }; api.Categories.Save(category1); var category2 = new Data.Category() { Id = CATEGORY2_ID, BlogId = PAGE2_ID, Title = "Default category" }; api.Categories.Save(category2); // Add tags var tag = new Data.Tag() { Id = TAG1_ID, BlogId = PAGE1_ID, Title = "My tag" }; api.Tags.Save(tag); tag = new Data.Tag() { Id = TAG2_ID, BlogId = PAGE2_ID, Title = "My other tag" }; api.Tags.Save(tag); // Add posts var post1 = MyPost.Create(api); post1.Id = POST1_ID; post1.BlogId = page1.Id; post1.Category = category1; post1.Title = "My first post"; post1.Body = "My first body"; post1.Tags.Add("My tag"); post1.Published = DateTime.Now; api.Posts.Save(post1); var post2 = MyPost.Create(api); post2.Id = POST2_ID; post2.BlogId = page2.Id; post2.Category = category2; post2.Title = "My second post"; post2.Body = "My second body"; post2.Tags.Add("My other tag"); post2.Published = DateTime.Now; api.Posts.Save(post2); } }
/// <summary> /// Saves the given post model /// </summary> /// <param name="model">The post model</param> public void Save <T>(T model) where T : Models.PostBase { var type = api.PostTypes.GetById(model.TypeId); if (type != null) { var currentRegions = type.Regions.Select(r => r.Id).ToArray(); // Ensure category if (model.Category.Id == Guid.Empty) { Data.Category category = null; if (!string.IsNullOrWhiteSpace(model.Category.Slug)) { category = api.Categories.GetBySlug(model.BlogId, model.Category.Slug); } if (category == null && !string.IsNullOrWhiteSpace(model.Category.Title)) { category = api.Categories.GetByTitle(model.BlogId, model.Category.Title); } if (category == null) { category = new Data.Category() { Id = Guid.NewGuid(), BlogId = model.BlogId, Title = model.Category.Title }; api.Categories.Save(category); } model.Category.Id = category.Id; } // Ensure tags foreach (var t in model.Tags) { if (t.Id == Guid.Empty) { Data.Tag tag = null; if (!string.IsNullOrWhiteSpace(t.Slug)) { tag = api.Tags.GetBySlug(model.BlogId, t.Slug); } if (tag == null && !string.IsNullOrWhiteSpace(t.Title)) { tag = api.Tags.GetByTitle(model.BlogId, t.Title); } if (tag == null) { tag = new Data.Tag() { Id = Guid.NewGuid(), BlogId = model.BlogId, Title = t.Title }; api.Tags.Save(tag); } t.Id = tag.Id; } } var post = db.Posts .Include(p => p.Fields) .Include(p => p.Tags) .FirstOrDefault(p => p.Id == model.Id); // If not, create a new post if (post == null) { post = new Post() { Id = model.Id != Guid.Empty ? model.Id : Guid.NewGuid(), Created = DateTime.Now, LastModified = DateTime.Now }; db.Posts.Add(post); model.Id = post.Id; } else { post.LastModified = DateTime.Now; } // Ensure that we have a slug if (string.IsNullOrWhiteSpace(model.Slug)) { model.Slug = Utils.GenerateSlug(model.Title); } else { model.Slug = Utils.GenerateSlug(model.Slug); } // Map basic fields App.Mapper.Map <Models.PostBase, Post>(model, post); // Remove tags var removedTags = new List <PostTag>(); foreach (var tag in post.Tags) { if (!model.Tags.Any(t => t.Id == tag.TagId)) { removedTags.Add(tag); } } if (removedTags.Count > 0) { db.PostTags.RemoveRange(removedTags); } // Add tags foreach (var tag in model.Tags) { if (!post.Tags.Any(t => t.PostId == post.Id && t.TagId == tag.Id)) { post.Tags.Add(new PostTag() { PostId = post.Id, TagId = tag.Id }); } } // Map regions foreach (var regionKey in currentRegions) { // Check that the region exists in the current model if (HasRegion(model, regionKey)) { var regionType = type.Regions.Single(r => r.Id == regionKey); if (!regionType.Collection) { MapRegion(model, post, GetRegion(model, regionKey), regionType, regionKey); } else { var items = new List <Guid>(); var sortOrder = 0; foreach (var region in GetEnumerable(model, regionKey)) { var fields = MapRegion(model, post, region, regionType, regionKey, sortOrder++); if (fields.Count > 0) { items.AddRange(fields); } } // Now delete removed collection items var removedFields = db.PostFields .Where(f => f.PostId == model.Id && f.RegionId == regionKey && !items.Contains(f.Id)) .ToList(); if (removedFields.Count > 0) { db.PostFields.RemoveRange(removedFields); } } } } // If this is a published post, update last modified for the // blog page for caching purposes. if (post.Published.HasValue) { var page = db.Pages .FirstOrDefault(p => p.Id == post.BlogId); page.LastModified = DateTime.Now; } db.SaveChanges(); if (cache != null) { RemoveFromCache(post); } } }
/// <summary> /// Saves the given post model /// </summary> /// <param name="model">The post model</param> public void Save <T>(T model) where T : Models.PostBase { var type = api.PostTypes.GetById(model.TypeId); if (type != null) { // Ensure category if (model.Category.Id == Guid.Empty) { Data.Category category = null; if (!string.IsNullOrWhiteSpace(model.Category.Slug)) { category = api.Categories.GetBySlug(model.BlogId, model.Category.Slug); } if (category == null && !string.IsNullOrWhiteSpace(model.Category.Title)) { category = api.Categories.GetByTitle(model.BlogId, model.Category.Title); } if (category == null) { category = new Data.Category() { Id = Guid.NewGuid(), BlogId = model.BlogId, Title = model.Category.Title }; api.Categories.Save(category); } model.Category.Id = category.Id; } // Ensure tags foreach (var t in model.Tags) { if (t.Id == Guid.Empty) { Data.Tag tag = null; if (!string.IsNullOrWhiteSpace(t.Slug)) { tag = api.Tags.GetBySlug(model.BlogId, t.Slug); } if (tag == null && !string.IsNullOrWhiteSpace(t.Title)) { tag = api.Tags.GetByTitle(model.BlogId, t.Title); } if (tag == null) { tag = new Data.Tag() { Id = Guid.NewGuid(), BlogId = model.BlogId, Title = t.Title }; api.Tags.Save(tag); } t.Id = tag.Id; } } // Ensure that we have a slug if (string.IsNullOrWhiteSpace(model.Slug)) { model.Slug = Utils.GenerateSlug(model.Title, false); } else { model.Slug = Utils.GenerateSlug(model.Slug, false); } var post = db.Posts .Include(p => p.Blocks).ThenInclude(b => b.Block).ThenInclude(b => b.Fields) .Include(p => p.Fields) .Include(p => p.Tags) .FirstOrDefault(p => p.Id == model.Id); // If not, create a new post if (post == null) { post = new Post() { Id = model.Id != Guid.Empty ? model.Id : Guid.NewGuid(), Created = DateTime.Now, LastModified = DateTime.Now }; db.Posts.Add(post); model.Id = post.Id; } else { post.LastModified = DateTime.Now; } post = contentService.Transform <T>(model, type, post); // Transform blocks var blockModels = model.Blocks; if (blockModels != null && blockModels.Count > 0) { var blocks = contentService.TransformBlocks(blockModels); var current = blocks.Select(b => b.Id).ToArray(); // Delete removed blocks var removed = post.Blocks .Where(b => !current.Contains(b.BlockId) && !b.Block.IsReusable) .Select(b => b.Block); db.Blocks.RemoveRange(removed); // Delete the old page blocks post.Blocks.Clear(); // Now map the new block for (var n = 0; n < blocks.Count; n++) { var block = db.Blocks .Include(b => b.Fields) .FirstOrDefault(b => b.Id == blocks[n].Id); if (block == null) { block = new Block() { Id = blocks[n].Id != Guid.Empty ? blocks[n].Id : Guid.NewGuid(), Created = DateTime.Now }; db.Blocks.Add(block); } block.CLRType = blocks[n].CLRType; block.IsReusable = blocks[n].IsReusable; block.Title = blocks[n].Title; block.LastModified = DateTime.Now; var currentFields = blocks[n].Fields.Select(f => f.FieldId).Distinct(); var removedFields = block.Fields.Where(f => !currentFields.Contains(f.FieldId)); db.BlockFields.RemoveRange(removedFields); foreach (var newField in blocks[n].Fields) { var field = block.Fields.FirstOrDefault(f => f.FieldId == newField.FieldId); if (field == null) { field = new BlockField() { Id = newField.Id != Guid.Empty ? newField.Id : Guid.NewGuid(), BlockId = block.Id, FieldId = newField.FieldId }; db.BlockFields.Add(field); block.Fields.Add(field); } field.SortOrder = newField.SortOrder; field.CLRType = newField.CLRType; field.Value = newField.Value; } // Create the page block post.Blocks.Add(new PostBlock() { Id = Guid.NewGuid(), BlockId = block.Id, Block = block, PostId = post.Id, SortOrder = n }); } } // Remove tags var removedTags = new List <PostTag>(); foreach (var tag in post.Tags) { if (!model.Tags.Any(t => t.Id == tag.TagId)) { removedTags.Add(tag); } } foreach (var removed in removedTags) { post.Tags.Remove(removed); } // Add tags foreach (var tag in model.Tags) { if (!post.Tags.Any(t => t.PostId == post.Id && t.TagId == tag.Id)) { post.Tags.Add(new PostTag() { PostId = post.Id, TagId = tag.Id }); } } db.SaveChanges(); if (cache != null) { RemoveFromCache(post); } } }