Example #1
0
        public async Task <Blog> CreateBlog(CreateViewModel createViewModel, ClaimsPrincipal claimsPrincipal)
        {
            Blog blog = createViewModel.Blog;

            blog.Creator = await userManager.GetUserAsync(claimsPrincipal);

            blog.CreatedOn = DateTime.Now;
            blog.UpdatedOn = DateTime.Now;
            blog.UrlSlug   = UrlSlug(createViewModel.Blog.Title);
            blog.Category  = blogServices.GetCategoryById(createViewModel.Blog.Category.Id);
            blog           = await blogServices.Add(blog);

            if (createViewModel.BlogHeaderImage != null)
            {
                string webRootPath = webHostEnvironment.WebRootPath;
                string pathToImage = $@"{webRootPath}\UserFiles\Blogs\{blog.Id}\HeaderImage.jpg";
                EnsureFolder(pathToImage);
                using (var filestream = new FileStream(pathToImage, FileMode.Create))
                {
                    await createViewModel.BlogHeaderImage.CopyToAsync(filestream);
                }
            }
            return(blog);
            //return await blogServices.Add(blog);
        }