Ejemplo n.º 1
0
        public async Task <IActionResult> Create([Bind("BlogId,Title,Abstract,Content,PublishState,ImageFile,IsFeatured")] Post post)
        {
            if (ModelState.IsValid)
            {
                //created date
                post.Created = DateTime.Now;

                //upload blog image. If there is no image, default to defaultBlogImage
                post.ImageData = await _blogImageService.EncodeFileAsync(post.ImageFile) ??
                                 await _blogImageService.EncodeFileAsync(_configuration["DefaultPostImage"]);

                post.ContentType = post.ImageFile is null ?
                                   _configuration["DefaultPostImage"].Split('.')[1] :
                                   _blogImageService.ContentType(post.ImageFile);

                //Slug stuff goes here
                var slug = _slugService.UrlFriendly(post.Title);
                if (!_slugService.IsUnique(slug))
                {
                    //I must now add a Model Error and inform the user of the problem
                    ModelState.AddModelError("Title", "There is an issue with the title, please try again");
                    return(View(post));
                }

                post.Slug = slug;
                _context.Add(post);
                await _context.SaveChangesAsync();

                return(RedirectToAction("BlogPostIndex", new { id = post.BlogId }));
            }

            ViewData["BlogId"] = new SelectList(_context.Blogs, "Id", "Description", post.BlogId);
            return(View("BlogPostIndex", post));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Create([Bind("BlogId,Title,Abstract,Content,PublishState,ImageFile")] Post post)
        {
            if (User.Identity.IsAuthenticated)
            {
                ViewData["HeaderText"] = $"Dear {(await _userManager.GetUserAsync(User)).GivenName}";
            }
            else
            {
                ViewData["HeaderText"] = "Dear Coder";
            }

            ViewData["SubheaderText"] = "Please create a Post.";
            if (ModelState.IsValid)
            {
                post.Created = DateTime.Now;

                post.ImageData = (await _fileService.EncodeFileAsync(post.ImageFile)) ??
                                 await _fileService.EncodeFileAsync(_configuration["DefaultPostImage"]);

                post.ContentType = post.ImageFile is null ?
                                   _configuration["DefaultPostImage"].Split('.')[1] :
                                   _fileService.ContentType(post.ImageFile);

                //Add the post's slug

                var slug = _slugService.UrlFriendly(post.Title);
                if (!_slugService.IsUnique(slug))
                {
                    //I must now add a Model Error and inform the user of the problem
                    ModelState.AddModelError("Title", "There is an issue with the Title. Please try again.");
                    ModelState.AddModelError("", "Where does this thing show up?");
                    ModelState.AddModelError("", "How about this one?");
                    return(View(post));
                }

                post.Slug = slug;

                _context.Add(post);
                await _context.SaveChangesAsync();

                return(RedirectToAction("BlogPostIndex", new { id = post.BlogId }));
            }
            ViewData["BlogId"] = new SelectList(_context.Blogs, "Id", "Name", post.BlogId);
            return(View(post));
        }