Example #1
0
        public ProductPaginated GetProductsByCompaniesId(List <int> companiesIds, int page, int limit)
        {
            if (companiesIds.Count < 1)
            {
                throw new BadRequestException("Algo deu errado, tente recarregar a página ou entre em contato com o suporte");
            }

            List <Product> products = new List <Product>();

            try
            {
                products = _repository.getProductsByCompaniesId(companiesIds, page, limit);
            }
            catch (Exception e)
            {
                throw new Exception($"Algo deu errado: {e.GetType()}");
            }

            if (products.Count < 1)
            {
                throw new NotFoundException("Nenhum produto cadastrado");
            }

            ProductPaginated productPaginated = new ProductPaginated();

            productPaginated.products = products;
            productPaginated.total    = _repository.countProductsByCompaniesId(companiesIds);

            return(productPaginated);
        }
        public ProductPaginated GetProductsPaginated(int pageIndex, int pageSize, string name, int categoryId, string orderByNameOrPrice, string ascOrDesc)
        {
            ProductPaginated prodPaginated = new ProductPaginated();

            using (var connection = new SqlConnection(connectionString))
            {
                var queryString = "exec [Products_GetProductsPaginated] @PageIndex, @PageSize, @Name, @CategoryId, @OrderByNameOrPrice, @ASCorDESC";
                var values      = new
                {
                    PageIndex          = pageIndex,
                    PageSize           = pageSize,
                    Name               = name,
                    CategoryId         = categoryId,
                    OrderByNameOrPrice = orderByNameOrPrice,
                    ASCorDESC          = ascOrDesc
                };
                connection.Open();
                var reader = connection.QueryMultiple(queryString, values);
                prodPaginated.products = reader.Read <Product>().AsList();
                prodPaginated.TotalRow = reader.Read <int>().FirstOrDefault();
            }

            return(prodPaginated);
        }