Exemplo n.º 1
0
        public IActionResult Post([FromBody] ProductBasicInfoPostDTO newProduct)
        {
            // Se añade un registro y se obtiene el id con el que se registró.
            int id = new ProductSC().AddNewProduct(newProduct);

            return(Created("GET " + Request.Path.Value + "/" + id, new { Id = id }));
        }
Exemplo n.º 2
0
        public IActionResult Put(int id, [FromBody] ProductBasicInfoPutDTO modifiedProduct)
        {
            Product dataBaseProduct = new ProductSC().GetProductById(id);

            if (dataBaseProduct == null)
            {
                return(NotFound());
            }

            try
            {
                //TODO: Check if it is possible to pass the dataBaseProduct insted of the id.
                new ProductSC().UpdateProduct(id, modifiedProduct);
            }
            catch (Exception ex) when(ExceptionTypes.IsSqlException(ex))
            {
                string message = SqlExceptionMessages.GetCustomSqlExceptionMessage(ex.InnerException as SqlException);

                if (message != null)
                {
                    return(Conflict(message));
                }

                throw;
            }

            return(NoContent());
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            /*
             * NorthwindContext dbContext = new NorthwindContext();
             *
             * // SELECT * FROM Products
             * var products = dbContext.Products.Select(p => p).ToList();
             *
             * foreach(var product in products)
             * {
             *  // Imprimir en consola el Id y el nombre del producto
             *  Console.WriteLine(product.ProductId + " - "+ product.ProductName);
             * }
             */

            var products = new ProductSC().GetAllProducts().ToList();

            foreach (var product in products)
            {
                // Imprimir en consola el Id y el nombre del producto
                Console.WriteLine(product.ProductId + " - " + product.ProductName);
            }

            Console.ReadLine();
        }
Exemplo n.º 4
0
        public IActionResult Delete(int id)
        {
            Product dataBaseProduct = new ProductSC().GetProductById(id);

            if (dataBaseProduct == null)
            {
                return(NotFound());
            }

            try
            {
                new ProductSC().DeleteProduct(dataBaseProduct);
            }
            catch (Exception ex) when(ExceptionTypes.IsSqlException(ex))
            {
                string message = SqlExceptionMessages.GetCustomSqlExceptionMessage(ex.InnerException as SqlException);

                if (message != null)
                {
                    return(Conflict(message));
                }

                throw;
            }

            return(NoContent());
        }
Exemplo n.º 5
0
        public IActionResult GetPage(int requestedPage)
        {
            const int elementsPerPage = 10;

            if (requestedPage < 1)
            {
                return(BadRequest($"{nameof(requestedPage)} must be at least 1."));
            }

            // Calculate Pages
            int lastPage = new ProductSC().CalculateLastPage(elementsPerPage);
            Pagination <ProductBasicInfoDTO> response = new(requestedPage, lastPage);

            if (lastPage == 0)
            {
                return(Ok(response));
            }

            // Get Selected Page
            IQueryable <Product>       dbProducts = new ProductSC().GetPage(elementsPerPage, (int)response.CurrentPage);
            List <ProductBasicInfoDTO> products   = BaseSC.MaterializeIQueryable <Product, ProductBasicInfoDTO>(dbProducts);

            // Attach elements of the page to the response
            response.ResponseList = products;

            return(Ok(response));
        }
Exemplo n.º 6
0
        static void Main(string[] args)
        {
            var ProductsQry = new ProductSC().GetProducts();
            var output      = ProductsQry.ToList();

            output.ForEach(f => Console.WriteLine(f.ProductName));
        }
Exemplo n.º 7
0
        public IActionResult GetPage([PositiveInteger] int requestedPage)
        {
            const int elementsPerPage = 10;

            // Cálculo de las Páginas.
            int lastPage = new ProductSC().CalculateLastPage(elementsPerPage);
            Pagination <ProductBasicInfoDTO> response = new(requestedPage, lastPage);

            if (lastPage == 0)
            {
                return(Ok(response));
            }

            // Selección de la página solicitada.
            IQueryable <Product> dbProducts = new ProductSC()
                                              .GetPage(elementsPerPage, (int)response.CurrentPage);

            // Materialización de los registros.
            List <ProductBasicInfoDTO> products = BaseSC
                                                  .MaterializeIQueryable <Product, ProductBasicInfoDTO>(dbProducts);

            // Los registros se añaden a la respuesta.
            response.ResponseList = products;

            return(Ok(response));
        }
        public List <ProductDTO> addProduct([FromBody] ProductDTO newproduct)
        {
            var products = new ProductSC().getAllProducts();

            products.Add(newproduct);

            return(new ProductSC().getAllProducts());
        }
Exemplo n.º 9
0
        public IActionResult GetAll()
        {
            IQueryable <Product> dbProducts = new ProductSC().GetAllProducts();

            List <ProductBasicInfoDTO> products = BaseSC.MaterializeIQueryable <Product, ProductBasicInfoDTO>(dbProducts);

            return(Ok(products));
        }
Exemplo n.º 10
0
        public IActionResult Get([PositiveInteger] int id)
        {
            // Obtiene el empleado de la Base de Datos.
            Product dbProduct = new ProductSC().GetProductById(id);
            // Genera un modelo con la información del empleado obtenido.
            ProductBasicInfoDTO product = new(dbProduct);

            return(Ok(product));
        }
Exemplo n.º 11
0
        public IActionResult GetAll()
        {
            // Se obtienen todos los registros.
            IQueryable <Product> dbProducts = new ProductSC().GetAllProducts();

            // Se materialzian.
            List <ProductBasicInfoDTO> products = BaseSC
                                                  .MaterializeIQueryable <Product, ProductBasicInfoDTO>(dbProducts);

            return(Ok(products));
        }
Exemplo n.º 12
0
        public List <ProductModel> Get()
        {
            var products = new ProductSC().GetAllProducts().Select(s => new ProductModel
            {
                idNumber = s.ProductId,
                Name     = s.ProductName,
                Price    = (decimal)s.UnitPrice,
                Stock    = (short)s.UnitsInStock
            }).ToList();

            return(products);
        }
Exemplo n.º 13
0
        public List <ProductModel> Get()
        {
            var products = new ProductSC().GetAllProducts().Select(s => new ProductModel
            {
                // pasa algunos datos de la clase products a un productModel para no mostrar los nombres de los atributos
                idNumber = s.ProductId,
                Name     = s.ProductName,
                Price    = (decimal)s.UnitPrice,
                Stock    = (short)s.UnitsInStock
            }).ToList();

            return(products);
        }
Exemplo n.º 14
0
        public IActionResult Delete(int id)
        {
            try
            {
                using (NorthwindContext dbContext = new())
                {
                    ProductSC.DeleteProduct(dbContext, id);
                }

                return(Ok());
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }
Exemplo n.º 15
0
        public IActionResult Put(int id, [FromBody] ProductBasicInfoDTO modifiedProduct)
        {
            try
            {
                using (NorthwindContext dbContext = new())
                {
                    ProductSC.UpdateProduct(dbContext, id, modifiedProduct);
                }

                return(Ok());
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }
Exemplo n.º 16
0
        public IActionResult Get()
        {
            List <ProductBasicInfoDTO> products = new();

            using (NorthwindContext dbContext = new())
            {
                IQueryable <Product> dbProducts = ProductSC.GetAllProducts(dbContext).AsNoTracking();

                foreach (Product dbProduct in dbProducts)
                {
                    products.Add(new ProductBasicInfoDTO(dbProduct));
                }
                ;
            }

            return(Ok(products));
        }
Exemplo n.º 17
0
        public IActionResult Post([FromBody] ProductBasicInfoDTO newProduct)
        {
            int id;

            try
            {
                using (NorthwindContext dbContext = new())
                {
                    id = ProductSC.AddNewProduct(dbContext, newProduct);
                }

                return(Ok("Registered Id: " + id));
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }
Exemplo n.º 18
0
        public IActionResult Get(int id)
        {
            if (id < 1)
            {
                return(BadRequest($"{nameof(id)} must be at least 1."));
            }

            // Get Product from Database
            Product dbProduct = new ProductSC().GetProductById(id);

            if (dbProduct == null)
            {
                return(NotFound());
            }

            ProductBasicInfoDTO product = new(dbProduct);

            return(Ok(product));
        }
Exemplo n.º 19
0
        public IActionResult Get(int id)
        {
            try
            {
                ProductBasicInfoDTO product;

                using (NorthwindContext dbContext = new())
                {
                    Product dbProduct = ProductSC.GetProductById(dbContext, id);

                    product = new(dbProduct);
                }

                return(Ok(product));
            }
            catch (Exception ex)
            {
                return(InternalServerError(ex));
            }
        }
Exemplo n.º 20
0
        public IActionResult Post([FromBody] ProductBasicInfoPostDTO newProduct)
        {
            int id;

            try
            {
                id = new ProductSC().AddNewProduct(newProduct);
            }
            catch (Exception ex) when(ExceptionTypes.IsSqlException(ex))
            {
                string message = SqlExceptionMessages.GetCustomSqlExceptionMessage(ex.InnerException as SqlException);

                if (message != null)
                {
                    return(Conflict(message));
                }

                throw;
            }

            return(Created("GET " + Request.Path.Value + "/" + id, id));
        }
Exemplo n.º 21
0
        public ProductModel Get(int id)
        {
            var product      = new ProductSC().GetProductById(id);
            var productModel = new ProductModel();

            try
            {
                productModel.idNumber = product.ProductId;
                productModel.Name     = product.ProductName;
                productModel.Price    = (decimal)product.UnitPrice;
                productModel.Stock    = (short)product.UnitsInStock;
            }
            catch
            {
                productModel.idNumber = null;
                productModel.Name     = "";
                productModel.Price    = null;
                productModel.Stock    = null;
            }

            return(productModel);
        }
Exemplo n.º 22
0
        public ProductModel Get(int id)
        {
            var product      = new ProductSC().GetProductById(id);
            var productModel = new ProductModel();

            try // Como el metodo GetProductById devuelve un null si no lo encuentra se puso en un try catch
            {   // ya que el ProductModel no acepta datos nulos
                productModel.idNumber = product.ProductId;
                productModel.Name     = product.ProductName;
                productModel.Price    = (decimal)product.UnitPrice;
                productModel.Stock    = (short)product.UnitsInStock;
            }
            catch
            {
                productModel.idNumber = null;
                productModel.Name     = "";
                productModel.Price    = null;
                productModel.Stock    = null;
            }

            return(productModel);
        }
Exemplo n.º 23
0
        public IEnumerable <Product> Get()
        {
            var products = new ProductSC().GetProducts().ToList();

            return(products);
        }