List reservations by Administrator

Asked

Viewed 60 times

0

I am trying to list my bookings by different administrators. However, as I did, it lists all bookings.

That’s the method I’m using:

public ActionResult Index()
    {
        Administrador administrador = db.Administradores.FirstOrDefault(a => a.Email == User.Identity.Name);
        if (administrador != null)
        {
            return View(db.Reservas.ToList());
        }
        else
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }


    }

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; }
}

Restaurant Class:

public abstract class Restaurante
{
    [Key]
    public int RestauranteID { get; set; }

    [Required(ErrorMessage = "Preencha o nome do Restaurante")]
    [DisplayName("Restaurante")]
    [StringLength(50, MinimumLength = 5, ErrorMessage = "O nome do restaurante deve ter entre 5 e 50 caracteres.")]
    public string Nome { get; set; }

    [Required(ErrorMessage = "Preencha o tipo de cozinha")]
    [DisplayName("Cozinha")]
    public int CozinhaID { get; set; }
    public virtual Cozinha Cozinha { get; set; }

    [Required(ErrorMessage = "Preencha o telefone")]
    [DisplayName("Telefone")]
    [StringLength(12, MinimumLength = 10, ErrorMessage = "O número de telefone deve haver 12 caracteres.")]
    public string Telefone { get; set; }

    [Required(ErrorMessage = "Preencha o CNPJ")]
    [DisplayName("Cnpj")]
    [StringLength(18, MinimumLength = 18, ErrorMessage = "O CNPJ deve haver no máximo, 18 caracteres.")]
    public string Cnpj { get; set; }

    [Required(ErrorMessage = "Preencha o CEP")]
    [DisplayName("CEP")]
    [StringLength(9, MinimumLength = 8, ErrorMessage = "O CEP deve haver no máximo 9 caracteres.")]
    public string Cep { get; set; }

    [Required(ErrorMessage = "Preencha a Cidade")]
    [DisplayName("Cidade")]
    [StringLength(50, MinimumLength = 5, ErrorMessage = "A cidade deve ter entre 5 e 50 caracteres.")]
    public string Cidade { get; set; }

    [Required(ErrorMessage = "Preencha o Estado")]
    [DisplayName("Estado")]
    [StringLength(2, MinimumLength = 2, ErrorMessage = "A sigla do estado deve haver 2 caracteres.")]
    public string Estado { get; set; }

    [Required(ErrorMessage = "Preencha o Bairro")]
    [DisplayName("Bairro")]
    [StringLength(50, MinimumLength = 5, ErrorMessage = "O bairro deve ter entre 5 e 50 caracteres.")]
    public string Bairro { get; set; }

    [Required(ErrorMessage = "Preencha a Rua")]
    [DisplayName("Rua")]
    [StringLength(50, MinimumLength = 5, ErrorMessage = "A rua deve ter entre 5 e 50 caracteres.")]
    public string Rua { get; set; }

    [Required(ErrorMessage = "Preencha a número do estabelecimento")]
    [DisplayName("Número")]
    public int NumRua { get; set; }

    [Required(ErrorMessage = "Preencha o nome do responsável pelo estabelecimento")]
    [DisplayName("Nome")]
    [StringLength(50, MinimumLength = 5, ErrorMessage = "O nome do responsável deve ter entre 5 e 50 caracteres.")]
    public string NomeResp { get; set; }

    [Required(ErrorMessage = "Preencha o sobrenome do responsável pelo estabelecimento")]
    [DisplayName("Sobrenome")]
    [StringLength(50, MinimumLength = 5, ErrorMessage = "O sobrenome do responsável deve ter entre 5 e 50 caracteres.")]
    public string SobrenomeResp { get; set; }

    [Required(ErrorMessage = "Preencha o Email")]
    [DisplayName("Email")]
    [DataType(DataType.EmailAddress)]
    [EmailAddress(ErrorMessage = "E-mail inválido")]
    [StringLength(255, MinimumLength = 3, ErrorMessage = "O email deve ter entre 3 e 255 caracteres.")]
    public string Email { get; set; }

    [Required(ErrorMessage = "Preencha o Celular")]
    [DisplayName("Telefone")]
    [StringLength(15, MinimumLength = 10, ErrorMessage = "O número de celular deve haver entre 10 e 15 caracteres.")]
    public string Celular { get; set; }

    [Required(ErrorMessage = "Preencha a senha")]
    [DisplayName("Senha")]
    [StringLength(50, MinimumLength = 3, ErrorMessage = "A senha deve ter entre 3 e 50 caracteres.")]
    [DataType(DataType.Password)]
    public string Senha { get; set; }

    public virtual Usuario Usuario { get; set; }

    [DisplayName("Foto")]
    public string Foto { get; set; }

    public virtual Reserva Reserva { get; set; }

    public virtual Produto Produto { get; set; }

}

Restaurant is abstract, because the Administrator class inherits all fields from it. But it is empty.

  • within Reservations is there anything that indicates that it is from a particular Administrator? , put the Reservations and Administrator classes in your question

  • There you go. I put.

  • Are you recording this right there? like there are values in Restaurant? #strange

  • There is, it’s just an Abstract. It’s a mother class. The registrations, edits, I do all with the Administrator class, who inherits the Restaurant class fields. But in the database is saved in the table restaurant. =)

  • So I would not do so, because in the Administrator class there are no additional fields and I see no logic for that. If you have to take the relationship between the fields that in your Restaurant class does not have!?

  • But it’s just a detail that can be changed. The question is, I want to list reservations from the administrator. That’s it. But I don’t know how to write it within the method.

  • What is the field (or key) that relates Reserve and Restaurant, thinking of Class model (logical) related to bank model (physical) ???

  • As written in the code above, the restaurant takes the Reservaid attribute from Reserve. And reservation has restaurants.

  • Where the Reservarid field within Restaurant? just remembering in Entity Framework it has to exist! (different for example from Nhibernate)

  • I will put. Right after that, what is the procedure for listing? The method, in this case.

  • First thing, tidy up your entire layout, placing the keys and structuring your base to the ORM Entity Framework. Has a line Administrador administrador = db.Administradores.FirstOrDefault(a => a.Email == User.Identity.Name); If you bring in the administrator, you’ll have reservations and a restaurant, all for relationships. Without the relationship working gets complicated and with them is the answer of what you need.

Show 6 more comments

1 answer

0

This brings all the reservations, obviously:

return View(db.Reservas.ToList());

This modeling doesn’t make any sense. A reservation can only belong to a restaurant:

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

    ...

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

Switch to:

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

    ...

    public virtual Restaurante Restaurante { get; set; }
}

By its modeling, there is no correlation between Restaurante and Administrador. It needs to be defined somehow.

My suggestion is to link Administrador to a Cozinha. For example:

public class AdministradorCozinha
{
    [Key]
    public int AdministradorCozinhaID { get; set; }
    [Index("IX_UQ_AdministradorCozinha_Administrador_Cozinha", Order = 1, IsUnique = true)]
    public int AdministradorID { get; set; }
    [Index("IX_UQ_AdministradorCozinha_Administrador_Cozinha", Order = 2, IsUnique = true)]
    public int CozinhaID { get; set; }

    public virtual Administrador Administrador { get; set; }
    public virtual Cozinha Cozinha { get; set; }
}

Then it becomes simple:

public class Administrador 
{
    [Key]
    public int AdministradorID { get; set; }

    ...

    public virtual ICollection<AdministradorCozinha> Cozinhas { get; set; }
}

Finally:

public ActionResult Index()
{
    var administrador = db.Administradores
                          .Include(a => a.Cozinhas)
                          .FirstOrDefault(a => a.Email == User.Identity.Name);

    if (administrador != null)
    {
        return View(administrador.Cozinhas.SelectMany(c => c.Reservas));
    }

    return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}

Browser other questions tagged

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