Example #1
0
        public HttpResponseMessage GetAll()
        {
            try
            {
                List <PedVendaCabecalho> lstPedVendaCabecalho = new List <PedVendaCabecalho>();

                using (MySqlConnection conn = new MySqlConnection(ConnectionString))
                {
                    conn.Open();

                    using (MySqlCommand command = new MySqlCommand())
                    {
                        command.Connection  = conn;
                        command.CommandText = "select idPedCabecalho, idCliente, dtPedido, dtEntrega, desconto, valTotal from pedvenda_cabecalho";

                        MySqlDataReader reader = command.ExecuteReader();

                        while (reader.Read())
                        {
                            PedVendaCabecalho pedVendaCabecalho = new PedVendaCabecalho()
                            {
                                idPedCabecalho = reader["idPedCabecalho"] == DBNull.Value ? 0 : Convert.ToInt32(reader["idPedCabecalho"]),
                                idCliente      = reader["idCliente"] == DBNull.Value ? 0 : Convert.ToInt32(reader["idCliente"]),
                                dtPedido       = reader["dtPedido"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(reader["dtPedido"]),
                                dtEntrega      = reader["dtEntrega"] == DBNull.Value ? DateTime.MinValue : Convert.ToDateTime(reader["dtEntrega"]),
                                desconto       = reader["desconto"] == DBNull.Value ? 0 : Convert.ToDecimal(reader["desconto"]),
                                valTotal       = reader["valTotal"] == DBNull.Value ? 0 : Convert.ToDecimal(reader["valTotal"])
                            };

                            lstPedVendaCabecalho.Add(pedVendaCabecalho);
                        }
                    }

                    conn.Close();
                }

                return(Request.CreateResponse(HttpStatusCode.OK, lstPedVendaCabecalho.ToArray()));
            }
            catch (Exception ex)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message));
            }
        }
Example #2
0
        public HttpResponseMessage Post(PedVendaCabecalho pedVendaCabecalho)
        {
            try
            {
                bool result = false;

                if (pedVendaCabecalho == null)
                {
                    throw new ArgumentNullException("pedVendaCabecalho");
                }

                using (MySqlConnection conn = new MySqlConnection(ConnectionString))
                {
                    conn.Open();

                    using (MySqlCommand command = new MySqlCommand())
                    {
                        command.Connection  = conn;
                        command.CommandText = "insert into pedvenda_cabecalho(idCliente, dtPedido, dtEntrega, desconto, valTotal) values(@idCliente, @dtPedido, @dtEntrega, @desconto, @valTotal)";

                        command.Parameters.AddWithValue("idCliente", pedVendaCabecalho.idCliente);
                        command.Parameters.AddWithValue("dtPedido", pedVendaCabecalho.dtPedido);
                        command.Parameters.AddWithValue("dtEntrega", pedVendaCabecalho.dtEntrega);
                        command.Parameters.AddWithValue("desconto", pedVendaCabecalho.desconto);
                        command.Parameters.AddWithValue("valTotal", pedVendaCabecalho.valTotal);

                        int i = command.ExecuteNonQuery();
                        result = i > 0;
                    }

                    conn.Close();
                }

                return(Request.CreateResponse(HttpStatusCode.OK, result));
            }
            catch (Exception ex)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message));
            }
        }