I am trying to add data to related entities. But I think the way I code is really not appropriate. Should I use SharedService instead of Shared Controller.
Here is my Code
[HttpPost("addtocart")]
public IActionResult AddToCart([FromBody] AddToCart addToCart)
{
_cartService.Add(addToCart.Cart);
addToCart.Product.CartId = addToCart.Cart.Id;
_productService.Add(addToCart.Product);
addToCart.ProductOption.ProductId = addToCart.Product.Id;
_productOptionService.Add(addToCart.ProductOption);
return Ok("Added");
}
Cart.cs
public class Cart:IEntity
{
public int Id { get; set; }
public int UserId { get; set; }
public DateTime CreatedDate { get; set; }
public virtual ICollection<Product> Products { get; set; }
}
Product.cs
public class Product:IEntity
{
public int Id { get; set; }
public string SerialNumber { get; set; }
public string StockCode { get; set; }
public int CartId { get; set; }
public int Quantity { get; set; }
public virtual ICollection<ProductOption> ProductOptions { get; set; }
[ForeignKey(nameof(CartId))]
public virtual Cart Cart { get; set; }
}
ProductOption.cs
public class ProductOption:IEntity
{
public int Id { get; set; }
public int ProductId { get; set; }
public int OptionId { get; set; }
[ForeignKey(nameof(ProductId))]
public virtual Product Product { get; set; }
[ForeignKey(nameof(OptionId))]
public virtual Option Options { get; set; }
}
How can I make it better. Thanks...