Example #1
0
        public IActionResult Cadastro(Autor autor)
        {
            var problemas = new List <string>();

            if (string.IsNullOrEmpty(autor.Nome) || autor.Nome.Length < Autor.NomeMinLength || autor.Nome.Length > Autor.NomeMaxLength)
            {
                problemas.Add($"O nome deve ter pelo menos {Autor.NomeMinLength} caracteres e no máximo {Autor.NomeMaxLength}.");
            }

            if (problemas.Count == 0)
            {
                var cadastrou = false;
                if (autor.AutorId > 0)
                {
                    var cadastro = db.Autores.FirstOrDefault(x => x.AutorId == autor.AutorId);
                    if (cadastro != null)
                    {
                        cadastro.Nome = autor.Nome;
                        cadastrou     = db.SaveChanges() > 0;
                    }
                }
                else
                {
                    db.Autores.Add(autor);
                    cadastrou = db.SaveChanges() > 0;
                }
                if (cadastrou)
                {
                    return(RedirectToAction("Index"));
                }
            }

            ViewBag.Problemas = problemas;
            return(View(autor));
        }
        public RetornoBase <int> Inserir(Livro livro)
        {
            var retorno = new RetornoBase <int>();

            try
            {
                var tabLivro = new TabLivro();
                tabLivro.LivroId       = livro.LivroId;
                tabLivro.Titulo        = livro.Titulo;
                tabLivro.Estante       = livro.Estante;
                tabLivro.AnoPublicacao = livro.AnoPublicacao;
                tabLivro.Editora       = db.Editoras.FirstOrDefault(x => x.EditoraId == livro.Editora.EditoraId);
                tabLivro.Autores       = new List <TabAutor>();
                foreach (var autor in livro.Autores)
                {
                    tabLivro.Autores.Add(db.Autores.FirstOrDefault(x => x.AutorId == autor.AutorId));
                }
                db.Livros.Add(tabLivro);
                db.SaveChanges();
                retorno.Valor = tabLivro.LivroId;
            }
            catch (Exception ex)
            {
                retorno.Mensagem = $"Não foi possível inseriro livro '{livro.Titulo}'.";
                retorno.Problemas.Add($"Falha ao {nameof(Inserir)} em {nameof(RepositorioLivro)}: {ex.Message}");
            }

            return(retorno);
        }
        public IActionResult Cadastro(Estado estado)
        {
            ViewBag.problemas = new List <string>();

            if (string.IsNullOrEmpty(estado.UF))
            {
                ViewBag.problemas.Add("Informe a UF!");
            }

            if (string.IsNullOrEmpty(estado.Nome))
            {
                ViewBag.problemas.Add("Informe o nome!");
            }

            if (ViewBag.problemas.Count <= 0)
            {
                var cadastro = db.Estados.FirstOrDefault(x => x.UF == estado.UF);

                if (cadastro == null)
                {
                    db.Estados.Add(estado);
                }
                else
                {
                    cadastro.Nome = estado.Nome;
                }

                if (db.SaveChanges() > 0)
                {
                    return(RedirectToAction("Index"));
                }
            }

            return(View(estado));
        }
Example #4
0
        public IActionResult Cadastro(Contato contato)
        {
            bool resultOperacao = false;

            ViewBag.problemas = new List <string>();

            if (string.IsNullOrEmpty(contato.Nome))
            {
                ViewBag.problemas.Add("Informe o nome!");
            }

            if (string.IsNullOrEmpty(contato.Valor))
            {
                ViewBag.problemas.Add("Informe o valor!");
            }

            if (string.IsNullOrEmpty(contato.UF))
            {
                ViewBag.problemas.Add("Informe a UF!");
            }

            ViewBag.Estados = db.Estados.ToList();
            if (ViewBag.problemas.Count <= 0)
            {
                if (contato.ContatoId > 0)
                {
                    var cadastro = db.Contatos.FirstOrDefault(x => x.ContatoId == contato.ContatoId);
                    if (cadastro != null)
                    {
                        cadastro.Nome  = contato.Nome;
                        cadastro.Valor = contato.Valor;
                        cadastro.UF    = contato.UF;
                        resultOperacao = db.SaveChanges() > 0;
                    }
                }
                else
                {
                    db.Contatos.Add(contato);
                    resultOperacao = db.SaveChanges() > 0;
                }
            }
            if (resultOperacao)
            {
                return(RedirectToAction("Index"));
            }

            return(View(contato));
        }
Example #5
0
        public RetornoBase <bool> Alterar(Editora editora)
        {
            var retorno = new RetornoBase <bool>();

            try
            {
                var tabEditora = db.Editoras.FirstOrDefault(x => x.EditoraId == editora.EditoraId);
                tabEditora.Nome = editora.Nome;
                tabEditora.Site = editora.Site;
                retorno.Valor   = db.SaveChanges() > 0;
            }
            catch (Exception ex)
            {
                retorno.Mensagem = $"Não foi possível alterar a editora '{editora.EditoraId}'.";
                retorno.Problemas.Add($"Falha ao {nameof(Alterar)} em {nameof(RepositorioEditora)}: {ex.Message}");
            }

            return(retorno);
        }
        public RetornoBase <bool> Alterar(Autor autor)
        {
            var retorno = new RetornoBase <bool>();

            try
            {
                var tabAutor = db.Autores.FirstOrDefault(x => x.AutorId == autor.AutorId);
                tabAutor.Nome      = autor.Nome.Nome;
                tabAutor.Sobrenome = autor.Nome.Sobrenome;
                tabAutor.Email     = autor.Email.Endereco;
                retorno.Valor      = db.SaveChanges() > 0;
            }
            catch (Exception ex)
            {
                retorno.Mensagem = $"Não foi possível alterar o autor {autor.AutorId}.";
                retorno.Problemas.Add($"Falha ao {nameof(Alterar)} em {nameof(RepositorioAutor)}: {ex.Message}");
            }

            return(retorno);
        }
Example #7
0
        public AoAlterarEmRepositorioAutor Alterar(Autor autor)
        {
            var retorno = new AoAlterarEmRepositorioAutor();

            try
            {
                var tabAutor = db.Autores.FirstOrDefault(x => x.AutorId == autor.AutorId);
                tabAutor.Nome             = autor.Nome;
                tabAutor.Sobrenome        = autor.Sobrenome;
                tabAutor.Email            = autor.Email;
                retorno.AlterouComSucesso = db.SaveChanges() > 0;
            }
            catch (Exception ex)
            {
                retorno.Mensagem = $"Não foi possível alterar o autor '{autor.AutorId}'.";
                retorno.Problemas.Add($"Falha ao {nameof(Alterar)} em {nameof(RepositorioAutor)}: {ex.Message}");
            }

            return(retorno);
        }
        public IActionResult Cadastro(LivroVM livro)
        {
            if (livro.EditoraId > 0)
            {
                livro.Editora = db.Editoras.FirstOrDefault(x => x.EditoraId == livro.EditoraId);
            }

            if (livro.LivroId > 0)
            {
                livro.Autores = this.GetAutores(livro.LivroId);
            }

            var cadastrou = false;

            if (livro.LivroId > 0)
            {
                var cadastro = db.Livros.Include(x => x.Editora).FirstOrDefault(x => x.LivroId == livro.LivroId);
                if (cadastro != null)
                {
                    cadastro.Titulo  = livro.Titulo;
                    cadastro.Editora = livro.Editora;
                    cadastrou        = db.SaveChanges() > 0;
                }
            }
            else
            {
                var cadastro = new Livro(livro.LivroId, livro.Titulo, livro.Editora);
                db.Livros.Add(cadastro);
                cadastrou = db.SaveChanges() > 0;
            }
            if (cadastrou)
            {
                return(RedirectToAction("Index"));
            }

            ViewBag.Editoras = db.Editoras.ToList();
            return(View(livro));
        }
Example #9
0
        public IActionResult Cadastro(Editora editora)
        {
            var problemas = new List <string>();

            if (string.IsNullOrEmpty(editora.Nome))
            {
                problemas.Add("Informe o nome!");
            }

            if (problemas.Count == 0)
            {
                bool cadastrou = false;
                if (editora.EditoraId > 0)
                {
                    var cadastro = db.Editoras.Include(x => x.Livros).FirstOrDefault(x => x.EditoraId == editora.EditoraId);
                    if (cadastro != null)
                    {
                        editora.Livros = cadastro.Livros;

                        cadastro.Nome = editora.Nome;
                        cadastrou     = db.SaveChanges() > 0;
                    }
                }
                else
                {
                    db.Editoras.Add(editora);
                    cadastrou = db.SaveChanges() > 0;
                }
                if (cadastrou)
                {
                    return(RedirectToAction("Index"));
                }
            }

            ViewBag.Problemas = problemas;
            return(View(editora));
        }