0
I have a method where I do a certain validation and depending on the scenario, I need to return only an alert and continue with the request or a possible Badrequest also returning a message.
I thought I’d try to access a variable inside mine if using the angular. Or send some feedback to my controller of angular.
I need to get the variable that’s in if :
(if (damMesAnterior != null))
In case it meets this validation.
private async Task VerificarDam(NotaViewModel nota, Prestador prestador)
{
var timezone = TimeZoneInfo.FindSystemTimeZoneById("E. South America Standard Time");
var dataAtual = TimeZoneInfo.ConvertTime(DateTime.Now, timezone);
if (dataAtual.Day >= 16)
{
var damDoisMesesAnterior = await (DbContext.ValidarPagamentoDAM(nota.Competencia.Value, prestador.Id));
var damMesAnterior = await (DbContext.VerificarDamCompAnterior(nota.Competencia.Value, prestador.Id));
if (damMesAnterior != null)
{
//Nesse bloco que preciso enviar essa variavel para o front
var msg = "Mensagem de teste atraso.....";
}
if (damDoisMesesAnterior != null)
{
if (prestador.DataLiberacao != null)
{
if (prestador.DataLiberacao < dataAtual)
{
BadRequest(
"Existem pendências. Entre em contato com ...");
}
}
}
}
}
Angular controller where I get request data:
$scope.emitir = function () {
notaService.emitir($scope.nota).then(function (results) {
var nota = results.data;
logService.success('teste mensagem de sucesso.');
var email = nota.user.email;
notaService.emitirXML(nota.value2.prestador.value1, nota.value2, nota.value3).then(function (resultsXML) {
if (email) {
var url = 'nota.html#/nota/' + nota.prestador.value1 + '/' + nota.value2 + '/' + notaGerada.verificador;
var baseLen = $location.absUrl().length - $location.url().length;
url = $location.absUrl().substring(0, baseLen - 1) + url;
notaService.enviarNota(url, email).then(function (results) {
notaService.apagarXML(nota.prestador.value1, nota.value2, nota.value3).then(function (result) {
}, function (error)
{
logService.log(error);
console.log(error);
});
},
function (error)
{
logService.log(error);
console.log(error);
});
}
}, function (error) {
logService.log(error);
});
$scope.newNota();
$scope.initNota();
$window.open('nota.html#/nota/' + nota.prestador.value1 + '/' + nota.value2 + '/' + nota.value3);
}, function (error) {
logService.log(error);
$scope.nota.valid = true;
});
};
Method is where I make certain validations and return to the public method (which is my action), which is accessed by angular:
public async Task<IHttpActionResult> Emitir(NotaViewModel nota)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var prestador = await DbContext.GetContribuinteByUserIdAsync(this.User.Identity.GetUserId());
await VerificarDam(nota, prestador);
var valueNota = new Nota
{
Valor1 = nota.Valor1,
Valor2 = nota.Valor2,
Valor3 = nota.Valor3,
Valor = nota.CofiValorns,
};
DbContext.Servicos.Add(valueNota);
await DbContext.SaveChangesAsync();
nota = await GetNotaAsync(valueNota);
return Ok(nota);
}
That method Check is a
action
of yourcontroller
?– Barbetta
Can you add this public method to the question?
– Barbetta
It’s a basic registration method. If you really need it, I can share the code.
– Vinícius
is that the return to the angular is in this access method, I can make an example without seeing it, but it is better to see
– Barbetta
I’ll share then. Just a second.
– Vinícius
in his
IHttpActionResult
you already return an object of type "note"(return Ok(nota);
), if to return something fromVerificarDam
will change this return, this is what you want?– Barbetta
Yes. But this message I only need to send according to the rules applied in the method. It can happen the situation where I can also perform an Insert and in it n return any of these validation messages.
– Vinícius