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

4

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

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

How to show in the View, the reservations registered? For in the way that mine is, it simply shows all reservations. It does not matter which administrator is logged in.

Reservation List Method:

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; }
}
    
asked by anonymous 23.09.2016 / 08:52

2 answers

1

Just use linq's Where and pass the id you want.

public ActionResult Index() 
{ 
     return View(db.Reservas.Where(n => n.administrador == 1 ).ToList()); 
} 
    
23.09.2016 / 11:27
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 Reserva class that created this link, something like this:

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

So, you 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 comments from other responses that you are using Session to keep user information logged in. Well, do not do that .

  • Sessions are allocated in the IIS Pool: When the AppPool memory is recycled, its data stored in Session is released;
  • Sessions are exclusive access: Whenever Session access is performed, every execution pipe is locked until access is terminated. So you can only have one Session access at a time. Imagine this with multiple connected users 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, you can not scale horizontally.

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

Just remember that Cache is shared by every application. So if you are going to cache a user's data, simply Cache["DadosDoUsuario" + UsuarioId] and be happy.

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

    
27.09.2016 / 18:04