How to list bookings registered by a single administrator? (1:N)

Asked

Viewed 84 times

4

I register my attributes from the Reserve class, logged in as an Administrator. I would like to know how to list these registered reservations, by a specific administrator?

Example: Administrator 1, registered 2 reservations. Administrator 2, registered 3 reservations.

How to show in View, registered reservations? Because in the way mine is, it simply shows all reservations. It doesn’t matter which administrator is logged in.

Method of Listing Reservations:

public ActionResult Index()
{
    return View(db.Reservas.ToList());
} 

In View, I use this Model: @model IEnumerable<FoodInTime.Models.Reserva>

Reserve Class:

public class Reserva
{
    [Key]
    public int ReservaID { get; set; }

    [Required(ErrorMessage = "Preencha o horário para reserva")]
    [DisplayName("Horário")]
    [DataType(DataType.Time)]
    public string Horario { get; set; }

    [Required(ErrorMessage = "Preencha o limite de pedidos/hora")]
    [DisplayName("Limite de Pedidos/Hora")]
    public int LimitePedidos { get; set; }

    [Required(ErrorMessage = "Preencha a mesa")]
    [DisplayName("Mesa")]
    [StringLength(10, MinimumLength = 1, ErrorMessage = "A mesa deve ter no máximo 10 caracteres")]
    public string Mesa { get; set; }

    [Required(ErrorMessage = "Preencha o valor da reserva")]
    [DisplayName("Valor")]
    public double Valor { get; set; }

    public virtual ICollection<Restaurante> Restaurantes { get; set; }
}

2 answers

3

The first thing I can notice is that there is no link between your reservations and who actually made the reservation.

It will be nice to put a property in the class Reserva to create this link, something like:

class Reserva
{
    // .. outra propriedades
    // ID do Administrador que fez a reserva
    public int ReservadoPor { get; set; }
}

Thus, one can do as in the other answer, and consult the reservations:

var reservas = db.Reservas.Find(r => r.ReservadoPor == IdDoUsuarioLogado).ToList();

So you will have all reservations made by the logged in user.

I also saw in the comments of other answers you are using Session to keep user information logged in. Well, don’t do this.

  • Sessions are allocated in the IIS Pool: when Apppool memory is recycled, your data stored in Session is released;
  • Sessions are of exclusive access: Whenever an access to the Session is made, every execution pipe is locked until the access is finalized. So you can only have one access to Session at a time. Imagine this with multiple users connected simultaneously.
  • Session are not scalable: As it is allocated in Apppool, if hosted on scalable servers, this information will not be shared among other hosts. So if your app uses Session, it’s not possible to scale horizontally.

That’s why we use Cache instead of Session. And that’s also why there are several Cache solutions on the market today.

Just don’t forget that Cache is shared throughout the application. So if you’re going to cache a user’s data, simply Cache["DadosDoUsuario" + UsuarioId] and be happy.

You can use the System.Web.Caching.Cache per hour, and if you really need to invest in Cache data, go to a Redis, for example.

1


Just use the Where from the Line and pass the id you want.

public ActionResult Index() 
{ 
     return View(db.Reservas.Where(n => n.administrador == 1 ).ToList()); 
} 
  • I understood the logic, but I am using Session in my program. There are several administrators. For example, I use: httpcontext.current.Session["ID"] to get my administrator ID. However, the controller cannot use this inside. How could I take the session ID and enter this code above?

  • Just one thing, there is an error in the above code. From where that administrator in lowercase, will be able to work? When we pass the letter to choose the class, it doesn’t even appear as an option.

  • You have to adapt to the field of your model. You have not posted as it is.

  • What am I sending here for you to take a look at?

Browser other questions tagged

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