<commit_before>﻿using Blog.Models;
using Blog.Models.Data;
using Blog.Models.PostViewModels;
using Blog.ViewModels;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;


namespace Blog.Controllers
{

    sealed public class PostController : Controller
    {
        private ITagService tagService { get; }

        private IPostService postService { get; }

        public PostController(IPostService repository,
                              ITagService tagService)
        {
            this.postService = repository;
            this.tagService = tagService;
        }

        [HttpGet("Open/{id}")]
        public IActionResult OpenPost(int id)
        {
            var post = postService.GetPostById(id);

            var postViewModel = ModelFactory.Create(post);

            return View(postViewModel);
        }

        [Authorize(Roles ="Admin, User")]
        [HttpGet]
        public IActionResult CreatePost()
        {
            var postCreateViewModel  = new PostCreateViewModel();
           
            return View(postCreateViewModel);
        }

        [Authorize(Roles = "Admin, User")]
        [HttpPost]
        public IActionResult CreatePost(PostCreateViewModel viewModel) 
        {
            if (ModelState.IsValid)
            {
                
 
                var newPost             = ModelFactory.Create(viewModel);
                    newPost.Author      = User.Identity.Name;
                    newPost.PostedOn    = DateTime.UtcNow;
                    newPost.IsPublished = false;

             
                tagService.AddTags(newPost.Tags);
                postService.AddPost(newPost);
                postService.SaveAll();
              

                return RedirectToActionPermanent("OpenPost", new { newPost, newPost.Id });
            }

            return NotFound();
        }

    }
}<commit_msg>Added API for CreatePost view<commit_after>﻿using Blog.Models;
using Blog.Models.Data;
using Blog.Models.PostViewModels;
using Blog.ViewModels;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;


namespace Blog.Controllers
{

    sealed public class PostController : Controller
    {
        private ITagService tagService { get; }

        private IPostService postService { get; }

        public PostController(IPostService repository,
                              ITagService tagService)
        {
            this.postService = repository;
            this.tagService = tagService;
        }

        [HttpGet("Open/{id}")]
        public IActionResult OpenPost(int id)
        {
            var post = postService.GetPostById(id);

            var postViewModel = ModelFactory.Create(post);

            return View(postViewModel);
        }


        [Authorize(Roles = "Admin, User")]
        public IActionResult CreatePost()
        {
            return View();
        }

        [Authorize(Roles ="Admin, User")]
        [HttpGet("api/post/create")]
        public IActionResult GetPostForm()
        {
            var postCreateViewModel  = new PostCreateViewModel();
           
            return Json(postCreateViewModel);
        }


        [Authorize(Roles = "Admin, User")]
        [HttpPost("api/post/submit")]
        public IActionResult GetnewPost([FromBody]PostCreateViewModel viewModel) 
        {
            if (ModelState.IsValid)
            {
                
 
                var newPost             = ModelFactory.Create(viewModel);
                    newPost.Author      = User.Identity.Name;
                    newPost.PostedOn    = DateTime.UtcNow;
                    newPost.IsPublished = false;

             
                tagService.AddTags(newPost.Tags);
                postService.AddPost(newPost);
                postService.SaveAll();
              

                return RedirectToActionPermanent("OpenPost", new { newPost, newPost.Id });
            }

            return BadRequest();
        }

    }
}
{'fraction_non_alphanumeric': 0.08383383383383383, 'fraction_numerical': 0.0, 'mean_word_length': 1.6735785953177258, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5384420', 'n_tokens_mistral': 1036, 'n_tokens_neox': 981, 'n_words': 198}