Esempio n. 1
0
        public async Task <string> Publish(PublishArticle article)
        {
            var sb = new StringBuilder();

            foreach (var slice in article.Slices)
            {
                if (slice.Type == PublishArticleSliceType.String)
                {
                    sb.Append(slice.Value);
                    continue;
                }

                if (slice.Type == PublishArticleSliceType.Image)
                {
                    sb.Append($"{article.BaseUrl}/{slice.Value}");
                }
            }

            var content = await _entitiesService.All().Include(c => c.ContentCaches).FirstOrDefaultAsync(c => c.Id == article.Id);

            var existed = content != null;

            if (!existed)
            {
                content = new Content
                {
                    Id      = article.Id,
                    Created = DateTime.Now
                };
            }

            content.Updated = DateTime.Now;
            content.Name    = article.Title;
            content.Group   = article.Group;
            content.Data    = sb.ToString();
            content.Url     = article.Url;
            if (existed)
            {
                content = await _entitiesService.Update(content);

                var files = content.ContentCaches == null ? null : content.ContentCaches.Select(c => c.Content).ToList();
                if (files != null && files.Count > 0)
                {
                    files.ForEach(f => _fileService.Delete(f));
                }
            }
            else
            {
                content = await _entitiesService.Add(content);
            }
            return(content.Id.ToString());
        }
Esempio n. 2
0
        public async Task <object> Get(string templateName, string articleId)
        {
            var template = await GetTemplate(templateName);

            if (template == null || !Guid.TryParse(articleId, out var id))
            {
                return(NotFound());
            }

            var item = await _contentsService.All().Include(c => c.ContentCaches)
                       .FirstOrDefaultAsync(c => c.Id == id);

            if (item == null)
            {
                return(NotFound());
            }

            var cache = item.ContentCaches.FirstOrDefault(c => c.TemplateId == template.Id);

            if (cache != null)
            {
                return(Redirect($"/{cache.Content}"));
            }

            var cacheContent = $"{HtmlPrefix(item.Name)}\n{template.ArticlePrefix}\n{item.Data}\n{template.ArticleSurfix}\n{HtmlSurfix}\n";

            var cacheId = await _filesService.Add(_resourcesGroup, cacheContent, "html");

            cache = new ContentCache
            {
                ContentId  = item.Id,
                TemplateId = template.Id,
                Content    = cacheId,
                Updated    = DateTime.Now
            };

            item.ContentCaches ??= new List <ContentCache>();

            item.ContentCaches.Add(cache);

            await _contentsService.Update(item);

            return(Content(cacheContent, "text/html", Encoding.Default));
        }