public async Task <IActionResult> CreateBlog([FromBody] BlogForCreatingDto newBlog) { if (newBlog == null) { return(BadRequest()); } if (ModelState.IsValid) { var slug = urlHelper.ToFriendlyUrl(newBlog.Title); var isUniqueSlug = await unitOfWork.Blogs.IsBlogSlugUniqueAsync(slug); if (isUniqueSlug) { await unitOfWork.Blogs.AddBlog(newBlog); await unitOfWork.SaveAsync(); return(StatusCode(201)); } else { ModelState.AddModelError("Title", "This title has already has been taken."); } } return(new UnprocessableEntityObjectResult(ModelState)); }
public async Task <BlogTable> AddBlog(BlogForCreatingDto newBlogDto) { var blogEntity = mapper.Map <BlogForCreatingDto, BlogTable>(newBlogDto); await context.Blogs .AddAsync(blogEntity); return(blogEntity); }