public async Task DeleteAsync(CreateOrDeleteContentTaxonomyInput input)
        {
            var contentTaxonomy = new ContentTaxonomy {
                IdContent = input.IdContent, IdTaxonomy = input.IdTaxonomy
            };

            _context.Entry(contentTaxonomy).State = EntityState.Deleted;
            await _context.SaveChangesAsync();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Saves the given content model
        /// </summary>
        /// <param name="model">The content model</param>
        /// <param name="languageId">The selected language id</param>
        public async Task Save <T>(T model, Guid languageId) where T : Models.GenericContent
        {
            var type         = App.ContentTypes.GetById(model.TypeId);
            var lastModified = DateTime.MinValue;

            if (type != null)
            {
                // Ensure category
                if (type.UseCategory)
                {
                    if (model is Models.ICategorizedContent categorized)
                    {
                        var category = await _db.Taxonomies
                                       .FirstOrDefaultAsync(c => c.Id == categorized.Category.Id)
                                       .ConfigureAwait(false);

                        if (category == null)
                        {
                            if (!string.IsNullOrWhiteSpace(categorized.Category.Slug))
                            {
                                category = await _db.Taxonomies
                                           .FirstOrDefaultAsync(c => c.GroupId == type.Group && c.Slug == categorized.Category.Slug && c.Type == TaxonomyType.Category)
                                           .ConfigureAwait(false);
                            }
                            if (category == null && !string.IsNullOrWhiteSpace(categorized.Category.Title))
                            {
                                category = await _db.Taxonomies
                                           .FirstOrDefaultAsync(c => c.GroupId == type.Group && c.Title == categorized.Category.Title && c.Type == TaxonomyType.Category)
                                           .ConfigureAwait(false);
                            }

                            if (category == null)
                            {
                                category = new Taxonomy
                                {
                                    Id           = categorized.Category.Id != Guid.Empty ? categorized.Category.Id : Guid.NewGuid(),
                                    GroupId      = type.Group,
                                    Type         = TaxonomyType.Category,
                                    Title        = categorized.Category.Title,
                                    Slug         = Utils.GenerateSlug(categorized.Category.Title),
                                    Created      = DateTime.Now,
                                    LastModified = DateTime.Now
                                };
                                await _db.Taxonomies.AddAsync(category).ConfigureAwait(false);
                            }
                            categorized.Category.Id    = category.Id;
                            categorized.Category.Title = category.Title;
                            categorized.Category.Slug  = category.Slug;
                        }
                    }
                }

                // Ensure tags
                if (type.UseTags)
                {
                    if (model is Models.ITaggedContent tagged)
                    {
                        foreach (var t in tagged.Tags)
                        {
                            var tag = await _db.Taxonomies
                                      .FirstOrDefaultAsync(tg => tg.Id == t.Id)
                                      .ConfigureAwait(false);

                            if (tag == null)
                            {
                                if (!string.IsNullOrWhiteSpace(t.Slug))
                                {
                                    tag = await _db.Taxonomies
                                          .FirstOrDefaultAsync(tg => tg.GroupId == type.Group && tg.Slug == t.Slug && tg.Type == TaxonomyType.Tag)
                                          .ConfigureAwait(false);
                                }
                                if (tag == null && !string.IsNullOrWhiteSpace(t.Title))
                                {
                                    tag = await _db.Taxonomies
                                          .FirstOrDefaultAsync(tg => tg.GroupId == type.Group && tg.Title == t.Title && tg.Type == TaxonomyType.Tag)
                                          .ConfigureAwait(false);
                                }

                                if (tag == null)
                                {
                                    tag = new Taxonomy
                                    {
                                        Id           = t.Id != Guid.Empty ? t.Id : Guid.NewGuid(),
                                        GroupId      = type.Group,
                                        Type         = TaxonomyType.Tag,
                                        Title        = t.Title,
                                        Slug         = Utils.GenerateSlug(t.Title),
                                        Created      = DateTime.Now,
                                        LastModified = DateTime.Now
                                    };
                                    await _db.Taxonomies.AddAsync(tag).ConfigureAwait(false);
                                }
                                t.Id = tag.Id;
                            }
                            t.Title = tag.Title;
                            t.Slug  = tag.Slug;
                        }
                    }
                }

                var content = await _db.Content
                              .Include(c => c.Translations)
                              .Include(c => c.Blocks).ThenInclude(b => b.Fields).ThenInclude(f => f.Translations)
                              .Include(c => c.Fields).ThenInclude(f => f.Translations)
                              .Include(c => c.Category)
                              .Include(c => c.Tags).ThenInclude(t => t.Taxonomy)
                              .AsSplitQuery()
                              .FirstOrDefaultAsync(p => p.Id == model.Id)
                              .ConfigureAwait(false);

                // If not, create new content
                if (content == null)
                {
                    content = new Content
                    {
                        Id           = model.Id != Guid.Empty ? model.Id : Guid.NewGuid(),
                        Created      = DateTime.Now,
                        LastModified = DateTime.Now
                    };
                    model.Id = content.Id;

                    await _db.Content.AddAsync(content).ConfigureAwait(false);
                }
                else
                {
                    content.LastModified = DateTime.Now;
                }
                content = _service.Transform <T>(model, type, content, languageId);

                // Process fields
                foreach (var field in content.Fields)
                {
                    // Ensure foreign key for new fields
                    if (field.ContentId == Guid.Empty)
                    {
                        field.ContentId = content.Id;
                        await _db.ContentFields.AddAsync(field).ConfigureAwait(false);
                    }
                }

                if (type.UseTags)
                {
                    if (model is Models.ITaggedContent taggedModel)
                    {
                        // Remove tags
                        var removedTags = new List <ContentTaxonomy>();
                        foreach (var tag in content.Tags)
                        {
                            if (!taggedModel.Tags.Any(t => t.Id == tag.TaxonomyId))
                            {
                                removedTags.Add(tag);
                            }
                        }
                        foreach (var removed in removedTags)
                        {
                            content.Tags.Remove(removed);
                        }

                        // Add tags
                        foreach (var tag in taggedModel.Tags)
                        {
                            if (!content.Tags.Any(t => t.ContentId == content.Id && t.TaxonomyId == tag.Id))
                            {
                                var contentTaxonomy = new ContentTaxonomy
                                {
                                    ContentId  = content.Id,
                                    TaxonomyId = tag.Id
                                };
                                content.Tags.Add(contentTaxonomy);
                            }
                        }
                    }
                }

                // Transform blocks
                if (model is Models.IBlockContent blockModel)
                {
                    var blockModels = blockModel.Blocks;

                    if (blockModels != null)
                    {
                        var blocks  = _service.TransformContentBlocks(blockModels, languageId);
                        var current = blocks.Select(b => b.Id).ToArray();

                        // Delete removed blocks
                        var removed = content.Blocks
                                      .Where(b => !current.Contains(b.Id) && b.ParentId == null)
                                      .ToList();
                        var removedItems = content.Blocks
                                           .Where(b => !current.Contains(b.Id) && b.ParentId != null) // && removed.Select(p => p.Id).ToList().Contains(b.ParentId.Value))
                                           .ToList();

                        _db.ContentBlocks.RemoveRange(removed);
                        _db.ContentBlocks.RemoveRange(removedItems);

                        // Map the new block
                        for (var n = 0; n < blocks.Count; n++)
                        {
                            var block = content.Blocks.FirstOrDefault(b => b.Id == blocks[n].Id);

                            if (block == null)
                            {
                                block = new ContentBlock
                                {
                                    Id = blocks[n].Id != Guid.Empty ? blocks[n].Id : Guid.NewGuid()
                                };
                                await _db.ContentBlocks.AddAsync(block).ConfigureAwait(false);
                            }
                            block.ParentId  = blocks[n].ParentId;
                            block.SortOrder = n;
                            block.CLRType   = blocks[n].CLRType;

                            var currentFields = blocks[n].Fields.Select(f => f.FieldId).Distinct();
                            var removedFields = block.Fields.Where(f => !currentFields.Contains(f.FieldId));

                            _db.ContentBlockFields.RemoveRange(removedFields);

                            foreach (var newField in blocks[n].Fields)
                            {
                                var field = block.Fields.FirstOrDefault(f => f.FieldId == newField.FieldId);
                                if (field == null)
                                {
                                    field = new ContentBlockField
                                    {
                                        Id      = newField.Id != Guid.Empty ? newField.Id : Guid.NewGuid(),
                                        BlockId = block.Id,
                                        FieldId = newField.FieldId
                                    };
                                    await _db.ContentBlockFields.AddAsync(field).ConfigureAwait(false);

                                    block.Fields.Add(field);
                                }
                                field.SortOrder = newField.SortOrder;
                                field.CLRType   = newField.CLRType;
                                field.Value     = newField.Value;

                                foreach (var newTranslation in newField.Translations)
                                {
                                    var translation = field.Translations.FirstOrDefault(t => t.LanguageId == newTranslation.LanguageId);
                                    if (translation == null)
                                    {
                                        translation = new ContentBlockFieldTranslation
                                        {
                                            FieldId    = field.Id,
                                            LanguageId = languageId
                                        };
                                        await _db.ContentBlockFieldTranslations.AddAsync(translation).ConfigureAwait(false);

                                        field.Translations.Add(translation);
                                    }
                                    translation.Value = newTranslation.Value;
                                }
                            }
                            content.Blocks.Add(block);
                        }
                    }
                }

                await _db.SaveChangesAsync().ConfigureAwait(false);

                /*
                 * TODO
                 *
                 * await DeleteUnusedCategories(model.BlogId).ConfigureAwait(false);
                 * await DeleteUnusedTags(model.BlogId).ConfigureAwait(false);
                 */
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Saves the given content model
        /// </summary>
        /// <param name="model">The content model</param>
        /// <param name="languageId">The selected language id</param>
        public async Task Save <T>(T model, Guid languageId) where T : Models.GenericContent
        {
            var type         = App.ContentTypes.GetById(model.TypeId);
            var lastModified = DateTime.MinValue;

            if (type != null)
            {
                // Ensure category
                if (model is Models.ICategorizedContent categorized)
                {
                    var category = await _db.Taxonomies
                                   .FirstOrDefaultAsync(c => c.Id == categorized.Category.Id)
                                   .ConfigureAwait(false);

                    if (category == null)
                    {
                        if (!string.IsNullOrWhiteSpace(categorized.Category.Slug))
                        {
                            category = await _db.Taxonomies
                                       .FirstOrDefaultAsync(c => c.GroupId == type.Group && c.Slug == categorized.Category.Slug && c.Type == TaxonomyType.Category)
                                       .ConfigureAwait(false);
                        }
                        if (category == null && !string.IsNullOrWhiteSpace(categorized.Category.Title))
                        {
                            category = await _db.Taxonomies
                                       .FirstOrDefaultAsync(c => c.GroupId == type.Group && c.Title == categorized.Category.Title && c.Type == TaxonomyType.Category)
                                       .ConfigureAwait(false);
                        }

                        if (category == null)
                        {
                            category = new Taxonomy
                            {
                                Id           = categorized.Category.Id != Guid.Empty ? categorized.Category.Id : Guid.NewGuid(),
                                GroupId      = type.Group,
                                Type         = TaxonomyType.Category,
                                Title        = categorized.Category.Title,
                                Slug         = Utils.GenerateSlug(categorized.Category.Title),
                                Created      = DateTime.Now,
                                LastModified = DateTime.Now
                            };
                            await _db.Taxonomies.AddAsync(category).ConfigureAwait(false);
                        }
                        categorized.Category.Id    = category.Id;
                        categorized.Category.Title = category.Title;
                        categorized.Category.Slug  = category.Slug;
                    }
                }

                // Ensure tags
                if (model is Models.ITaggedContent tagged)
                {
                    foreach (var t in tagged.Tags)
                    {
                        var tag = await _db.Taxonomies
                                  .FirstOrDefaultAsync(tg => tg.Id == t.Id)
                                  .ConfigureAwait(false);

                        if (tag == null)
                        {
                            if (!string.IsNullOrWhiteSpace(t.Slug))
                            {
                                tag = await _db.Taxonomies
                                      .FirstOrDefaultAsync(tg => tg.GroupId == type.Group && tg.Slug == t.Slug && tg.Type == TaxonomyType.Tag)
                                      .ConfigureAwait(false);
                            }
                            if (tag == null && !string.IsNullOrWhiteSpace(t.Title))
                            {
                                tag = await _db.Taxonomies
                                      .FirstOrDefaultAsync(tg => tg.GroupId == type.Group && tg.Title == t.Title && tg.Type == TaxonomyType.Tag)
                                      .ConfigureAwait(false);
                            }

                            if (tag == null)
                            {
                                tag = new Taxonomy
                                {
                                    Id           = t.Id != Guid.Empty ? t.Id : Guid.NewGuid(),
                                    GroupId      = type.Group,
                                    Type         = TaxonomyType.Tag,
                                    Title        = t.Title,
                                    Slug         = Utils.GenerateSlug(t.Title),
                                    Created      = DateTime.Now,
                                    LastModified = DateTime.Now
                                };
                                await _db.Taxonomies.AddAsync(tag).ConfigureAwait(false);
                            }
                            t.Id = tag.Id;
                        }
                        t.Title = tag.Title;
                        t.Slug  = tag.Slug;
                    }
                }

                var content = await _db.Content
                              .Include(c => c.Translations)
                              .Include(c => c.Fields).ThenInclude(f => f.Translations)
                              .Include(c => c.Category)
                              .Include(c => c.Tags).ThenInclude(t => t.Taxonomy)
                              .AsSplitQuery()
                              .FirstOrDefaultAsync(p => p.Id == model.Id)
                              .ConfigureAwait(false);

                // If not, create new content
                if (content == null)
                {
                    content = new Content
                    {
                        Id           = model.Id != Guid.Empty ? model.Id : Guid.NewGuid(),
                        Created      = DateTime.Now,
                        LastModified = DateTime.Now
                    };
                    model.Id = content.Id;

                    await _db.Content.AddAsync(content).ConfigureAwait(false);
                }
                else
                {
                    content.LastModified = DateTime.Now;
                }
                content = _service.Transform <T>(model, type, content, languageId);

                // Process fields
                foreach (var field in content.Fields)
                {
                    // Ensure foreign key for new fields
                    if (field.ContentId == Guid.Empty)
                    {
                        field.ContentId = content.Id;
                        await _db.ContentFields.AddAsync(field).ConfigureAwait(false);
                    }
                }

                if (model is Models.ITaggedContent taggedModel)
                {
                    // Remove tags
                    var removedTags = new List <ContentTaxonomy>();
                    foreach (var tag in content.Tags)
                    {
                        if (!taggedModel.Tags.Any(t => t.Id == tag.TaxonomyId))
                        {
                            removedTags.Add(tag);
                        }
                    }
                    foreach (var removed in removedTags)
                    {
                        content.Tags.Remove(removed);
                    }

                    // Add tags
                    foreach (var tag in taggedModel.Tags)
                    {
                        if (!content.Tags.Any(t => t.ContentId == content.Id && t.TaxonomyId == tag.Id))
                        {
                            var contentTaxonomy = new ContentTaxonomy
                            {
                                ContentId  = content.Id,
                                TaxonomyId = tag.Id
                            };
                            content.Tags.Add(contentTaxonomy);
                        }
                    }
                }

                await _db.SaveChangesAsync().ConfigureAwait(false);

                /*
                 * TODO
                 *
                 * await DeleteUnusedCategories(model.BlogId).ConfigureAwait(false);
                 * await DeleteUnusedTags(model.BlogId).ConfigureAwait(false);
                 */
            }
        }