public Task UpdateAsync(Domain.ArticleAggregate.Article article)
        {
            ArticleModel target = _dbContext.ArticleModels.Local.FirstOrDefault(am => am.Id == article.Id);

            _dbContext.Entry(ArticleMapper.MapToDataModel(article, target)).State = EntityState.Modified;
            return(Task.CompletedTask);
        }
Example #2
0
        public IEnumerable <ArticleDTO> GetArticlesWithTegFilter(IEnumerable <TegDTO> tegs)
        {
            IEnumerable <Article> articles = _unitOfWork.ArticleRepository.Get(includeProperties: "ArticleTegs");
            List <Article>        result   = new List <Article>();

            foreach (TegDTO teg in tegs)
            {
                Teg tegEntity;
                if (teg.Name != null)
                {
                    tegEntity = _unitOfWork.TegRepository.Get(t => t.Name == teg.Name).FirstOrDefault();
                    if (tegEntity == null)
                    {
                        throw new ArgumentNullException(nameof(tegEntity));
                    }
                }
                else
                {
                    throw new ArgumentNullException(nameof(teg));
                }
                var filtered = articles.Where(a => a.ArticleTegs.Contains(a.ArticleTegs.Where(at => at.ArticleId == a.Id && at.TegId == tegEntity.Id).FirstOrDefault()));
                foreach (Article article in filtered)
                {
                    if (!result.Contains(article))
                    {
                        result.Add(article);
                    }
                }
            }
            return(ArticleMapper.Map(result));
        }
Example #3
0
        public async Task <ArticleDTO> GetArticleById(int id)
        {
            var article = _unitOfWork.ArticleRepository.Get(a => a.Id == id, includeProperties: "Comments,ArticleTegs").FirstOrDefault();

            if (article == null)
            {
                throw new ArgumentNullException(nameof(article));
            }
            var result = ArticleMapper.Map(article);

            if (article.Comments != null && article.Comments.Count > 0)
            {
                result.Comments = new List <CommentDTO>();
                foreach (Comment comment in article.Comments)
                {
                    CommentDTO dto = CommentMapper.Map(comment);
                    dto.CreatorUsername = (await _userManager.FindByIdAsync(comment.UserId)).UserName;
                    result.Comments.Add(dto);
                }
            }
            result.AuthorId       = (await _unitOfWork.BlogRepository.GetByIdAsync(article.BlogId)).OwnerId;
            result.AuthorUsername = (await _userManager.FindByIdAsync(result.AuthorId)).UserName;
            if (article.ArticleTegs != null && article.ArticleTegs.Count > 0)
            {
                result.Tegs = new List <TegDTO>();
                foreach (ArticleTeg teg in article.ArticleTegs)
                {
                    result.Tegs.Add(TegMapper.Map(await _unitOfWork.TegRepository.GetByIdAsync(teg.TegId)));
                }
            }
            return(result);
        }
Example #4
0
        public void MapToViewModelMapsAllProperties()
        {
            // Arrange
            var article = new Article
            {
                Id     = 1,
                Title  = "Test Title",
                Body   = "Test Body",
                Author = new User {
                    UserName = "******"
                },
                DatePublished = new DateTime(2017, 1, 1),
                DateModified  = new DateTime(2017, 1, 2)
            };
            var mapper = new ArticleMapper();

            // Act
            var viewModel = new ArticleViewModel();

            mapper.MapToViewModel(article, viewModel);

            // Assert
            Assert.AreEqual(article.Id, viewModel.Id);
            Assert.AreEqual(article.Title, viewModel.Title);
            Assert.AreEqual(article.Body, viewModel.Body);
            Assert.AreEqual(article.Author.UserName, viewModel.Author);
            Assert.AreEqual(article.DatePublished, new DateTime(2017, 1, 1));
            Assert.AreEqual(article.DateModified, new DateTime(2017, 1, 2));
        }
Example #5
0
        public void MapToModelUpdatesTitleAndBodyOnly()
        {
            // Arrange
            var viewModel = new ArticleViewModel
            {
                Id            = 1,
                Title         = "Test Title",
                Body          = "Test Body",
                Author        = "Test Author",
                DatePublished = new DateTime(2017, 1, 1),
                DateModified  = new DateTime(2017, 1, 2)
            };
            var mapper = new ArticleMapper();

            // Act
            var model = new Article();

            mapper.MapToModel(viewModel, model);

            // Assert
            Assert.AreEqual(default(int), model.Id);
            Assert.AreEqual(viewModel.Title, model.Title);
            Assert.AreEqual(viewModel.Body, model.Body);
            Assert.IsNull(model.Author);
            Assert.AreEqual(default(DateTime), model.DatePublished);
            Assert.AreNotEqual(default(DateTime), model.DateModified);
        }
        public Task AddAsync(Domain.ArticleAggregate.Article article)
        {
            var mappedModel = ArticleMapper.MapToDataModel(article);

            _dbContext.ArticleModels.Add(mappedModel);
            article.SetId(mappedModel.Id);
            return(Task.CompletedTask);
        }
Example #7
0
#pragma warning disable CS1591 // 缺少对公共可见类型或成员“WhisperController.WhisperController(IUserService, UserMapper, IArticleService, ArticleMapper)”的 XML 注释
        public WhisperController(IUserService userService, UserMapper userMapper, IArticleService articleService, ArticleMapper articleMapper)
#pragma warning restore CS1591 // 缺少对公共可见类型或成员“WhisperController.WhisperController(IUserService, UserMapper, IArticleService, ArticleMapper)”的 XML 注释
        {
            this.userService    = userService;
            this.userMapper     = userMapper;
            this.articleService = articleService;
            this.articleMapper  = articleMapper;
        }
Example #8
0
 public ActionResult PublishArticle(ArticleViewModel viewModel)
 {
     if (ModelState.IsValid)
     {
         Service.Publish(ArticleMapper.Map(viewModel));
     }
     return(GetUserArticles());
 }
Example #9
0
        public IEnumerable <ArticleDTO> GetAllArticles()
        {
            var articles = _unitOfWork.ArticleRepository.Get();

            if (articles == null)
            {
                throw new ArgumentNullException(nameof(articles));
            }
            return(ArticleMapper.Map(articles));
        }
Example #10
0
        public IEnumerable <ArticleDTO> GetArticlesWihtTextFilter(string filter)
        {
            var articles = _unitOfWork.ArticleRepository.Get(a => a.Content.Contains(filter) || a.Name.Contains(filter));

            if (articles == null)
            {
                throw new ArgumentNullException(nameof(articles));
            }
            return(ArticleMapper.Map(articles));
        }
Example #11
0
#pragma warning disable CS1591 // 缺少对公共可见类型或成员“IndexController.IndexController(IArticleService, IMessageService, IUserService, ITagService, IConfiguration, ArticleMapper, MessageMapper)”的 XML 注释
        public IndexController(IArticleService articleService, IMessageService messageService, IUserService userService, ITagService tagService, IConfiguration conf, ArticleMapper articleMapper, MessageMapper messageMapper)
#pragma warning restore CS1591 // 缺少对公共可见类型或成员“IndexController.IndexController(IArticleService, IMessageService, IUserService, ITagService, IConfiguration, ArticleMapper, MessageMapper)”的 XML 注释
        {
            this.articleService = articleService;
            this.messageService = messageService;
            this.userService    = userService;
            this.tagService     = tagService;
            this.conf           = conf;
            this.articleMapper  = articleMapper;
            this.messageMapper  = messageMapper;
        }
Example #12
0
        public ActionResult Article(string alias)
        {
            ArticleViewModel article = ArticleMapper.ToViewModel(newsService.News.FirstOrDefault(x => x.Alias == alias));

            if (article == null)
            {
                throw new HttpException(404, "Указанная страница не найдена!");
            }

            return(View(article));
        }
#pragma warning disable CS1591 // 缺少对公共可见类型或成员“DetailsController.DetailsController(IFileAttachmentService, IArticleService, ITagService, ICommentService, IUserService, ArticleMapper, CommentMapper, EmailHelper)”的 XML 注释
        public DetailsController(IFileAttachmentService fileAttachmentService, IArticleService articleService, ITagService tagService, ICommentService commentService, IUserService userService, ArticleMapper articleMapper, CommentMapper commentMapper, EmailHelper emailHelper)
#pragma warning restore CS1591 // 缺少对公共可见类型或成员“DetailsController.DetailsController(IFileAttachmentService, IArticleService, ITagService, ICommentService, IUserService, ArticleMapper, CommentMapper, EmailHelper)”的 XML 注释
        {
            this.fileAttachmentService = fileAttachmentService;
            this.articleService        = articleService;
            this.tagService            = tagService;
            this.commentService        = commentService;
            this.userService           = userService;
            this.articleMapper         = articleMapper;
            this.commentMapper         = commentMapper;
            this.emailHelper           = emailHelper;
        }
Example #14
0
        public override void PreInitialize()
        {
            // 配置权限功能
            Configuration.Authorization.Providers.Add <ZYShopAuthorizationProvider>();
            Configuration.Authorization.Providers.Add <ArticleAuthorizationProvider>();
            Configuration.Authorization.Providers.Add <ArticleClassAuthorizationProvider>();

            // 自定义类型映射
            Configuration.Modules.AbpAutoMapper().Configurators.Add(configuration =>
            {
                ArticleMapper.CreateMappings(configuration);
                ArticleClassMapper.CreateMappings(configuration);
            });
        }
Example #15
0
        public void Map_GivenIsValidArticles_ShouldMapCorrectly()
        {
            var articleMapper    = new ArticleMapper();
            var articleRepositry = new ArticleRepository();

            var articles = articleRepositry.GetAll();

            var articlesDto = articleMapper.Map(articles);

            articlesDto.Should().BeEquivalentTo(articles, options =>
                                                options
                                                .Excluding(art => art.LastModified)
                                                .Excluding(art => art.LastModifiedBy)
                                                );
        }
Example #16
0
        public void Map_GivenIsValidArticle_ShouldMapCorrectly()
        {
            var articleMapper    = new ArticleMapper();
            var articleRepositry = new ArticleRepository();

            var article = articleRepositry.FindById(1001);

            var articleDto = articleMapper.Map(article);

            articleDto.Should().BeEquivalentTo(article, options =>
                                               options
                                               .Excluding(art => art.LastModified)
                                               .Excluding(art => art.LastModifiedBy)
                                               );
        }
Example #17
0
        public async Task <ArticleDTO> CreateArticle(ArticleDTO article, string token)
        {
            if (article == null)
            {
                throw new ArgumentNullException(nameof(article));
            }
            if (article.Name == null)
            {
                throw new ArgumentNullException(nameof(article.Name));
            }
            if (article.Content == null)
            {
                throw new ArgumentNullException(nameof(article.Content));
            }

            string ownerId = (await _unitOfWork.BlogRepository.GetByIdAsync(article.BlogId.GetValueOrDefault())).OwnerId;

            if (ownerId.CompareTo(_jwtFactory.GetUserIdClaim(token)) != 0)
            {
                throw new NotEnoughtRightsException();
            }
            var articleEntity = ArticleMapper.Map(article);

            articleEntity.LastUpdate = DateTime.Now;

            _unitOfWork.ArticleRepository.Insert(articleEntity);
            await _unitOfWork.SaveAsync();

            await AddTegs(articleEntity, article);

            var result = ArticleMapper.Map(articleEntity);

            if (articleEntity.ArticleTegs != null && articleEntity.ArticleTegs.Count > 0)
            {
                result.Tegs = new List <TegDTO>();
                foreach (ArticleTeg teg in articleEntity.ArticleTegs)
                {
                    result.Tegs.Add(TegMapper.Map(_unitOfWork.TegRepository.GetById(teg.TegId)));
                }
            }
            return(result);
        }
Example #18
0
        public void MapToModelUpdatesDatePublishedWhenCreatingNewModel()
        {
            // Arrange
            var viewModel = new ArticleViewModel
            {
                Id            = 1,
                Title         = "Test Title",
                Body          = "Test Body",
                Author        = "Test Author",
                DatePublished = new DateTime(2017, 1, 1),
                DateModified  = new DateTime(2017, 1, 2)
            };
            var mapper = new ArticleMapper();

            // Act
            var model = mapper.MapToModel(viewModel);

            // Assert
            Assert.AreNotEqual(default(DateTime), model.DatePublished);
        }
Example #19
0
        public ActionResult Index(string tag)
        {
            ArticlesViewModel model = new ArticlesViewModel();

            if (TempData["FlagError"] != null && ((bool)TempData["FlagError"]) == true)
            {
                return(View(model));
            }

            var newsSection = sectionService.Sections.FirstOrDefault(x => x.Url == "/articles");

            if (newsSection == null)
            {
                TempData["FlagError"]      = true;
                ViewBag.ServerErrorMessage = "Ошибка сервера 500";

                return(View(model));
            }
            if (!newsSection.IsActive)
            {
                return(Redirect("/"));
            }

            if (Request.Url.Segments.Any(x => x == "index"))
            {
                return(RedirectPermanent($"/news"));
            }

            model.Tags = newsService.Tags.Where(x => x.IsActive && !x.IsDelete);

            var news = newsService.Get(tag);

            if (news != null)
            {
                model.Articles = news.Where(x => x.IsActive && !x.IsDelete)
                                 .Select(x => ArticleMapper.ToViewModel(x));
            }

            return(View(model));
        }
Example #20
0
        public override void PreInitialize()
        {
            IocManager.Register<BlogMongoDbDatabaseProvider>();

            ArticleMapper.CreateMappings();
        }
Example #21
0
        public void MapArticle(ArticleDTO articleDTO)
        {
            var model = ArticleMapper.MapArticle(articleDTO);

            Assert.Equal(articleDTO.Content, model.Content);
        }
 public async Task <Domain.ArticleAggregate.Article> GetByIdAsync(int articleId)
 {
     return(ArticleMapper.MapToDomainModel(
                await _dbContext.ArticleModels.Include(a => a.ArticleTags).FirstOrDefaultAsync(a => a.Id == articleId)
                ));
 }
Example #23
0
        public ActionResult GetArticle(int articleId)
        {
            var article = Service.GetArticle(articleId);

            return(View("RenderTemplates/ArticleView", ArticleMapper.Map(article)));
        }
 public ArticleRepository(IContext context)
 {
     this.Context = context;
     this.Mapper  = new ArticleMapper(context);
 }
Example #25
0
        public void CreateArticle(ArticleDto input)
        {
            var article = ArticleMapper.Map(input);

            _articleRepository.Add(article);
        }
Example #26
0
 public Article CreateModel(ArticleDTO articleDto)
 {
     return(ArticleMapper.MapArticle(articleDto));
 }
Example #27
0
        public void UpdateArticle(ArticleDto input)
        {
            var article = ArticleMapper.Map(input);

            _articleRepository.Update(article);
        }