public string Save(Isencao i)
        {
            ArgumentsValidator.RaiseExceptionOfInvalidArguments(
                RaiseException.IfNullOrEmpty(i.Descricao, "Descrição não informada"),
                RaiseException.IfTrue(i.DtAta == DateTime.MinValue, "Data não informada"),
                RaiseException.IfNull(i.DtAta, "Data não informada - Nula"),
                RaiseException.IfTrue(i.AnoEvento == 0, "Ano da Isencão não informada")
                );

            if (i.TipoIsencao.Equals("1"))
            {
                ArgumentsValidator.RaiseExceptionOfInvalidArguments(
                    RaiseException.IfTrue(i.EventoId == 0, "Evento não informado"),
                    RaiseException.IfNull(i.EventoId, "Evento não informado")
                    );
            }

            if (i.TipoIsencao.Equals("2"))
            {
                ArgumentsValidator.RaiseExceptionOfInvalidArguments(
                    RaiseException.IfTrue(i.AnuidadeId == 0, "Anuidade não informada"),
                    RaiseException.IfNull(i.AnuidadeId, "Anuidade não informada")
                    );
            }


            Isencao _i = new Isencao {
                IsencaoId   = i.IsencaoId,
                AnuidadeId  = i.AnuidadeId,
                EventoId    = i.EventoId,
                Descricao   = i.Descricao,
                DtAta       = i.DtAta,
                AnoEvento   = i.AnoEvento,
                TipoIsencao = i.TipoIsencao,
                Ativo       = i.Ativo
            };

            try
            {
                if (_i.IsencaoId == 0)
                {
                    return(_isencaoService.Insert(_i));
                }
                else
                {
                    return(_isencaoService.Update(i.IsencaoId, _i));
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public Isencao GetIsencaoById(int id)
        {
            query = @"SELECT I.IsencaoId, I.AnuidadeId, I.EventoId, 
                        I.Descricao, I.DtAta, I.AnoEvento, I.TipoIsencao, I.Ativo 
                    FROM dbo.AD_Isencao I 
                    WHERE I.IsencaoId = " + id + " ";

            // Define o banco de dados que será usando:
            CommandSql cmd = new CommandSql(strConnSql, query, EnumDatabaseType.SqlServer);

            // Obtém os dados do banco de dados:
            Isencao _collection = GetCollection <Isencao>(cmd)?.FirstOrDefault <Isencao>();

            return(_collection);
        }
        public Isencao SetIsencao(string tipoIsencao)
        {
            Isencao _i = new Isencao
            {
                IsencaoId   = 0,
                AnuidadeId  = null,
                EventoId    = null,
                Descricao   = "",
                DtAta       = null,
                AnoEvento   = 0,
                TipoIsencao = tipoIsencao,
                Ativo       = true
            };

            return(_i);
        }
        public Task <HttpResponseMessage> Post(Isencao isencao)
        {
            HttpResponseMessage response = new HttpResponseMessage();
            var    tsc       = new TaskCompletionSource <HttpResponseMessage>();
            string resultado = "false";

            try
            {
                if (isencao == null)
                {
                    throw new ArgumentNullException("O objeto 'Isensao' está nulo!");
                }

                resultado = _isencaoApplication.Save(isencao);

                response = Request.CreateResponse(HttpStatusCode.OK, resultado);
                response.ReasonPhrase = resultado;

                tsc.SetResult(response);

                return(tsc.Task);
            }
            catch (Exception ex)
            {
                if (ex.GetType().Name == "InvalidOperationException" || ex.Source == "prmToolkit.Validation")
                {
                    response = Request.CreateResponse(HttpStatusCode.NotFound);
                    response.ReasonPhrase = ex.Message;
                }
                else
                {
                    response = Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message);
                }
                tsc.SetResult(response);

                return(tsc.Task);
            }
        }
 public string Update(int id, Isencao isencao)
 {
     return(_isencaoRepository.Update(id, isencao));
 }
 public string Insert(Isencao isencao)
 {
     return(_isencaoRepository.Insert(isencao));
 }
        public string Update(int id, Isencao i)
        {
            bool   _resultado = false;
            string _msg       = "";

            using (SqlConnection connection = new SqlConnection(strConnSql))
            {
                connection.Open();

                SqlCommand     command = connection.CreateCommand();
                SqlTransaction transaction;

                // Start a local transaction.
                transaction = connection.BeginTransaction("AtualizarIsencao");

                command.Connection  = connection;
                command.Transaction = transaction;

                try
                {
                    string _atributos = "";

                    // Inserindo os dados na tabela:
                    if (i.AnuidadeId != null)
                    {
                        _atributos = _atributos + ", AnuidadeId = @AnuidadeId ";
                        command.Parameters.AddWithValue("AnuidadeId", i.AnuidadeId);
                    }

                    if (i.EventoId != null)
                    {
                        _atributos = _atributos + ", EventoId = @EventoId ";
                        command.Parameters.AddWithValue("EventoId", i.EventoId);
                    }

                    command.CommandText = "" +
                                          "Update dbo.AD_Isencao Set Descricao = @Descricao, " +
                                          "   DtAta = @DtAta, AnoEvento = @AnoEvento, " +
                                          "   TipoIsencao = @TipoIsencao, Ativo = @Ativo " +
                                          "   " + _atributos +
                                          "WHERE IsencaoId = @id ";

                    command.Parameters.AddWithValue("Descricao", i.Descricao);
                    command.Parameters.AddWithValue("DtAta", i.DtAta);
                    command.Parameters.AddWithValue("AnoEvento", i.AnoEvento);
                    command.Parameters.AddWithValue("TipoIsencao", i.TipoIsencao);
                    command.Parameters.AddWithValue("Ativo", i.Ativo);
                    command.Parameters.AddWithValue("id", id);

                    int x = command.ExecuteNonQuery();
                    _resultado = x > 0;

                    _msg = _resultado ? "Atualização Realizada com sucesso" : "Atualização NÃO Realizada com sucesso";

                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    // Attempt to roll back the transaction.
                    try
                    {
                        transaction.Rollback();
                    }
                    catch (Exception ex2)
                    {
                        throw new Exception($"Rollback Exception Type:{ex2.GetType()}. Erro:{ex2.Message}");
                    }
                    throw new Exception($"Commit Exception Type:{ex.GetType()}. Erro:{ex.Message}");
                }
                finally
                {
                    connection.Close();
                }
            }
            return(_msg);
        }
        public string Insert(Isencao i)
        {
            bool   _resultado = false;
            string _msg       = "";
            Int32  id         = 0;
            string _ident     = "";

            using (SqlConnection connection = new SqlConnection(strConnSql))
            {
                connection.Open();

                SqlCommand     command = connection.CreateCommand();
                SqlTransaction transaction;

                // Start a local transaction.
                transaction = connection.BeginTransaction("IncluirIsencao");

                command.Connection  = connection;
                command.Transaction = transaction;

                try
                {
                    string _atributos = "";
                    string _values    = "";

                    // Inserindo os dados na tabela:
                    if (i.AnuidadeId != null)
                    {
                        _atributos = _atributos + ", AnuidadeId ";
                        _values    = _values + ", @AnuidadeId ";
                        command.Parameters.AddWithValue("AnuidadeId", i.AnuidadeId);
                    }

                    if (i.EventoId != null)
                    {
                        _atributos = _atributos + ", EventoId ";
                        _values    = _values + ", @EventoId ";
                        command.Parameters.AddWithValue("EventoId", i.EventoId);
                    }

                    command.CommandText = "" +
                                          "INSERT into dbo.AD_Isencao (Descricao, " +
                                          "   DtAta, AnoEvento, TipoIsencao, Ativo " + _atributos + ") " +
                                          "VALUES(@Descricao, " +
                                          "   @DtAta, @AnoEvento, @TipoIsencao, @Ativo " + _values + ") " +
                                          "SELECT CAST(scope_identity() AS int) ";

                    command.Parameters.AddWithValue("Descricao", i.Descricao);
                    command.Parameters.AddWithValue("DtAta", i.DtAta);
                    command.Parameters.AddWithValue("AnoEvento", i.AnoEvento);
                    command.Parameters.AddWithValue("TipoIsencao", i.TipoIsencao);
                    command.Parameters.AddWithValue("Ativo", i.Ativo);

                    id         = (Int32)command.ExecuteScalar();
                    _resultado = id > 0;

                    if (id > 0)
                    {
                        _ident = _ident.PadLeft(10 - id.ToString().Length, '0') + id.ToString();
                    }

                    _msg = id > 0 ? $"{_ident}Inclusão Realizada com sucesso" : $"{_ident}Inclusão Não Realizada com sucesso";

                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    // Attempt to roll back the transaction.
                    try
                    {
                        transaction.Rollback();
                    }
                    catch (Exception ex2)
                    {
                        throw new Exception($"Rollback Exception Type:{ex2.GetType()}. Erro:{ex2.Message}");
                    }
                    throw new Exception($"Commit Exception Type:{ex.GetType()}. Erro:{ex.Message}");
                }
                finally
                {
                    connection.Close();
                }
            }
            return(_msg);
        }