internal List <Mercado> Retrieve()
        {
            MySqlConnection con     = Connect();
            MySqlCommand    command = con.CreateCommand();

            command.CommandText = "select * from mercados";

            try
            {
                con.Open();
                MySqlDataReader res = command.ExecuteReader();

                Mercado        mer      = null;
                List <Mercado> mercados = new List <Mercado>();
                while (res.Read())
                {
                    Debug.WriteLine("Recuperado: " + res.GetInt32(0) + " " + res.GetInt32(1) + " " + res.GetDouble(2) + " " + res.GetDouble(3) + " " + res.GetString(6) + " " + res.GetDouble(4) + " " + res.GetDouble(5));
                    mer = new Mercado(res.GetInt32(0), res.GetInt32(1), res.GetDouble(2), res.GetDouble(3), res.GetString(6), res.GetDouble(4), res.GetDouble(5));
                    mercados.Add(mer);
                }

                con.Close();
                return(mercados);
            }
            catch (MySqlException)
            {
                Debug.WriteLine("Se ha producido un error de conexión.");
                return(null);
            }
        }
        //Inserció inicial de dades
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            CultureInfo culInfo = new CultureInfo("es-ES");

            culInfo.NumberFormat.NumberDecimalSeparator          = ".";
            culInfo.NumberFormat.CurrencyDecimalSeparator        = ".";
            culInfo.NumberFormat.PercentDecimalSeparator         = ".";
            culInfo.NumberFormat.CurrencyDecimalSeparator        = ".";
            System.Threading.Thread.CurrentThread.CurrentCulture = culInfo;



            Usuario usuario = new Usuario(1, "User1", "Apellidos", "*****@*****.**", "password", 20, 150);

            Cuenta cuenta = new Cuenta(1, "BBVA", "1234", usuario.UsuarioId);

            Evento evento1 = new Evento(1, "Valencia", "Madrid", DateTime.Today);
            Evento evento2 = new Evento(2, "Barcelona", "Madrid", DateTime.Today);

            Mercado mercado1 = new Mercado(1, 1.80, 1.57, "1.5", 100, 100, evento1.EventoId);
            Mercado mercado2 = new Mercado(2, 1.70, 1.35, "3.5", 150, 180, evento2.EventoId);

            Apuesta apuesta = new Apuesta(1, 1.8, 50, "over 1.5", usuario.UsuarioId, mercado1.MercadoId);

            modelBuilder.Entity <Apuesta>().HasData(apuesta);
            modelBuilder.Entity <Cuenta>().HasData(cuenta);
            modelBuilder.Entity <Evento>().HasData(evento1);
            modelBuilder.Entity <Evento>().HasData(evento2);
            modelBuilder.Entity <Mercado>().HasData(mercado1);
            modelBuilder.Entity <Mercado>().HasData(mercado2);
            modelBuilder.Entity <Usuario>().HasData(usuario);
        }
        internal Mercado BuscarMercadoPorID(int id)
        {
            MySqlConnection con     = Connect();
            MySqlCommand    command = con.CreateCommand();

            command.CommandText = "select * from mercados where id=@id";
            command.Parameters.AddWithValue("@id", id);

            try
            {
                con.Open();
                MySqlDataReader res = command.ExecuteReader();

                Mercado mer = null;
                while (res.Read())
                {
                    Debug.WriteLine("Recuperado: " + res.GetInt32(0) + " " + res.GetInt32(1) + " " + res.GetDouble(2) + " " + res.GetDouble(3) + " " + res.GetString(6) + " " + res.GetDouble(4) + " " + res.GetDouble(5));
                    mer = new Mercado(res.GetInt32(0), res.GetInt32(1), res.GetDouble(2), res.GetDouble(3), res.GetString(6), res.GetDouble(4), res.GetDouble(5));
                }

                con.Close();
                return(mer);
            }
            catch (MySqlException)
            {
                Debug.WriteLine("Se ha producido un error de conexión.");
                return(null);
            }
        }
        internal void Save(Mercado m)
        {
            PlaceMyBetContext context = new PlaceMyBetContext();

            context.Mercados.Add(m);
            context.SaveChanges();
        }
        internal void UpdateMercadoExistente(int id, Apuesta apuesta)
        {
            MySqlConnection con     = Connect();
            MySqlCommand    command = con.CreateCommand();
            Mercado         mercado = QueMercadoEsLaApuesta(apuesta);

            mercado = RecalculoCuotas(mercado, apuesta);

            command.CommandText = "UPDATE mercados SET id=@id, id_evento=@id_evento, cuota_over=@cuota_over, cuota_under=@cuota_under, dinero_over=@dinero_over," +
                                  "dinero_under=@dinero_under, tipo_mercado=@tipo_mercado WHERE id=@id";

            command.Parameters.AddWithValue("@id", mercado.MercadoId);
            command.Parameters.AddWithValue("@id_evento", mercado.EventoId);
            command.Parameters.AddWithValue("@cuota_over", mercado.CuotaOver);
            command.Parameters.AddWithValue("@cuota_under", mercado.CuotaUnder);
            command.Parameters.AddWithValue("@dinero_over", mercado.DineroOver);
            command.Parameters.AddWithValue("@dinero_under", mercado.DineroUnder);
            command.Parameters.AddWithValue("@tipo_mercado", mercado.TipoMercado.ToString());

            try
            {
                con.Open();
                MySqlDataReader res = command.ExecuteReader();
                con.Close();
            }
            catch (MySqlException)
            {
                Debug.WriteLine("Se ha producido un error de conexión.");
            }
        }
        internal void UpdateMercadoExistente(int id, Apuesta apuesta)
        {
            Mercado mercado = QueMercadoEsLaApuesta(apuesta);

            mercado = RecalculoCuotas(mercado, apuesta);

            PlaceMyBetContext context = new PlaceMyBetContext();

            context.Mercados.Update(mercado);
            context.SaveChanges();
        }
        /*internal void SaveMercadoNuevo(Mercado mercado)
         * {
         *  MySqlConnection con = Connect();
         *  MySqlCommand command = con.CreateCommand();
         *  command.CommandText = "insert";
         * }*/

        internal List <Mercado> MercadosQuery(int id, double tM, double cO, double cU)
        {
            MySqlConnection con     = Connect();
            MySqlCommand    command = con.CreateCommand();

            CultureInfo culInfo = new System.Globalization.CultureInfo("es-ES");

            culInfo.NumberFormat.NumberDecimalSeparator          = ".";
            culInfo.NumberFormat.CurrencyDecimalSeparator        = ".";
            culInfo.NumberFormat.PercentDecimalSeparator         = ".";
            culInfo.NumberFormat.CurrencyDecimalSeparator        = ".";
            System.Threading.Thread.CurrentThread.CurrentCulture = culInfo;

            command.CommandText = "select * from mercados where id_evento=@id and tipo_mercado=@tM and cuota_over=@cO and cuota_under=@cU";
            command.Parameters.AddWithValue("@id", id);
            command.Parameters.AddWithValue("@tM", tM);
            command.Parameters.AddWithValue("@cO", cO);
            command.Parameters.AddWithValue("@cU", cU);

            List <Mercado> mercados = new List <Mercado>();

            try
            {
                con.Open();
                MySqlDataReader res = command.ExecuteReader();

                Mercado mer = null;
                while (res.Read())
                {
                    Debug.WriteLine("Recuperado: " + res.GetInt32(0) + " " + res.GetInt32(1) + " " + res.GetDouble(2) + " " + res.GetDouble(3) + " " + res.GetString(6) + " " + res.GetDouble(4) + " " + res.GetDouble(5));
                    mer = new Mercado(res.GetInt32(0), res.GetInt32(1), res.GetDouble(2), res.GetDouble(3), res.GetString(6), res.GetDouble(4), res.GetDouble(5));
                }

                mercados.Add(mer);
                con.Close();
            }
            catch (MySqlException)
            {
                Debug.WriteLine("Se ha producido un error de conexión.");
                return(null);
            }
            return(mercados);
        }
        internal Mercado RecalculoCuotas(Mercado mercado, Apuesta apuesta)
        {
            double probabilidadOver  = 0.0;
            double probabilidadUnder = 0.0;
            string tipoApuesta       = apuesta.Tipo.ToLower();

            if (tipoApuesta.Contains("over"))
            {
                mercado.DineroOver += apuesta.Cantidad;
            }
            else
            {
                mercado.DineroUnder += apuesta.Cantidad;
            }
            probabilidadOver  = mercado.DineroOver / (mercado.DineroOver + mercado.DineroUnder);
            probabilidadUnder = mercado.DineroUnder / (mercado.DineroOver + mercado.DineroUnder);

            mercado.CuotaOver  = Math.Round(1 / probabilidadOver * 0.95, 2);
            mercado.CuotaUnder = Math.Round(1 / probabilidadUnder * 0.95, 2);

            return(mercado);
        }
 public ApuestaDTO(int usuarioId, int eventoId, double cuota, double cantidad, string tipo, Mercado mercado)
 {
     Cuota     = cuota;
     Cantidad  = cantidad;
     EventoId  = eventoId;
     UsuarioId = usuarioId;
     Tipo      = tipo;
 }
 public MercadoDTO ToDTO(Mercado mercado)
 {
     return(new MercadoDTO(mercado.CuotaUnder, mercado.CuotaOver, mercado.TipoMercado));
 }