1

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...

2
  • It is hard to underestand what do you want. If you need to save addToCart items to DB then post AddToCart and Product classes pls. Commented Jun 14, 2021 at 10:33
  • I have added the entities. Commented Jun 14, 2021 at 13:48

2 Answers 2

1

For starters, why do you have data handling both in service (where it should be) and in controller? your service should do both. The tutorials usually have a service per model - but in real applications services are related to some business functions. For example here you can have one service (say, cartService) that handles all the actions related to cart.

But now we are moving to the realm of opinion, which should be avoided on Stack Overflow

Also, your post subject is completely unrelated to the post itself (nothing about Entity framework, or cascade, or delete)

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your answer. I am a learner so What you say is really important for me and wanna learn best practise. I made different Services for single responsibility(SOLID). But, I guess I'm wrong
Single responsibility isn't the same as single model... "Unwrapping" the object in controller and dispatching to different services is breaking encapsulation, which is much worse!
I will consider your advice. Thanks for your time
0

Sending related JSON data to only CartService solved my problem.

{
    "userId": 2,
    "createdDate": "2021-05-24T00:00:00",
    "products": [
        {
            "serialNumber": "000000499",
            "stockCode": "SP150049  ",
            "quantity": 1,
            "productOptions": null
        }
    ]
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.