problem with foreach in mvc 4 asp .net c #

0

my problem is based on a table RESTRICTIONS, which is based on a turn-taking system, these restrictions can be created by the admin from its backoffice, so to go through this table I use a foreach, and for example I rely on The common restrictions that can be generated: Mandatory Sunday - mandatory opening -Up to 4 turns per user.

my system will only design it with these 3 restrictions but the ideal is that my code covers all the possibilities. then I have the following in the controller.

                try
                {
                int contador = 0;
                bool apertura = false;
                bool domingo = false;
                bool check1 = false;
                foreach (string valor in Horario)
                {

                string[] arreglo = valor.Split(new char[] { '_' }, System.StringSplitOptions.RemoveEmptyEntries);
                Dia tDia = new Dia();
                UsuarioTurnos usuarioTurnos = new UsuarioTurnos();
                string DiaCortado = arreglo[0];
                string TurnoCortado = arreglo[1];
                int idDia = Convert.ToInt32(DiaCortado);
                int idTurno = Convert.ToInt32(TurnoCortado);
                var restricciones = db.Restriccion.Where(r => r.Dia_idDia == idDia && r.Turnos_idTurnos == idTurno).ToList();
                foreach (var restriccion in restricciones)
                {
                    var cantidad = restriccion.cantidad;
                    var minimoMaximo = restriccion.tipo;
                    var dia = restriccion.Dia_idDia;
                    var turno = restriccion.Turnos_idTurnos;


                    //restriccion para un turno domingo obligatorio
                    if (contador <= 4)
                    {
                        if (dia == 7)
                        {
                            if (cantidad == 1)
                            {
                                if (minimoMaximo == "min")
                                {
                                    if (contador <= 4)
                                    {
                                        usuarioTurnos.Usuario_RutUsuario = (string)Session["rut"];
                                        usuarioTurnos.Turnos_idTurnos = Convert.ToInt32(turno);
                                        usuarioTurnos.Dia_idDia = Convert.ToInt32(dia);
                                        db.UsuarioTurnos.Add(usuarioTurnos);
                                        db.SaveChanges();
                                        contador++;
                                        domingo = true;
                                    }
                                }

                            }
                        }


                        //restriccion para 1 turnos apertura obligatorios por semana
                        if (turno == 1)
                        {
                            if (dia != 7)
                            {
                                if (cantidad == 2)
                                {
                                    if (minimoMaximo == "min")
                                    {
                                        if (contador <= 4)
                                        {
                                            usuarioTurnos.Usuario_RutUsuario = (string)Session["rut"];
                                            usuarioTurnos.Turnos_idTurnos = Convert.ToInt32(turno);
                                            usuarioTurnos.Dia_idDia = Convert.ToInt32(dia);

                                            db.UsuarioTurnos.Add(usuarioTurnos);
                                            db.SaveChanges();
                                            contador++;
                                            apertura = true;
                                        }
                                    }
                                }
                            }
                        }
                        //en esta restriccion solo se guardaran los turnos que no sean ni domingo ni apertura
                        if (cantidad == 4)
                        {
                            if (minimoMaximo == "max")
                            {
                                if (apertura == true && domingo == true)
                                {
                                    usuarioTurnos.Usuario_RutUsuario = (string)Session["rut"];
                                    usuarioTurnos.Turnos_idTurnos = Convert.ToInt32(turno);
                                    usuarioTurnos.Dia_idDia = Convert.ToInt32(dia);
                                    db.UsuarioTurnos.Add(usuarioTurnos);
                                    db.SaveChanges();
                                    contador++;
                                }

                            }
                        }

                        //restriccion para maximo 4 turnos por semana

                    }
                    else
                    {
                        return RedirectToAction("MiPerfil", "Usuarios");
                    }
                }

good and in the bd I have 4 columns, id_dia, id_turno, quantity, type (min, max)

If you could help me please, thank you very much in advance, by the way I'm new to this forum.

Greetings from Chile

    
asked by cesar calbucan 23.10.2018 в 04:11
source

0 answers