Esempio n. 1
0
        public ActionResult Disponibilidade(DisponibilidadeViewModel dvm)
        {
            DisponibilidadePostViewModel dpvm = GetDVMFromRequest(Request);

            List <AlertaDeDisponibilidade> alertasExistentes = db.Alertas.Include(a => a.Email).ToList();

            foreach (var alertaExistente in alertasExistentes)
            {
                if (!dpvm.alertas.Any(a => a.Key == alertaExistente.Id))
                {
                    foreach (Insumo insumo in db.Insumos.Where(ins => ins.Alerta.Id == alertaExistente.Id))
                    {
                        insumo.Alerta = null;
                    }
                    db.Alertas.Remove(alertaExistente);
                }
            }

            foreach (var alertaNovo in dpvm.alertas)
            {
                if (alertasExistentes.Any(a => a.Id == alertaNovo.Key))
                {
                    AlertaDeDisponibilidade alertaExistente = db.Alertas.Find(alertaNovo.Key);
                    alertaExistente.Ativado          = alertaNovo.Value.Ativado;
                    alertaExistente.Email            = alertaNovo.Value.Email;
                    alertaExistente.QuantidadeMinima = alertaNovo.Value.QuantidadeMinima;
                }
                else
                {
                    AlertaDeDisponibilidade alertaASerInserido = new AlertaDeDisponibilidade()
                    {
                        Ativado          = alertaNovo.Value.Ativado,
                        Email            = alertaNovo.Value.Email,
                        Id               = alertaNovo.Key,
                        QuantidadeMinima = alertaNovo.Value.QuantidadeMinima
                    };

                    db.Alertas.Add(alertaASerInserido);
                }
            }

            db.SaveChanges();

            return(Disponibilidade());
        }
Esempio n. 2
0
        private DisponibilidadePostViewModel GetDVMFromRequest(HttpRequestBase request)
        {
            DisponibilidadePostViewModel dpvm = new DisponibilidadePostViewModel();

            dpvm.alertas = new Dictionary <int, AlertaDeDisponibilidade>();

            foreach (var item in request.Form.AllKeys)
            {
                string type  = item.Split(new Char[] { '-' })[0];
                int    id    = Convert.ToInt32(item.Split(new Char[] { '-' })[1]);
                string value = request.Form[item];

                if (value == null || value == "")
                {
                    continue;
                }

                if (!dpvm.alertas.ContainsKey(id))
                {
                    dpvm.alertas.Add(id, new AlertaDeDisponibilidade());
                }

                if (type == "email")
                {
                    dpvm.alertas[id].Email = new Email(value);
                }
                else if (type == "qtd")
                {
                    double val = 0;
                    double.TryParse(value, System.Globalization.NumberStyles.Float, new System.Globalization.CultureInfo("pt-br"), out val);
                    dpvm.alertas[id].QuantidadeMinima = val;
                }
                else if (type == "ativo")
                {
                    var ativo = request.Form[item];
                    dpvm.alertas[id].Ativado = request.Form[item] == "on";
                    ativo = "";
                }
            }
            return(dpvm);
        }