@@ -47,8 +47,9 @@ public OrderService(final OrderRepository orderRepository, final ProductService
4747 @ Transactional
4848 public OrderResponse addOrder (final OrderRequest orderRequest ) {
4949 final Order order = new Order ();
50- addLineItemsToOrder (order , orderRequest .lineItemRequests ());
51- return convertToResponse (orderRepository .save (order ));
50+ Order createdOrder = orderRepository .save (order );
51+ addLineItemsToOrder (createdOrder , orderRequest .lineItemRequests ());
52+ return convertToResponse (orderRepository .save (createdOrder ));
5253 }
5354
5455 /**
@@ -95,16 +96,29 @@ private void addLineItemsToOrder(final Order order, final List<LineItemRequest>
9596 final List <Long > lineItemProductIds = lineItemRequests .stream ()
9697 .map (LineItemRequest ::productId )
9798 .toList ();
99+
100+ final List <Product > products = getUnRetiredProductsListFromDatabase (lineItemProductIds );
101+
102+ final Map <Product , Integer > lineItems = convertProductsListToMapWithQuantity (products , lineItemRequests );
103+
104+ // clear existing order line items
105+ order .clearLineItems ();
106+
107+ // add line items to the order.
108+ lineItems .forEach ((k , v ) -> order .addLineItem (new LineItem (order , k , v )));
109+ }
110+
111+ private List <Product > getUnRetiredProductsListFromDatabase (List <Long > productIdsList ){
98112 // get all products from line Item list from database
99113 final List <Product > products = productService .findAllProductsInListOfIds (
100- lineItemProductIds
114+ productIdsList
101115 );
102116
103117 // get product id list from database
104118 final List <Long > foundProductIdsList = products .stream ().map (Product ::getId ).toList ();
105119
106120 // check if any of products did not exist in database, and if so throw Not Found exception
107- final List <Long > notFoundProducts = new ArrayList <>(lineItemProductIds );
121+ final List <Long > notFoundProducts = new ArrayList <>(productIdsList );
108122 notFoundProducts .removeAll (foundProductIdsList );
109123 if (!notFoundProducts .isEmpty ()){
110124 throw new NotFoundException (String .format ("Products with ID's %s do not exist" , notFoundProducts ));
@@ -120,23 +134,20 @@ private void addLineItemsToOrder(final Order order, final List<LineItemRequest>
120134 if (!retiredProducts .isEmpty ()){
121135 throw new RetirementException (String .format ("Products with ID's %s are retired" , retiredProducts ));
122136 }
137+ return products ;
138+ }
123139
140+ private Map <Product , Integer > convertProductsListToMapWithQuantity (List <Product > products , List <LineItemRequest > lineItemRequests ){
124141 // create helper map of product ids and products
125142 final Map <Long , Product > helper = products
126143 .stream ()
127144 .collect (Collectors .toMap (Product ::getId , Function .identity ()));
128145
129146 // create hashmap of product object and line item quantity
130- final Map < Product , Integer > lineItems = lineItemRequests
147+ return lineItemRequests
131148 .stream ()
132149 .collect (Collectors .toMap (lineItemRequest -> helper .get (lineItemRequest .productId ()),
133150 LineItemRequest ::quantity ));
134-
135- // clear existing order line items
136- order .clearLineItems ();
137-
138- // add line items to the order.
139- lineItems .forEach ((k , v ) -> order .addLineItem (new LineItem (order , k , v )));
140151 }
141152
142153 /**
0 commit comments