public async Task <IActionResult> SubmitArticle([FromBody] CreateArticleDto createArticleDto)
        {
            var articleEntity =                             //Map create article dto to new article entity.
                                Mapper.Map <Article>(createArticleDto);

            articleEntity.ArticlePath =                     //save file to disk and add path to entity.
                                        await _fileRepository
                                        .SaveArticle(createArticleDto.ArticleFile, createArticleDto.UserName);

            articleEntity.Status = Status.Pending;          //Set status to pending
            _dbContext.Articles.Add(articleEntity);         //Add article to database
            if (_dbContext.SaveChanges() >= 0)              //save and redirect to index if successful
            {
                return(RedirectToAction(nameof(Index)));
            }
            throw new Exception("Failed to save article");  //throw new exception on failure.
        }
Exemple #2
0
        public async Task <IActionResult> OnPostAsync([FromForm] CreateArticleDto articleDto)
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }
            var username = _userManager.GetUserName(User);
            var path     = await _articleFileRepository.SaveArticle(articleDto.Article, username);

            var articleEntity = Mapper.Map <Article>(articleDto);

            articleEntity.AuthorId    = _userManager.GetUserId(User);
            articleEntity.ArticleFile = path;
            _articleRepository.Create(articleEntity);
            if (!_articleRepository.Save())
            {
                throw new Exception("Failed to create article database entry.");
            }
            return(RedirectToPage("/Articles/Article", new { id = articleEntity.Id }));
        }
Exemple #3
0
        public async Task <IActionResult> CreateArticle([FromBody] CreateArticleDto createArticleDto)
        {
            //Check ModelState
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            //Check if model bind failed.
            if (createArticleDto == null)
            {
                return(BadRequest());
            }
            //Map to entity
            var articleEntity = Mapper.Map <Article>(createArticleDto);

            //Save file and add path to articleEntity
            articleEntity.ArticlePath =
                await _fileRepository
                .SaveArticle(createArticleDto.ArticleFile, createArticleDto.UserName);

            //Confirm file was saved.
            if (articleEntity.ArticlePath == null)
            {
                return(BadRequest());
            }
            //Add articleEntity to database and save database.
            _dbContext.Articles.Add(articleEntity);
            if (!(await _dbContext.SaveChangesAsync() >= 0))
            {
                throw new Exception("Failed to save Article DB Entry.");
            }
            //Convert entity to GetArticleDto
            var articleToReturn = Mapper.Map <GetArticleDto>(articleEntity);

            //Redirect to GetArticle action
            return(CreatedAtRoute("GetArticle", new { articleId = articleEntity.Id }, articleToReturn));
        }