Exemplo n.º 1
0
        public async Task <ActionResult <BlogPostViewModel> > Update([FromRoute] string slug, [FromBody] BlogPost newPost)
        {
            System.Diagnostics.Debug.WriteLine("pokemoni", newPost);
            var existingPost = await _repo.Get(slug);

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

            if (newPost.Title == null)
            {
                newPost.Title = existingPost.Title;

                string s = existingPost.Title.ToLower().Normalize(NormalizationForm.FormD);
                s            = Regex.Replace(s, @"\s+", "-");
                s            = Regex.Replace(s, "[^0-9A-Za-z -]", "");
                s            = RemoveAccented.RemoveDiacritics(s);
                newPost.Slug = s;
            }
            else
            {
                string s = newPost.Title.ToLower().Normalize(NormalizationForm.FormD);
                s            = Regex.Replace(s, @"\s+", "-");
                s            = Regex.Replace(s, "[^0-9A-Za-z -]", "");
                s            = RemoveAccented.RemoveDiacritics(s);
                newPost.Slug = s;
            }

            if (newPost.Description == null)
            {
                newPost.Description = existingPost.Description;
            }

            if (newPost.Body == null)
            {
                newPost.Body = existingPost.Body;
            }

            newPost.CreatedAt = existingPost.CreatedAt;
            newPost.UpdatedAt = DateTime.Now;

            _context.Remove(existingPost);

            _context.Add(newPost);

            var updatedPost = await _repo.SaveAsync(newPost);

            System.Diagnostics.Debug.WriteLine("pokemoni", newPost.ToString());
            return(Ok(updatedPost));
        }