public string Save(AssinaturaEventoDao a)
        {
            AssinaturaEvento assinaturaEvento = new AssinaturaEvento
            {
                AssinaturaEventoId   = a.AssinaturaEventoId,
                AssociadoId          = a.AssociadoId,
                ValorEventoPublicoId = a.ValorEventoPublicoId,
                PercentualDesconto   = a.PercentualDesconto,
                TipoDesconto         = a.TipoDesconto,
                DtAssinatura         = a.DtAssinatura,
                DtAtualizacao        = a.DtAtualizacao,
                Ativo = a.Ativo
            };

            try
            {
                if (assinaturaEvento.AssinaturaEventoId == 0)
                {
                    return(_assinaturaEventoService.Insert(assinaturaEvento));
                }
                else
                {
                    return(_assinaturaEventoService.Update(assinaturaEvento.AssinaturaEventoId, assinaturaEvento));
                }
            }
            catch (Exception ex)
            {
                return(ex.Message);
            }
        }
 public string Update(int id, AssinaturaEvento assinaturaEvento)
 {
     return(_assinaturaEventoRepository.Update(id, assinaturaEvento));
 }
 public string Insert(AssinaturaEvento assinaturaEvento)
 {
     return(_assinaturaEventoRepository.Insert(assinaturaEvento));
 }
        public string Update(int id, AssinaturaEvento a)
        {
            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("AtualizarAssinaturaEvento");

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

                try
                {
                    command.CommandText = "" +
                                          "UPDATE dbo.AD_Assinatura_Evento " +
                                          "SET PercentualDesconto  = @PercentualDesconto, TipoDesconto = @TipoDesconto, " +
                                          "   DtAtualizacao = @DtAtualizacao, Ativo = @Ativo " +
                                          "WHERE AssinaturaEventoId = @id";

                    command.Parameters.AddWithValue("PercentualDesconto", a.PercentualDesconto);
                    command.Parameters.AddWithValue("TipoDesconto", a.TipoDesconto);
                    command.Parameters.AddWithValue("DtAtualizacao", DateTime.Now);
                    command.Parameters.AddWithValue("Ativo", a.Ativo);
                    command.Parameters.AddWithValue("id", id);

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

                    _msg = x > 0 ? "Atualização realizada com sucesso" : "Atualização NÃO realizada com sucesso";

                    transaction.Commit();

                    // Log do UPDATE ASSINATURA_EVENTO:
                    StringBuilder sb = new StringBuilder();
                    sb.Append("Parâmetros: ");

                    for (int z = 0; z < command.Parameters.Count; z++)
                    {
                        sb.Append(command.Parameters[z].ParameterName + ": " + command.Parameters[z].Value + ", ");
                    }

                    _instrucaoSql = sb.ToString();
                    _result       = x > 0 ? "SUCESSO" : "FALHA";

                    string log = logRep.SetLogger(className + "/Update",
                                                  "UPDATE", "ASSINATURA_EVENTO", id, _instrucaoSql, _result);
                    //Fim do Log
                }
                catch (Exception ex)
                {
                    try
                    {
                        transaction.Rollback();
                    }
                    catch (Exception ex2)
                    {
                        _msg = $"ATENÇÃO: Ocorreu um erro ao tentar ATUALIZAR ASSINATURA_EVENTO: Commit Exception Type:{ex2.GetType()}. Erro:{ex2.Message}";
                        // throw new Exception($"Rollback Exception Type:{ex2.GetType()}. Erro:{ex2.Message}");
                    }

                    string log = logRep.SetLogger(className + "/Update",
                                                  "UPDATE", "ASSINATURA_EVENTO", 0, ex.Message, "FALHA");

                    _msg = $"ATENÇÃO: Ocorreu um erro ao tentar ATUALIZAR ASSINATURA_EVENTO: Commit Exception Type:{ex.GetType()}. Erro:{ex.Message}";
                    // throw new Exception($"Commit Exception Type:{ex.GetType()}. Erro:{ex.Message}");
                }
                finally
                {
                    connection.Close();
                }
            }
            return(_msg);
        }
        public string Insert(AssinaturaEvento a)
        {
            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("IncluirAssinaturaEvento");

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

                try
                {
                    command.CommandText = "" +
                                          "INSERT into dbo.AD_Assinatura_Evento (AssociadoId, ValorEventoPublicoId, PercentualDesconto, " +
                                          "       TipoDesconto, DtAssinatura, DtAtualizacao ) " +
                                          "VALUES (@AssociadoId, @ValorEventoPublicoId, @PercentualDesconto, " +
                                          "       @TipoDesconto, @DtAssinatura, @DtAtualizacao) " +
                                          "SELECT CAST(scope_identity() AS int) ";

                    command.Parameters.AddWithValue("AssociadoId", a.AssociadoId);
                    command.Parameters.AddWithValue("ValorEventoPublicoId", a.ValorEventoPublicoId);
                    command.Parameters.AddWithValue("PercentualDesconto", a.PercentualDesconto);
                    command.Parameters.AddWithValue("TipoDesconto", a.TipoDesconto);
                    command.Parameters.AddWithValue("DtAssinatura", DateTime.Now);
                    command.Parameters.AddWithValue("DtAtualizacao", DateTime.Now);

                    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();

                    // Log da Inserção ASSINATURA_EVENTO:
                    StringBuilder sb = new StringBuilder();
                    sb.Append("Parâmetros: ");

                    for (int z = 0; z < command.Parameters.Count; z++)
                    {
                        sb.Append(command.Parameters[z].ParameterName + ": " + command.Parameters[z].Value + ", ");
                    }

                    _instrucaoSql = sb.ToString();
                    _result       = id > 0 ? "SUCESSO" : "FALHA";

                    string log = logRep.SetLogger(className + "/Insert",
                                                  "INSERT", "ASSINATURA_EVENTO", id, _instrucaoSql, _result);
                    //Fim do Log
                }
                catch (Exception ex)
                {
                    // Attempt to roll back the transaction.
                    try
                    {
                        transaction.Rollback();
                    }
                    catch (Exception ex2)
                    {
                        _msg = $"ATENÇÃO: Ocorreu um erro ao tentar INCLUIR ASSINATURA_EVENTO: Commit Exception Type:{ex2.GetType()}. Erro:{ex2.Message}";
                        //throw new Exception($"Rollback Exception Type:{ex2.GetType()}. Erro:{ex2.Message}");
                    }

                    string log = logRep.SetLogger(className + "/Insert",
                                                  "INSERT", "ASSINATURA_EVENTO", 0, ex.Message, "FALHA");

                    _msg = $"ATENÇÃO: Ocorreu um erro ao tentar INCLUIR ASSINATURA_EVENTO: Commit Exception Type:{ex.GetType()}. Erro:{ex.Message}";
                    // throw new Exception($"Commit Exception Type:{ex.GetType()}. Erro:{ex.Message}");
                }
                finally
                {
                    connection.Close();
                }
            }
            return(_msg);
        }