TheCSharpAcademy / CONSOLE.PointOfSale

6 stars 15 forks source link

Create Add Order Functionality #44

Closed TheCSharpAcademy closed 1 year ago

TheCSharpAcademy commented 1 year ago

This is a slightly more complicated ticket, so it will be worth 20 XPs.

An order will populate two tables: order and orderproduct (which keeps track of the many to many relationship between the two entities). This can be achieved automatically with EF's help. An example can be found here. https://stackoverflow.com/questions/65510449/how-can-i-directly-fill-join-table-generated-by-ef-core-for-many-to-many-relatio

Architecture-wise, in the AddNewOrder method in MainMenu, you'll have to follow these steps: Show a list of products Get the user input for which product they want to add. Get the user input if they want to add more products or finish the order Upon finishing the order, pass the input to the Kebab Controller Create a method in the kebab controller where the order will be added to the DB. Show a message to the user saying the order was added successfully.

Please let me know if you have questions.

kimfom01 commented 1 year ago

I have a question. When the user selects a product (by id) to add to their order, I assign the id to the ProductId property of OrderProduct object but I am finding it impossible to add the current order's id to the OrderId of the OrderProduct object. Is creating the order's id my responsibility or the db's responsibility? If it is mine, how can I go about that?

TheCSharpAcademy commented 1 year ago

@kimfom01 EF should be able to take care of that as far as I'm concerned: See if this helps: https://stackoverflow.com/questions/71411414/many-to-many-relation-with-ef-6-crud.

Looks like you create everything in one go, looping through the products in the order and creating a new OrderProduct for each, which will contain an Order object.

kimfom01 commented 1 year ago

Thank you