Ejemplo n.º 1
0
        public async Task <ActionResult <ArticleDto> > PostArticle(ArticleCreateDto articleCreateDto)
        {
            if (articleCreateDto == null)
            {
                return(BadRequest());
            }

            var article = new Article();

            if (articleCreateDto.Picture == null)
            {
                articleCreateDto.Picture = new ImageCreateDto()
                {
                    IsMain    = true,
                    ImageName = "placeholder.png",
                    ImagePath = "img",
                    AltText   = "img",
                    Caption   = ""
                };
            }

            _mapper.Map(articleCreateDto, article);

            _context.Articles.Add(article);

            await _context.SaveChangesAsync().ConfigureAwait(false);

            return(CreatedAtAction("GetArticle", new { id = article.Id }, _mapper.Map <ArticleDto>(article)));
        }
Ejemplo n.º 2
0
        public ActionResult <ArticleReadDto> CreateArticle([FromBody] ArticleCreateDto articleCreateDto)
        {
            try
            {
                if (articleCreateDto == null)
                {
                    return(BadRequest("Article is null"));
                }

                if (!ModelState.IsValid)
                {
                    return(BadRequest("Invalid model object"));
                }

                var articleModel = _mapper.Map <Article>(articleCreateDto);

                _repository.CreateArticle(articleModel);
                _repository.SaveChanges();

                var articleReadDto = _mapper.Map <ArticleReadDto>(articleModel);

                return(CreatedAtRoute(nameof(GetArticleById), new { Id = articleReadDto.Id }, articleReadDto));
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Logging {ex}");
                return(StatusCode(500, "Internal server error"));
            }
        }
Ejemplo n.º 3
0
        public ActionResult UpdateArticle(int id, ArticleUpdateDto articleUpdateDto)
        {
            var articleModelFromRepo = _repository.GetArticleById(id);

            var processedData = new ArticleCreateDto
            {
                Image         = articleUpdateDto.Image,
                Content       = WebUtility.HtmlEncode(articleUpdateDto.Content),
                Name          = articleUpdateDto.Name,
                ContentIntro  = articleUpdateDto.ContentIntro,
                AuthorImg     = articleModelFromRepo.AuthorImg,
                AuthorName    = articleModelFromRepo.AuthorName,
                DateofPublish = articleModelFromRepo.DateofPublish,
                AuthorId      = articleModelFromRepo.AuthorId
            };

            if (articleModelFromRepo == null)
            {
                return(NotFound());
            }
            _mapper.Map(processedData, articleModelFromRepo);
            _repository.UpdateArticle(articleModelFromRepo);
            _repository.SaveChanges();

            return(Ok(articleModelFromRepo));
        }
 public ActionResult CreateArticle(ArticleCreateDto articleCreateDto)
 {
     _repositoryArticle.CreateArticle(_mapper.Map <Article>(articleCreateDto));
     _repositoryArticle.saveChange();
     return(StatusCode(StatusCodes.Status200OK, new
     {
         status = "Success",
         message = "Article has been added"
     }));
 }
Ejemplo n.º 5
0
        public ActionResult <ArticleReadDto> CreateArticle(ArticleCreateDto articleCreateDto)
        {
            var ArticleModel = _mapper.Map <Article>(articleCreateDto);

            _repository.CreateArticle(ArticleModel);
            _repository.SaveChanges();

            var articleReadDto = _mapper.Map <ArticleReadDto>(ArticleModel);

            return(CreatedAtRoute(nameof(GetArticleById), new { Id = ArticleModel.Id }, articleReadDto));
        }
Ejemplo n.º 6
0
        public async Task <long> CreateArticle(ArticleCreateDto articleCreate)
        {
            var article = _mapper.Map <Article>(articleCreate);

            var user = await _userRepository.GetById(articleCreate.UserId, u => u.Writer);

            article.WriterId = user.Writer.Id;

            await _repository.Insert(article);

            return(article.Id);
        }
        public async Task CheckArticle_ReturnsOk()
        {
            // Arrange
            var articleDto = new ArticleCreateDto
            {
                Content = "string"
            };
            var body = GetBody(articleDto);

            // Act
            var result = await _client.PostAsync($"/articles/check", body);

            var articles = await result.GetDataAsync <List <ArticleDto> >();

            // Assert
            Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
            Assert.That(articles, Is.Not.Null);
        }
Ejemplo n.º 8
0
        public async Task <IActionResult> CreateArticle([FromBody] ArticleCreateDto article)
        {
            var userType      = User.Claims.FirstOrDefault(x => x.Type.Equals("UserType", StringComparison.InvariantCultureIgnoreCase)).Value;
            var userRole      = User.Claims.FirstOrDefault(x => x.Type.Equals("UserRole", StringComparison.InvariantCultureIgnoreCase)).Value;
            var userCompanyId = User.Claims.FirstOrDefault(x => x.Type.Equals("CompanyId", StringComparison.InvariantCultureIgnoreCase)).Value;
            var userName      = User.Claims.FirstOrDefault(x => x.Type.Equals("UserName", StringComparison.InvariantCultureIgnoreCase)).Value;

            if (article != null)
            {
                if (userType == "HelpDesk" && userRole == "Manager")
                {
                    var _article = _mapper.Map <ArticleModel>(article);

                    var user = await _repository.User.GetUserByUserName(userName);

                    if (user != null)
                    {
                        _article.CreatedBy   = user.UserName;
                        _article.ArticleId   = (Guid.NewGuid()).ToString();
                        _article.CreatedDate = DateTime.Now;
                    }

                    _repository.Article.Create(_article);

                    await _repository.Save();

                    var insertArticle = await _repository.Article.GetArticleById(_article.ArticleId);



                    return(Ok(_mapper.Map <ArticleDto>(insertArticle)));
                }
                else
                {
                    return(StatusCode(401, "Unauthorized Access"));
                }
            }
            else
            {
                return(StatusCode(400, "Bad Request"));
            }
        }
Ejemplo n.º 9
0
        public async Task <ArticleDto> AddAsync(ArticleCreateDto dto)
        {
            if (string.IsNullOrEmpty(dto.Title))
            {
                throw new ArticleException(ArticleErrorCodes.ArticleTitleCannotBeNull, "Article Title field is mandatory.", dto);
            }

            if (string.IsNullOrEmpty(dto.Body))
            {
                throw new ArticleException(ArticleErrorCodes.ArticleBodyCannotBeNull, "Article Body field is mandatory.", dto);
            }

            if (dto.CategoryId == Guid.Empty)
            {
                throw new ArticleException(ArticleErrorCodes.ArticleCategoryCannotBeNull, "Article Category Id field is mandatory.", dto);
            }
            else
            {
                var categoryEntity = await _categoryRepository.GetByIdAsync(dto.CategoryId);

                if (categoryEntity == null || categoryEntity.Id == Guid.Empty)
                {
                    throw new ArticleException(ArticleErrorCodes.CategoryCouldNotBeFound, "Article Category Id could not be found..", dto);
                }
            }

            var entity = dto.Adapt <Domain.Article>();

            entity = await _articleRepository.AddAsync(entity);

            if (dto.TagIds != null && dto.TagIds.Any())
            {
                if (!await _tagRepository.CheckAllTagIdsExist(dto.TagIds))
                {
                    throw new ArticleException(ArticleErrorCodes.TagCouldNotBeFound, "Article cannot contain non-existed tagId", dto);
                }

                entity = await _articleRepository.AddTags(entity, dto.TagIds);
            }

            return(entity.Adapt <ArticleDto>());
        }
Ejemplo n.º 10
0
        public async Task <IActionResult> Post(ArticleCreateDto model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var article = mapper.Map <Article>(model);

            article.CreatedDate = DateTime.Now;
            article.UpdatedDate = DateTime.Now;
            await articleRepository.Add(article);

            if (await articleRepository.SaveChanges())
            {
                return(Ok(article));
            }

            return(StatusCode(304));
        }
        public async Task CreateArticle_ReturnsOk()
        {
            // Arrange
            var articleDto = new ArticleCreateDto
            {
                Content = "content"
            };
            var body = GetBody(articleDto);

            // Act
            var result = await _client.PostAsync("/articles", body);

            var article = await result.GetDataAsync <ArticleDto>();

            // Assert
            Assert.AreEqual(HttpStatusCode.OK, result.StatusCode);
            Assert.That(article.Id, Is.Not.Null);

            ArticleId = article.Id;
        }
Ejemplo n.º 12
0
        public async Task <IActionResult> CreateArticleAsync(
            [FromBody] ArticleCreateDto newsArticleCreateDTO)
        {
            if (newsArticleCreateDTO == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var article = _mapper.Map <Article>(newsArticleCreateDTO);

            article.Author = _mapper.Map <Author>(User);

            var newsArticle = await _repository.Add(article);

            return(Ok(_mapper.Map <ArticleDto>(newsArticle)));
        }
        public async Task <ArticleDto> CreateAsync(ArticleCreateDto articleCreateDto)
        {
            var articleToCreate = new Article
            {
                Content = articleCreateDto.Content,
                Tokens  = _articleSimilarityService.Tokenize(articleCreateDto.Content)
            };

            var article = await _bus.Send(new CreateArticleCommand(articleToCreate));

            await _bus.Publish(new ArticleCreatedEvent(article));

            return(new ArticleDto
            {
                Id = article.Id,
                Content = article.Content,
                CreatedTs = article.CreatedTs,
                UpdatedTs = article.UpdatedTs,

                DuplicateIds = await _articleSimilarityService.FindDuplicatesAsync(article)
            });
        }
Ejemplo n.º 14
0
        public ActionResult <ArticleReadDto> CreateArticle(ArticleCreateDto articleCreateDto)
        {
            var convertedData = new ArticleCreateDto
            {
                AuthorImg     = articleCreateDto.AuthorImg,
                Content       = WebUtility.HtmlEncode(articleCreateDto.Content),
                AuthorName    = articleCreateDto.AuthorName,
                Image         = articleCreateDto.Image,
                DateofPublish = DateTime.Now.ToString("dd/MM/yy"),
                Name          = articleCreateDto.Name,
                ContentIntro  = articleCreateDto.ContentIntro,
                AuthorId      = articleCreateDto.AuthorId
            };
            var articleModel = _mapper.Map <Article>(convertedData);

            _repository.CreateArticle(articleModel);
            _repository.SaveChanges();

            var articleReadDto = _mapper.Map <ArticleReadDto>(articleModel);

            return(CreatedAtRoute(nameof(GetArticleById), new { Id = articleReadDto.Id }, articleReadDto));
        }
Ejemplo n.º 15
0
        public ArticleDto Add(ArticleCreateDto dto)
        {
            Article article = _articleDomainService.Create(Mapper.Map <Article>(dto));

            return(Mapper.Map <ArticleDto>(article));
        }
Ejemplo n.º 16
0
 public Task <ArticleDto> Create([FromBody] ArticleCreateDto articleCreateDto)
 {
     return(_articleService.CreateAsync(articleCreateDto));
 }
Ejemplo n.º 17
0
        public async Task <IActionResult> CreateArticle([FromBody] ArticleCreateDto createArticle)
        {
            var id = await _articleService.CreateArticle(createArticle);

            return(Created($"api/articles/{id}", id));
        }
Ejemplo n.º 18
0
 public async Task <IActionResult> Create(ArticleCreateDto articleDto)
 {
     return(ActionResultInstance(await _articleService.AddAsync(ObjectMapper.Mapper.Map <ArticleDto>(articleDto))));
 }
Ejemplo n.º 19
0
 public ArticleDto Add([FromBody] ArticleCreateDto dto)
 {
     return(_articleAppService.Add(dto));
 }
Ejemplo n.º 20
0
        public ActionResult <ArticleDto> Post([FromBody] ArticleCreateDto dto)
        {
            var article = _app.Add(dto);

            return(CreatedAtAction(nameof(Get), new { id = article.Id }, article));
        }
Ejemplo n.º 21
0
        public async Task <IActionResult> CreateArticle([FromBody] ArticleCreateDto model)
        {
            var result = await _articleService.AddAsync(model);

            return(CreatedAtAction(nameof(GetArticleById), new { id = result.Id }, null));
        }
Ejemplo n.º 22
0
 public Task <List <ArticleDto> > Check([FromBody] ArticleCreateDto body)
 {
     return(_articleService.CheckDuplicatesAsync(body.Content));
 }