public string Save(Anuidade a) { ArgumentsValidator.RaiseExceptionOfInvalidArguments( RaiseException.IfTrue(a.Exercicio == 0, "Exercício não informado") ); Anuidade _a = new Anuidade { AnuidadeId = a.AnuidadeId, Exercicio = a.Exercicio, DtVencimento = a.DtVencimento, DtInicioVigencia = a.DtInicioVigencia, DtTerminoVigencia = a.DtTerminoVigencia, CobrancaLiberada = a.CobrancaLiberada, DtCobrancaLiberada = a.DtCobrancaLiberada, DtCadastro = a.DtCadastro, Ativo = a.Ativo }; try { if (_a.AnuidadeId == 0) { return(_anuidadeService.Insert(_a)); } else { return(_anuidadeService.Update(a.AnuidadeId, _a)); } } catch (Exception ex) { throw ex; } }
public Anuidade SetAnuidade() { Anuidade _a = new Anuidade { AnuidadeId = 0, Exercicio = 0, DtVencimento = DateTime.Now, DtInicioVigencia = DateTime.Now, DtTerminoVigencia = DateTime.Now, CobrancaLiberada = false, DtCobrancaLiberada = null, DtCadastro = DateTime.Now, Ativo = false }; return(_a); }
public Anuidade GetAnuidadeById(int id) { try { List <DbParameter> _parametros = new List <DbParameter>(); query = @"SELECT AnuidadeId, Exercicio, DtVencimento, DtInicioVigencia, DtTerminoVigencia, CobrancaLiberada, DtCobrancaLiberada, DtCadastro, Ativo FROM dbo.AD_Anuidade WHERE AnuidadeId = @id"; // Definição do parâmetros da consulta: SqlParameter paramId = new SqlParameter() { ParameterName = "@id", Value = id }; _parametros.Add(paramId); // Fim da definição dos parâmetros // Define o banco de dados que será usando: CommandSql cmd = new CommandSql(strConnSql, query, EnumDatabaseType.SqlServer, parametros: _parametros); // Obtém os dados do banco de dados: Anuidade _anuidade = GetCollection <Anuidade>(cmd)?.First(); // Log da consulta: string log = logRep.SetLogger(className + "/GetAnuidadeById", "SELECT", "ANUIDADE", id, query, _anuidade != null ? "SUCESSO" : "0"); // Fim Log return(_anuidade); } catch (Exception ex) { throw ex; } }
public Task <HttpResponseMessage> Post(Anuidade anuidade) { HttpResponseMessage response = new HttpResponseMessage(); var tsc = new TaskCompletionSource <HttpResponseMessage>(); string resultado = "false"; try { if (anuidade == null) { throw new ArgumentNullException("O objeto 'Anuidade' está nulo!"); } resultado = _anuidadeApplication.Save(anuidade); response = Request.CreateResponse(HttpStatusCode.OK, 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, Anuidade 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("AtualizarAnuidade"); command.Connection = connection; command.Transaction = transaction; try { command.CommandText = "" + "UPDATE dbo.AD_Anuidade " + "SET Exercicio = @Exercicio, DtVencimento = @DtVencimento, " + " DtInicioVigencia = @DtInicioVigencia, DtTerminoVigencia = @DtTerminoVigencia, " + " CobrancaLiberada = @CobrancaLiberada, DtCobrancaLiberada = @DtCobrancaLiberada, " + " Ativo = @Ativo " + "WHERE AnuidadeId = @id"; command.Parameters.AddWithValue("Exercicio", a.Exercicio); command.Parameters.AddWithValue("DtVencimento", a.DtVencimento); command.Parameters.AddWithValue("DtInicioVigencia", a.DtInicioVigencia); command.Parameters.AddWithValue("DtTerminoVigencia", a.DtTerminoVigencia); command.Parameters.AddWithValue("CobrancaLiberada", a.CobrancaLiberada); command.Parameters.AddWithValue("DtCobrancaLiberada", a.DtCobrancaLiberada); 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 ANUIDADE: 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", "ANUIDADE", id, _instrucaoSql, _result); //Fim do Log } catch (Exception ex) { try { transaction.Rollback(); } catch (Exception ex2) { _msg = $"ATENÇÃO: Ocorreu um erro ao tentar ATUALIZAR ANUIDADE: 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", "ANUIDADE", 0, ex.Message, "FALHA"); _msg = $"ATENÇÃO: Ocorreu um erro ao tentar ATUALIZAR ANUIDADE: 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(Anuidade 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("IncluirAnuidade"); command.Connection = connection; command.Transaction = transaction; try { command.CommandText = "" + "INSERT into dbo.AD_Anuidade (Exercicio, DtVencimento, DtInicioVigencia, " + " DtTerminoVigencia, CobrancaLiberada, DtCobrancaLiberada) " + "VALUES (@Exercicio, @DtVencimento, @DtInicioVigencia, " + " @DtTerminoVigencia, @CobrancaLiberada, @DtCobrancaLiberada) " + "SELECT CAST(scope_identity() AS int) "; command.Parameters.AddWithValue("Exercicio", a.Exercicio); command.Parameters.AddWithValue("DtVencimento", a.DtVencimento); command.Parameters.AddWithValue("DtInicioVigencia", a.DtInicioVigencia); command.Parameters.AddWithValue("DtTerminoVigencia", a.DtTerminoVigencia); command.Parameters.AddWithValue("CobrancaLiberada", a.CobrancaLiberada); command.Parameters.AddWithValue("DtCobrancaLiberada", a.DtCobrancaLiberada); 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 ANUIDADE: 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", "ANUIDADE", 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 ANUIDADE: 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", "ANUIDADE", 0, ex.Message, "FALHA"); _msg = $"ATENÇÃO: Ocorreu um erro ao tentar INCLUIR ANUIDADE: 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 Update(int id, Anuidade anuidade) { return(_anuidadeRepository.Update(id, anuidade)); }
public string Insert(Anuidade anuidade) { return(_anuidadeRepository.Insert(anuidade)); }