Ejemplo n.º 1
0
        public async Task <ClientArticle> CreateArticle(ArticleChange change, ClaimsPrincipal authUser)
        {
            var user = await userRepository.FindById(authUser.FindFirst(ClaimTypes.NameIdentifier).Value);

            if (user == null)
            {
                throw new ArgumentException("User not found");
            }

            var scheduleTime = DateTime.Now;

            if (change.Schedule != DateTime.MinValue)
            {
                scheduleTime = change.Schedule;
            }

            var sanitizer = new HtmlSanitizer();

            change.Body.Blocks = change.Body.Blocks.Select(block =>
            {
                if (block.Data.Text != null)
                {
                    block.Data.Text = sanitizer.Sanitize(block.Data.Text);
                }
                if (block.Data.Caption != null)
                {
                    block.Data.Caption = sanitizer.Sanitize(block.Data.Caption);
                }

                return(block);
            }).ToList();

            var article = new Article
            {
                Body  = change.Body,
                Title = change.Title,
                Lead  = change.Lead,
                User  = new ArticleUser
                {
                    Id    = user.ID.ToString(),
                    Name  = user.Name,
                    Email = user.Email,
                },
                Cover     = change.Cover,
                CreatedAt = DateTime.Now,
                Schedule  = scheduleTime,
                Author    = change.Author,
                Slug      = new SlugHelper().GenerateSlug(change.Title),
                Tags      = change.Tags,
            };

            await articleRepository.Create(article);

            return(ConvertArticle(article));
        }
Ejemplo n.º 2
0
        public async Task <ActionResult> Update(string id, [FromBody] ArticleChange article)
        {
            try
            {
                await service.Replace(id, article);

                return(Ok());
            }
            catch (Exception)
            {
                return(NotFound("Article not found"));
            }
        }
Ejemplo n.º 3
0
        public async Task <ActionResult <ClientArticle> > Create([FromBody] ArticleChange change)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                return(await service.CreateArticle(change, User));
            }
            catch (Exception e)
            {
                return(BadRequest(e.Message));
            }
        }
Ejemplo n.º 4
0
        public async Task Replace(string id, ArticleChange model)
        {
            var article = await articleRepository.GetById(id);

            if (article is null)
            {
                throw new ArgumentException($"Article with id {id} not found.");
            }

            if (article.Title != model.Title)
            {
                article.Slug = new SlugHelper().GenerateSlug(model.Title);
            }


            var sanitizer = new HtmlSanitizer();

            model.Body.Blocks = model.Body.Blocks.Select(block =>
            {
                if (block.Data.Text != null)
                {
                    block.Data.Text = sanitizer.Sanitize(block.Data.Text);
                }
                if (block.Data.Caption != null)
                {
                    block.Data.Caption = sanitizer.Sanitize(block.Data.Caption);
                }

                return(block);
            }).ToList();

            article.Title  = model.Title;
            article.Lead   = model.Lead;
            article.Body   = model.Body;
            article.Cover  = model.Cover;
            article.Author = model.Author;
            article.Tags   = model.Tags;
            if (model.Schedule != DateTime.MinValue)
            {
                article.Schedule = model.Schedule;
            }
            await articleRepository.Replace(article);
        }