Pedro Nunes
Pedro Nunes

Reputation: 412

Online Shop - Create an order with multiple products MVC .net

So I am building an online shop using Code-First MVC

So I created this model classes for now (don't take the types of the attributes too serious for now):

Products (Produto):

 public Produto()
        {
            ListaProdutoEncomenda = new HashSet<Produto_Encomenda>();
        }
        public int ProdutoID { get; set; }

        [Required]
        [StringLength(50)]
        public string Nome { get; set; }

        [Required]
        public double Preco { get; set; }

        [Required]
        public double Peso { get; set; }

        [Required]
        [StringLength(255)]
        public string Descricao { get; set; }

        [Required]
        public double IVA { get; set; }

        public string Imagem { get; set; }

        public DateTime UltimaAtualizacao { get; set; }

        public int Stock { get; set; }

        public int CategoriaID {get;set;}

        public virtual ICollection<Produto_Encomenda> ListaProdutoEncomenda { get; set; }
    }

Encomenda (Order):

public class Encomenda
{
    public Encomenda()
    {
        ListaProdutoEncomenda = new HashSet<Produto_Encomenda>();
    }
    [Key]
    public int IDEncomenda { get; set; }

    [Required]
    public DateTime DataSubmissao { get; set; }

    [Required]
    public DateTime DataEnvio { get; set; }

    [Required]
    public int EstadoEnvioID { get; set; }

    [StringLength(50)]
    public string NomeDestino { get; set; }

    [Required]
    public int TipoExpedicaoID { get; set; }

    [Required]
    public int RegiaoDestinoID { get; set; }

    [StringLength(50)]
    public string MoradaDestino { get; set; }

    [StringLength(50)]
    public string CodPostalDestino { get; set; }

    [Required]
    [StringLength(50)]
    public string MoradaFaturacao { get; set; }

    [Required]
    [StringLength(50)]
    public string CodPostalFaturacao { get; set; }

    public virtual ICollection<Produto_Encomenda> ListaProdutoEncomenda { get; set; }

}

And the connection table between the produtos (Products) and Encomenda (Order)

   public class Produto_Encomenda
    {

        [Key]
        public int IDProduto_Encomenda { get; set; }

        [Required]
        public string NomeProduto { get; set; }

        [Required]
        public int Quantidade { get; set; }

        [Required]
        public float preco { get; set; }

        [Required]
        public float IVA { get; set; }


        public virtual Encomenda Encomenda { get; set; }

        public virtual Produto Produto { get; set; }


        [ForeignKey("Encomenda")]
        public int IDEncomendaFK { get; set; }

        [ForeignKey("Produto")]
        public int IDProdutoFK { get; set; }

    }

So my question is.. Let's pretend that a costumer buys 2 or 3 products or more. How can I store all this products in a single line of an order? Cheers and thanks a lot in advance for the time spent reading.

Upvotes: 0

Views: 1739

Answers (2)

Himaan Singh
Himaan Singh

Reputation: 707

I guess you are looking to make a viewmodel

Create a class that contains Products and Encomenda class as property -

Model -

     public class MyViewModel
     {
       public Produto Pinst{get;set;}
       public Encomenda Einst{get;set;}
     }

Controller or method-

    public void SomeMethod()
    {
       List<MyViewModel> lst = new List<MyViewModel>();
       //Now suppose  
       foreach(var items in listThatGetCreatedWithBuyerproductInfo)
       {
         MyViewModel obj = new MyViewModel ();
         obj.Pinst = new Produto();
         obj.Einst = new Encomenda();

         //Here goes your properties from item in respected class instances 
         obj.Pinst.Nome  = items.Nome; 
         obj.Einst.DataSubmissao = items.DataSubmissao; 
         //when you are done loading add obj to list 
         lst.Add(obj);
       } 
    }

Hope it Helps if it does not tell me !!

Upvotes: 1

Chris Pratt
Chris Pratt

Reputation: 239300

I'm not sure what you mean by "a single line of an order". Each product is a separate line item, and your entities already model this through the many-to-many relationship.

However, in general this setup is a very bad idea. Your order should not be directly related to products. Instead, your order should simply have an order item, and you'll create those order items based on the products that were sold. The reason for this is that products are very likely to change. If a product is removed because it's no longer available, for example, that doesn't negate the fact that it was previously sold in an order. However, in order for referential integrity to be maintained, any orders sold with that product would have to also have their relationship with that product removed. By having an entirely separate entity, i.e. order item, products can come and go, while the already created orders remain unaffected.

Upvotes: 1

Related Questions