Implement Text Approval Flow

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 approval of another person or system before carrying out the action .

In my table textos of the bank has the following fields:

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

The biggest question is how to store this data because as id of admin is mandatory and the text may take a few days to be approved or not, there is no way to insert the database directly into the database.

Could anyone help me implement it?

    
asked by anonymous 04.10.2014 / 01:53

1 answer

3

I'm assuming that your system uses Entity Framework.

I'll deduce your Model from texts:

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-to-face when inserted. That is:

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 , so by inserting new text, it is cast as Aprovado = false; .

Once this is done, you only need to type Action to 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();
    
04.10.2014 / 02:07