Exemplo n.º 1
0
        public async Task <BannersGetByMenuIdResponse> BannerGetByMenuId(BannerGetByMenuIdRequest request)
        {
            BannersGetByMenuIdResponse response = new BannersGetByMenuIdResponse();

            try
            {
                var banners = await _bannerService.GetBannerByMenuId(request.MenuId);

                if (banners?.Length > 0)
                {
                    var bannerIds   = banners.Select(p => p.Id).Distinct().ToArray();
                    var bannerItems = (await _bannerService.GetBannerItemByBannerId(bannerIds)).GroupBy(p => p.BannerId).ToDictionary(p => p.Key, p => p.Select(q => q.ToModel()).ToArray());
                    response.Banners = banners.Select(p => p.ToModel()).ToArray();
                    foreach (var bannerViewModel in response.Banners)
                    {
                        bannerViewModel.BannerItems = bannerItems.ContainsKey(bannerViewModel.Id)
                            ? bannerItems[bannerViewModel.Id]
                            : new BannerItemViewModel[0];
                    }
                }
            }
            catch (Exception e)
            {
                response.SetFail(e);
                _logger.LogError(e, e.Message, request);
            }
            return(response);
        }
        public async Task <ICommandResult> Handle(MenuBannerMappingAddCommand message)
        {
            try
            {
                ICommandResult result;
                RMenu          rMenu = await _menuService.Get(message.MenuId);

                if (rMenu == null)
                {
                    result = new CommandResult()
                    {
                        Message      = "Menu not found",
                        ObjectId     = "",
                        Status       = CommandResult.StatusEnum.Fail,
                        ResourceName = ResourceKey.Template_NotFound
                    };
                    return(result);
                }
                RBanner[] rbanners = await _bannerService.GetBannerByMenuId(rMenu.Id);

                Menu    menu   = new Menu(rMenu, rbanners);
                RBanner banner = await _bannerService.GetBannerById(message.BannerId);

                var bannerAdd = menu.AddBanner(banner);
                await _menuService.AddMenuBannerMapping(menu.Id, bannerAdd.Id);

                result = new CommandResult()
                {
                    Message  = "",
                    ObjectId = menu.Id,
                    Status   = CommandResult.StatusEnum.Sucess
                };
                return(result);
            }
            catch (Exception e)
            {
                e.Data["Param"] = message;
                ICommandResult result = new CommandResult()
                {
                    Message = e.Message,
                    Status  = CommandResult.StatusEnum.Fail
                };
                return(result);
            }
        }