Esempio n. 1
0
        public int Add(Order order)
        {
            var books = _bookRepository.GetAllAvailable();

            var orderBooks = books.Where(x => order.OrderBooks.Any(y => y.BookId == x.Id)).ToList();

            var orderDto = new OrderDto()
            {
                BuyerId       = order.BuyerId,
                Comment       = order.Comment,
                OrderDateTime = order.OrderDateTime,
                OrderStatusId = order.OrderStatusId,
                SellerId      = order.SellerId,
                SummaryPrice  = orderBooks.Sum(x => x.Price * order.OrderBooks.FirstOrDefault(y => y.BookId == x.Id).Count)
            };

            var orderId = _orderRepository.Add(orderDto);

            foreach (var orderBook in order.OrderBooks)
            {
                _orderRepository.AddOrderBook(orderId, orderBook.BookId, orderBook.Count);

                var goodDto = _goodRepository.GetGoodByBookId(orderBook.BookId);
                goodDto.Count = goodDto.Count - orderBook.Count;

                _goodRepository.UpdateGood(goodDto);
            }

            return(orderId);
        }