Validation when editing an existing file?

0

I have a following problem: I'm moving the part of controller of my system in the part of edit registers and it does the following verification, if I type an already existing code, it displays a alert .

ModelState.AddModelError("UserNotFound", 
                         "Este Paciente já está cadastrado!: ");

And I wanted the alert to inform you of the patient ID that was registered with the one that made the following change.

ModelState.AddModelError("UserNotFound",
                         "Este Paciente já está cadastrado!: " + obj.CadastroId);

Only when I use obj.CadastroId it is displaying the Patient ID I'm editing. In short, when the alert appears, display the Patient ID that has already been registered with that data.

Can anyone help me with this? I left on this link the method I'm using in controller .

Repository:

public bool pacienteExiste(string numero_protuario)  
{  
    Cadastro cadastro = Db.Cadastros
         .FirstOrDefault(c => c.pront == numero_protuario);  
    return cadastro != null;  
} 

Service:

public bool pacienteExiste(string numero_protuario)
{
    return _cadastroRepository.pacienteExiste(numero_protuario);
}        

Controller:

[HttpPost]  
public ActionResult EditarCadastro(Cadastro obj)  
{  
    if (_cadastroService.pacienteExiste(obj.pront))  
    {  
        ModelState.AddModelError("UserNotFound", 
                "Este Paciente já está cadastrado!: " + obj.CadastroId);  
        return View(obj);  
    }  
    if ((obj.pront != null) || (obj.inpac != null) || (obj.dtnasc != null))  
    {  
        _cadastroService.Update(obj);  
    }  
    else  
    {  
        return RedirectToAction("Index", "Cadastro");  
    }  
    return RedirectToAction("Index", "Cadastro");  
}  
    
asked by anonymous 24.02.2017 / 14:40

2 answers

0

It is only necessary to search this record in the bank to return the information of it.

Enjoy the method that searches the registry for validation, removing the need to use _cadastroService.pacienteExiste(obj.pront) . So the query only needs to be done once in the database.

In fact, I realized that a service doing a middle ground (for all that ???) so I'll follow your pattern.

Repository:

public Cadastro BuscaPacientePeloProtuario(string numero_protuario)
{
    return Db.Cadastros.FirstOrDefault(c => c.pront == numero_protuario);  
}

Service:

public Cadastro BuscaPacientePeloProtuario(string numero_protuario)
{
    return _cadastroRepository.BuscaPacientePeloProtuario(numero_protuario);
}

Controller

[HttpPost]  
public ActionResult EditarCadastro(Cadastro obj)  
{  
    var existente = _cadastroService.BuscaPacientePeloProtuario(obj.pront);

    if (existente != null)
    {             
        ModelState.AddModelError("UserNotFound", "Este Paciente já está cadastrado!: " + existente.CadastroId);

        return View(obj);  
    }  

    if ((obj.pront != null) || (obj.inpac != null) || (obj.dtnasc != null))  
    {  
        _cadastroService.Update(obj);  
    }  
    else  
    {  
        return RedirectToAction("Index", "Cadastro");  
    }  

    return RedirectToAction("Index", "Cadastro");  
}  
    
24.02.2017 / 14:53
0

Thinking about a more elegant code I would recommend you to use Tell Do not Ask

Ta, but what does this help with your problem? By improving the design and you can return the error by encapsulating your user's validation logic. Here's an example below of how your code could be written, just adapt its structure, variable names and patterns:

 enum Status
{
    Sucesso,
    Falha
}

interface IErros
{
    void AdicionarErro(IErros erro);
}

interface IResultado
{
    Status Status { get; }
    IErros Erros { get; }
}

class Falha : IResultado
{
    public IErros Erros { get; private set; }

    public Status Status { get { return Status.Falha; } }

    public IResultado AdicionarErro(string mensagem)
    {
        // adiciona o erro
        return this;
    }
}

class Sucesso : IResultado
{
    public IErros Erros { get; private set; }

    public Status Status { get { return Status.Sucesso; } }
}

class Cadastro
{
    public int Id { get; set; }
    public string Nome { get; set; }
    public string NumeroProntuario { get; set; }
    public DateTime DataNascimento { get; set; }
}

class Db
{
    public static IEnumerable<Cadastro> Cadastros { get; set; }
}

class CadastroService
{
    public IResultado SalvarPaciente(Cadastro dadosCadastrais)
    {
        Cadastro cadastro = Db.Cadastros
             .FirstOrDefault(c => c.NumeroProntuario == dadosCadastrais.NumeroProntuario);
        if (cadastro != null)
            return new Falha().AdicionarErro("Já foi cadastrado um paciente com os mesmos dados cadastrais, Id: " + cadastro.Id);
        return new Sucesso();
    }
}

I made a small mock for db so I did not have to generate EF class and context. But note, that instead of returning a bool, you encapsulate the logic of your error and take the responsibility of the web and can access in your controller as follows:

    public IActionResult EditarCadastro(Cadastro obj)
    {
        var resultado = _cadastroService.SalvarPaciente(obj);
        if (resultado.Status == Status.Falha)
            ModelState.AddModelError(resultado.Erros.First());

        // faz o restando da lógica
    }

With this we get a better designed design and all the complexity of the encapsulated validations in the service.

    
24.02.2017 / 15:57