Implement Text Approval Flow

Asked

Viewed 128 times

2

I am developing a project where the user can publish texts, but before being posted on the site needs to be approved, something similar to the invitations of friendship of social networks where it is necessary to wait for the approval of another person or system before implementing the action.

On my table textos bank has the following fields:

  • idusuari (who wrote the text),
  • idtexto,
  • text and
  • idadmin (who will approve the text or not).

The biggest doubt is how to store this data as the id admin is mandatory and the text may take a few days to be approved or not, there is no way to do the "Insert" directly in the bank.

Someone could help me implement it?

  • 2

    did not understand your question exactly, but if your business rule says that to be published it needs to be OK (opa, it turns rs attribute) then at least you already need a field in your table called status and/or approved(yes/no) to validate this state anywhere in your system. And as you say, if you need an admin to approve, it should NOT be required (idadmin) at the time of insertion.

1 answer

3

I’m guessing your system uses Entity Framework.

I’ll deduce your Model text:

public class Texto 
{
    [Key]
    public int TextoId { get; set; }
    public int UsuarioId { get; set; }
    public int AdminId { get; set; }

    [DataType(DataType.MultilineText)]
    [Required]
    public String Texto { get; set; }

    public virtual Usuario Usuario { get; set; }
    public virtual Usuario Admin { get; set; }
}

If the text needs approval, it is impossible for there to be no persistence in the process (or, as you say, no insertion). What you can do is enrich your Model so that this text does not appear face-first when inserted. That is to say:

public class Texto 
{
    [Key]
    public int TextoId { get; set; }
    public int UsuarioId { get; set; }
    public int AdminId { get; set; }

    [DataType(DataType.MultilineText)]
    [Required]
    public String Texto { get; set; }

    public Boolean Aprovado { get; set; }
    public DateTime? DataAprovacao { get; set; } 

    public virtual Usuario Usuario { get; set; }
    public virtual Usuario Admin { get; set; }
}

Booleans in the Entity Framework are default false, then inserting a new text, it is released as Aprovado = false;.

Once done, just write a Action for the administrator to approve the text. Something like this:

public ActionResult Aprovar(int id)
{
    var texto = contexto.Textos.SingleOrDefault(t => t.TextoId == id);

    if (UsuarioEhAdministrador) // Coloque aqui seu critério para verificar se usuário é Administrador
    {
        texto.Aprovado = true;
        texto.DataAprovacao = DateTime.Now;

        contexto.Entry(texto).State = EntityState.Modified;
        contexto.SaveChanges();

        return RedirectToAction("Index", "Textos");
    }
}

And to not display unapproved texts, use:

var textos = contexto.Textos.Where(t => T.Aprovado).ToList();
  • got it. But what the insertion action would look like. Why even if it is approved later, should the admin id enter at the right time? Or I can leave the admin id as null to insert later with approval?

  • @Guilhermevinicius The ideal is that the Id of the approver is put in the approval, so any admin will be able to approve. The logic is also simpler. Insertion is therefore trivial, only with the text and author id.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.