示例#1
0
        public async Task <ActionResult <Pagination <ProductoDto> > > GetProductos([FromQuery] ProductoSpecificationParams productoParams)
        {
            var spec = new ProductoWithCategoriaAndMarcaSpecification(productoParams);

            var productos = await _productoRepository.GetAllWithSpec(spec);

            var specCount = new ProductoForCountingSpecification(productoParams);

            var totalProductos = await _productoRepository.CountAsync(specCount);

            var rounded = Math.Ceiling(Convert.ToDecimal(totalProductos / productoParams.PageSize));

            var totalPages = Convert.ToInt32(rounded);

            var data = _mapper.Map <IReadOnlyList <Producto>, IReadOnlyList <ProductoDto> >(productos);

            return(Ok(
                       new Pagination <ProductoDto>
            {
                Count = totalProductos,
                Data = data,
                PageCount = totalPages,
                PageIndex = productoParams.PageIndex,
                PageSize = productoParams.PageSize
            }));
        }
        public async Task <ActionResult <Pagination <Producto> > > GetProductosAll([FromQuery] ProductoSpecificationParams productoSpecificationParams)
        {
            // spec = debe incluir la logica de la condicion de la consulta y tambien las relaciones entre las entidades
            // en este caso seria la relacion, entre producto y marca, categoria
            var spec      = new ProductoWithCategoriaAndMarcaSpecification(productoSpecificationParams);
            var productos = await _productoRepository.GetAllWithSpec(spec);

            var specCount      = new ProductoForCountingSpecification(productoSpecificationParams);
            var totalProductos = await _productoRepository.CountAsync(specCount);

            var rounded    = Math.Ceiling(Convert.ToDecimal(totalProductos / productoSpecificationParams.PageSize));
            var totalPages = Convert.ToInt32(rounded);

            var data = _mapper.Map <IReadOnlyList <Producto>, IReadOnlyList <ProductoDto> >(productos);

            var result = new Pagination <ProductoDto>
            {
                Count     = totalProductos,
                Data      = data,
                PageCount = totalPages,
                PageIndex = productoSpecificationParams.PageIndex,
                PageSize  = productoSpecificationParams.PageSize
            };

            return(Ok(result));
        }
        public async Task <ActionResult <Pagination <Producto> > > getProductos([FromQuery] ProductoSpecificationParams productoParams) //FromQuery para decir que los parametros vienen desde url del endpoint
        {
            //var productos = await _productoRepository.getProductos();
            //var productos = await _productoRepository.getAllAsync(); //Métodos genericos sin la relación
            var spec      = new ProductoWithCategoriasAndMarcaSpecification(productoParams);
            var productos = await _productoRepository.getAllWithSpec(spec);

            var specCount      = new ProductoForCountingSpecification(productoParams);
            var totalProductos = await _productoRepository.countAsync(specCount); //Devuelve la cantidad de elemento

            var rounded    = Math.Ceiling(Convert.ToDecimal(totalProductos / productoParams.PageSize));
            var totalPages = Convert.ToInt32(rounded); //Total de páginas a devolver

            var data = _mapper.Map <IReadOnlyList <Producto>, IReadOnlyList <ProductoDto> >(productos);

            //return Ok(_mapper.Map<IReadOnlyList<Producto>, IReadOnlyList<ProductoDto>>(productos));

            return(Ok(new Pagination <ProductoDto>
            {
                Count = totalProductos,
                Data = data,
                PageCount = totalPages,
                PageIndex = productoParams.PageIndex,
                PageSize = productoParams.PageSize
            }));
        }