Esempio n. 1
0
        public async Task <ContentEntry> CreateContentWithCategoryAndTag(ContentEntry content, string categoryName, string tag)
        {
            var categoryEntity = await CategoryAccessor.OneAsync <Categories>(x => x.Name == categoryName);

            if (categoryEntity == null)
            {
                categoryEntity = new Categories
                {
                    Name       = categoryName,
                    CreateTime = DateTime.Now,
                    Id         = Guid.NewGuid()
                };
                await CategoryAccessor.Add(categoryEntity);
            }

            var contentEntry = await ContentAccessor.OneAsync <ContentEntry>(x => x.Id == content.Id);



            if (contentEntry == null)
            {
                contentEntry = new ContentEntry
                {
                    Id         = Guid.NewGuid(),
                    Category   = categoryEntity,
                    Content    = content.Content,
                    CreateTime = DateTime.Now,
                    IsFaq      = content.IsFaq,
                    IsTop      = content.IsTop,
                    Title      = content.Title,
                    Tags       = new List <Tags> {
                        new Tags {
                            Name       = tag,
                            Id         = Guid.NewGuid(),
                            CreateTime = DateTime.Now
                        }
                    },
                    ContentEntryInfo = new ContentEntryInfo
                    {
                        Id            = content.ContentEntryInfo.Id,
                        Author        = content.ContentEntryInfo.Author,
                        Source        = content.ContentEntryInfo.Source,
                        Type          = content.ContentEntryInfo.Type,
                        UserAccountId = content.ContentEntryInfo.UserAccountId
                    }
                };
                await ContentAccessor.Add(contentEntry);

                //if (contentEntry.MediaResource == null)
                //{
                //    contentEntry.MediaResource = new List<FileEntry>();
                //}
                if (content.MediaResource != null)
                {
                    foreach (var item in content.MediaResource)
                    {
                        item.ContentEntryId = contentEntry.Id;
                    }

                    await ContentAccessor.UpdateRange(content.MediaResource);
                }

                ////var mediaResource = ContentAccessor.Entry(content.MediaResource);
                ////contentEntry.MediaResource = mediaResource;
                //await ContentAccessor.Update(contentEntry);
            }
            else
            {
                contentEntry.Category = categoryEntity;
                contentEntry.Content  = content.Content;
                contentEntry.IsFaq    = content.IsFaq;
                contentEntry.IsTop    = content.IsTop;
                contentEntry.Title    = content.Title;
                if (content.MediaResource != null)
                {
                    foreach (var item in content.MediaResource)
                    {
                        item.ContentEntryId = contentEntry.Id;
                    }

                    await ContentAccessor.UpdateRange(content.MediaResource);
                }
                //contentEntry.MediaResource = content.MediaResource;
                await ContentAccessor.Update(contentEntry);
            }

            return(contentEntry);
        }