Exemplo n.º 1
0
        public ActionResult Index()
        {
            HomePageDTO homePageDTO = _commonService.GetHomePage();

            IMapper homePageMapper = new MapperConfiguration(cfg =>
            {
                cfg.CreateMap <HomePageDTO, HomePageViewModel>();
                cfg.CreateMap <ProductPageDTO, ProductPageViewModel>();
                cfg.CreateMap <ImageDTO, ImageViewModel>();
            }).CreateMapper();
            HomePageViewModel model = homePageMapper.Map <HomePageViewModel>(homePageDTO)
                                      ?? new HomePageViewModel {
                Title = "Главная страница"
            };

            if (_siteSettings.TryGetValue("HomeSliderId", out string bannerGroupIdString))
            {
                if (int.TryParse(bannerGroupIdString, out int bannerGroupId))
                {
                    BannerGroupDTO bannerGroupDTO = _bannerService.GetBannerGroup(bannerGroupId);

                    IMapper BannerGroupMapper = new MapperConfiguration(cfg =>
                    {
                        cfg.CreateMap <BannerGroupDTO, BannerGroupViewModel>();
                        cfg.CreateMap <BannerDTO, BannerViewModel>();
                        cfg.CreateMap <ImageDTO, ImageViewModel>();
                    }).CreateMapper();
                    BannerGroupViewModel bannerGroup = BannerGroupMapper.Map <BannerGroupViewModel>(bannerGroupDTO);
                    ViewBag.HomeBannerGroup = bannerGroup;
                }
            }


            if (_siteSettings.TryGetValue("SmallBannerId", out string bannerIdString))
            {
                if (int.TryParse(bannerIdString, out int singleBannerId))
                {
                    BannerDTO bannerDTO = _bannerService.GetBanner(singleBannerId);

                    IMapper BannerMapper = new MapperConfiguration(cfg =>
                    {
                        cfg.CreateMap <BannerDTO, BannerViewModel>();
                        cfg.CreateMap <ImageDTO, ImageViewModel>();
                    }).CreateMapper();
                    BannerViewModel banner = BannerMapper.Map <BannerViewModel>(bannerDTO);
                    ViewBag.SingleBanner = banner;
                }
            }

            ViewBag.Title = model.Title;

            return(View(model));
        }
Exemplo n.º 2
0
        public BannerGroupDTO GetBannerGroup(int bannerGroupId)
        {
            BannerGroup bannerGroup = _dbContext.BannerGroups.Include(bg => bg.Banners).ThenInclude(b => b.Image).FirstOrDefault(bg => bg.Id == bannerGroupId);

            if (bannerGroup == null)
            {
                return(null);
            }

            IMapper mapper = new MapperConfiguration(cfg =>
            {
                cfg.CreateMap <BannerGroup, BannerGroupDTO>();
                cfg.CreateMap <Banner, BannerDTO>();
                cfg.CreateMap <Image, ImageDTO>();
            }).CreateMapper();

            BannerGroupDTO bannerGroupDTO = mapper.Map <BannerGroupDTO>(bannerGroup);

            return(bannerGroupDTO);
        }