public ActionResult Get(int id, int idProducto) { string accept = Request.Headers["Accept"].ToString(); if (accept != "application/json" && accept != "*/*") { return(StatusCode(406)); //Unsupported Media Type } LineaPedidoConDetallesProducto result = ManejadoraLineasPedidos.obtenerLineaPedidoDePedidoPorIdProducto(id, idProducto); if (result != null) { return(Ok(result)); //200 } else { return(NotFound(idProducto)); //404 } }
/// <summary> /// Metodo que retorna la linea de pedido de un producto, buscandola por las id del producto y del pedido /// </summary> /// <param name="idPedido"></param> /// <param name="idProducto"></param> /// <returns>LineaPedidoConDetallesProducto o null en caso de no haber encontrado la líneaPedido con esos datos</returns> public static LineaPedidoConDetallesProducto obtenerLineaPedidoDePedidoPorIdProducto(int idPedido, int idProducto) { LineaPedidoConDetallesProducto lineaPedido = null; Connection conexion = new Connection(); Connection conexion2 = new Connection(); Connection conexion3 = new Connection(); SqlConnection sqlConnection = new SqlConnection(); SqlConnection sqlConnection2 = new SqlConnection(); SqlConnection sqlConnection3 = new SqlConnection(); SqlCommand command = new SqlCommand(); SqlCommand commandProd = new SqlCommand(); SqlCommand commandCat = new SqlCommand(); SqlDataReader lector = null; SqlDataReader lectorProd = null; SqlDataReader lectorCat = null; //Variables lineaPedido double precioUnitario, impuestos, subtotal; int idLinPed, cantidad; Producto producto = null; //Variables Producto int idProd, stock; String nombreProd, descripcion; double precioVenta; //Variables Categorias String nombreCat; List <Categoria> listaCat; try { //Conexión para obtener la líneaPedido del pedido sqlConnection = conexion.getConnection(); command.CommandText = "SELECT * FROM LineasDePedido AS L " + "INNER JOIN Productos AS P ON L.Id_Producto = P.Id " + "WHERE L.Id_Producto = @idProducto AND Id_Pedido = @idPedido"; command.Parameters.Add("@idProducto", SqlDbType.Int).Value = idProducto; command.Parameters.Add("@idPedido", SqlDbType.Int).Value = idPedido; command.Connection = sqlConnection; lector = command.ExecuteReader(); //Comprobar si el lector tiene filas y en caso afirmativo, recorrer if (lector.HasRows) { lector.Read(); //Definir los atributos de lineas pedidos idLinPed = (int)lector["Id_Pedido"]; idProd = (int)lector["Id_Producto"]; precioUnitario = (double)(decimal)lector["Precio_Unitario"]; cantidad = (int)lector["Cantidad"]; impuestos = (double)(decimal)lector["Impuestos"]; subtotal = (double)(decimal)lector["Subtotal"]; //Definicion de los parametros del comando commandProd.CommandText = "SELECT * FROM Productos WHERE Id = @idProducto"; commandProd.Parameters.Add("@idProducto", System.Data.SqlDbType.Int).Value = idProducto; sqlConnection2 = conexion2.getConnection(); commandProd.Connection = sqlConnection2; lectorProd = commandProd.ExecuteReader(); if (lectorProd.HasRows) { lectorProd.Read(); //Definir los atributos de productos idProd = (int)lectorProd["Id"]; nombreProd = (String)lectorProd["Nombre"]; descripcion = (String)lectorProd["Descripcion"]; precioVenta = (double)(decimal)lectorProd["Precio_venta"]; stock = (int)lectorProd["Stock"]; //Definir conexión para las categorías commandCat.CommandText = "SELECT Nombre_Categoria FROM Productos_Categorias WHERE Id_Producto = @idProducto"; commandCat.Parameters.Add("@idProducto", SqlDbType.Int).Value = idProducto; sqlConnection3 = conexion3.getConnection(); commandCat.Connection = sqlConnection3; lectorCat = commandCat.ExecuteReader(); listaCat = new List <Categoria>(); //Comprobar si el lector tiene filas y en caso afirmativo, recorrer if (lectorCat.HasRows) { while (lectorCat.Read()) { nombreCat = (String)lectorCat["Nombre_Categoria"]; listaCat.Add(new Categoria(nombreCat)); } } //Creamos el nuevo producto producto = new Producto(idProd, nombreProd, precioVenta, descripcion, stock, listaCat); } //Creamos la nueva linea pedido lineaPedido = new LineaPedidoConDetallesProducto(idLinPed, idProd, precioUnitario, cantidad, impuestos, subtotal, producto); } } catch (SqlException ex) { throw ex; } finally { //Cerramos el lector y la conexion conexion.closeConnection(ref sqlConnection); conexion2.closeConnection(ref sqlConnection2); conexion3.closeConnection(ref sqlConnection3); if (lector != null) { lector.Close(); } if (lectorProd != null) { lectorProd.Close(); } if (lectorCat != null) { lectorCat.Close(); } } return(lineaPedido); }