Exemple #1
0
        public override async Task <IBusinessResult <GetByIdProductResponse> > MapperAsync(IPipelineMessage input)
        {
            if (!input.HasContent <IEnumerable <Product> >() || !input.HasContent("Id"))
            {
                AddNotificationProductNotFound();
                return(null);
            }

            var products  = input?.GetContent <IEnumerable <Product> >();
            int productId = input.GetContent <int>("Id");

            var product = products.FirstOrDefault(x => x.Id == productId);

            if (product == null)
            {
                AddNotificationProductNotFound();
                return(null);
            }

            var result = new GetByIdProductResponse()
            {
                Id          = product.Id,
                Name        = product.Name,
                ImageLink   = product.ImageLink,
                Category    = product.Category,
                Description = product.Description,
                Price       = product.Price,
                ProductType = product.ProductType
            };

            return(await Task.FromResult(result.ToBusiness()));
        }
Exemple #2
0
        public override async Task <IPagingResult <ListProductResponse> > MapperAsync(IPipelineMessage input)
        {
            if (!input.HasContent <IEnumerable <Product> >() || !input.HasContent <IPagingCriteria>())
            {
                AddNotificationProductNotFound();
                return(null);
            }

            var products = input.GetContent <IEnumerable <Product> >();
            var paging   = input.GetContent <IPagingCriteria>();

            var items = new List <ListProductResponse>();

            int limit = ApiHelper.GetPagingLimit();

            if (paging.Limit > 0)
            {
                limit = paging.Limit;
            }

            var pagingProducts = products
                                 .Skip(limit * paging.Offset)
                                 .Take(limit);

            foreach (var item in pagingProducts)
            {
                items.Add(new ListProductResponse()
                {
                    Id        = item.Id,
                    Name      = item.Name,
                    Category  = item.Category,
                    ImageLink = item.ImageLink
                });
            }

            var totalCount = products.Count();
            var totalPages = (int)Math.Ceiling((double)totalCount / limit);

            var result = items.ToBusinessPaging(
                new PageResult(limit, paging.Offset, pagingProducts.Count()),
                new SummaryResult(totalCount, totalPages)
                );

            return(await Task.FromResult(result));
        }