Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Add a product with many prices to a shopping cart

$
0
0

I am trying to add a product with many prices to a shopping cart(a one-to-many relationship entity framework). The customer has chosen a special size with its price in the product presentation (a short list with buttons on the right). Now I want to add this selected product, size and price to my shopping cart. The problem is - I don't know how to access the specific price (the particular value in the ItemPrice-list).

To start with - here are my classes and my DbContext:

public class Item
{

private readonly ICollection<ItemPrice> Price;
public Item(ICollection<ItemPrice> price = null)
{
Price = price;
}
public Item() : this(null) { }

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

[Required(ErrorMessage = "Please enter an product name")]
public string Name { get; set; }

[Required(ErrorMessage = "Please specify a category")]
public string Category { get; set; }

public string SubCategory { get; set; }

public string Description { get; set; }

public string ImageSmallUrl { get; set; }

public string ImageSmallAlternativeDescription { get; set; }

public string ImageSmallContentType { get; set; }


public virtual ICollection<ItemPrice> Prices { get; set; }
}

public class ItemPrice
{
[Key]
public int ID { get; set; }
public int ItemID { get; set; }
public string Size { get; set; }
public decimal Value { get; set; }

public virtual Item Item { get; set; }

}

public class ApplicationDbContext : DbContext
{
public ApplicationDbContext
(DbContextOptions<ApplicationDbContext>
options)
:base(options) {}

public DbSet<Item> Items { get; set; }
public DbSet<ItemPrice> ItemPrices { get; set; }

protected override void OnModelCreating (ModelBuilder modelBuilder)
{
modelBuilder.Entity<ItemPrice>()
.HasOne(p => p.Item)
.WithMany(b => b.Prices);

}
}

I have started to work on the shopping cart and the cartItem but now I am stuck. I don't know how to continue. Here are the ShoppingCart and the CartItem classes:


public class ShoppingCart
{
private List<CartItem> cartItemCollection = new List<CartItem>();

public virtual void AddCartItem(Item item, int quantity)
{
CartItem cartItem = cartItemCollection
.Where(p => p.Item.ItemID == item.ItemID)
.FirstOrDefault();
if(cartItem == null)
{
cartItemCollection.Add(new CartItem
{
Item = item,
Quantity = quantity
});
}
else
{
cartItem.Quantity += quantity;
}
}

public virtual void RemoveCartItem(Item item) =>
cartItemCollection.RemoveAll(l => l.Item.ItemID == item.ItemID);

public virtual decimal ComputeTotalValue()
{
// I WANT TO ACHIEVE THIS:
return cartItemCollection.Sum(e => e.Item.Price *
e.Quantity);
// BUT BECAUSE MY ITEM (aka: PRODUCT) HAS MANY PRICES I AM STUCK
HERE:
return cartItemCollection.Sum(e => e.Item.Prices.) *
e.Quantity)
}
}

public class CartItem
{
public int CarItemID { get; set; }
public Item Item { get; set; }
public int Quantity { get; set; }
}

I have tried different lambda-expression and LINQ but I cannot reach the value so it can be multiplied by the quantity value. How do I get the particular value (one of the properties of the ItemPrice-class) so I can retrieve a sum. Very grateful for help here!


Viewing all articles
Browse latest Browse all 9386

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>