コード例 #1
0
        public JsonResult Cadastrar(EcoSense model)
        {
            try
            {
                if (model == null)
                {
                    throw new Exception("Preencha o serial");
                }

                if (model.SerialAparelho == null)
                {
                    throw new Exception("Preencha o serial");
                }

                using (var service = new Service <EcoSense>())
                    using (var serviceSerial = new Service <SerialAparelho>())
                    {
                        var userId = Convert.ToInt32(Session["UserId"]);
                        model.UsuarioId = userId;
                        model.Ativo     = false;

                        var serial = serviceSerial.GetRepository().Where(s => s.Serial.Equals(model.SerialAparelho.Serial)).FirstOrDefault();

                        if (serial == null)
                        {
                            throw new Exception("Serial não existe/incorreto");
                        }

                        var list = service.GetRepository().ToList().Where(es => es.SerialAparelho.Serial.Equals(serial.Serial));

                        if (list.ToList().Count() != 0)
                        {
                            throw new Exception("Este serial ja foi cadastrado por outra pessoa.");
                        }

                        if (model.Id == 0)
                        {
                            service.Save(model);
                        }


                        TempData["Sucesso"]  = true;
                        TempData["Mensagem"] = "Serial " + model.SerialAparelho.Serial + " cadastrado com sucesso";
                    }
            }
            catch (Exception exception)
            {
                TempData["Erro"]     = true;
                TempData["Mensagem"] = exception.Message;
            }

            var view = RedirectToAction("Index");

            view.ExecuteResult(ControllerContext);

            return(null);
        }
コード例 #2
0
ファイル: AdminController.cs プロジェクト: YuuSoft/ecocharge
        public ActionResult MedidoresSave(EcoSense ecosense)
        {
            try
            {
                using (var service = new Service <EcoSense>())
                {
                    service.Save(ecosense);

                    TempData["Sucesso"]  = true;
                    TempData["Mensagem"] = "Cadastrado com sucesso.";
                }
            }
            catch (Exception ex)
            {
                TempData["Erro"]     = true;
                TempData["Mensagem"] = "Não foi possível cadastrar. Erro: " + ex.Message;
            }

            var view = RedirectToAction("Aparelhos");

            view.ExecuteResult(ControllerContext);

            return(null);
        }