public BasketItemView(BasketItem basketItem) { BookRepository bookRepository; BookVariableInfoRepository bookVariableInfoRepository; BasketRepository basketRepository; PaymentRepository paymentRepository; bookRepository = new BookRepository(); bookVariableInfoRepository = new BookVariableInfoRepository(); basketRepository = new BasketRepository(); paymentRepository = new PaymentRepository(); this.Id = basketItem.Id; this.Amount = basketItem.Amount; this.BasketId = basketItem.BasketId; this.BookVariableInfoId = basketItem.BookVariableInfoId; this.ItemCount = basketItem.ItemCount; this.PaymentId = basketItem.PaymentId; this.BookVariableInfo = (BookVariableInfo) bookVariableInfoRepository.FindById(this.BookVariableInfoId); this.Book = (Book)bookRepository.FindById(this.BookVariableInfoId); if (this.BasketId != null) this.Basket = (Basket)basketRepository.FindById((Int32)this.BasketId); if (this.PaymentId != null) this.Payment = (Payment)paymentRepository.FindById((Int32)this.PaymentId); }
public void Edit(BasketItem newBasketItem) { BasketItem oldBasketItem; using (BookMarketDbContext db = new BookMarketDbContext()) { db.EditBasketItem(newBasketItem.Id, newBasketItem.Amount, newBasketItem.ItemCount, newBasketItem.BookVariableInfoId, newBasketItem.BasketId, newBasketItem.PaymentId); } }
//Добавление экземпляров книги в корзину покупателя //Должна быть транзакция public bool AddBookItem(Basket basket, BasketItem basketItem) { Int32 allBasketItemCount; Book book; BookVariableInfo bookVariableInfo, newBookVariableInfo; BasketRepository basketRepository; if (basket != null && basketItem != null) { if (basket.BasketItems == null) basket.BasketItems = new List<BasketItem>(); bookVariableInfo = (BookVariableInfo) base._bookVariableInfoRepository.FindById( basketItem.BookVariableInfoId); book = (Book)this._bookRepository.FindById(bookVariableInfo.Id); //Валидация. Общее число заказанного товара не должно быть >= имеющегося числа allBasketItemCount = BasketItemCounting(basketItem); if (allBasketItemCount + basketItem.ItemCount > bookVariableInfo.ProductCount) return false; //Добавление заказанной книги basketItem.BasketId = basket.Id; basketItem.Amount = bookVariableInfo.Price * basketItem.ItemCount; base._basketItemRepository.Add(basketItem); //Обновление корзины basket.TotalAmount += basketItem.Amount; basketRepository = new BasketRepository(); basketRepository.Edit(basket); return true; } else { throw new Exception("Корзина или элемент корзины не заданы!"); } return false; }
//Подсчет числа экзмемпляров книг корзины private Int32 BasketItemCounting(BasketItem basketItem) { Int32 basketProductCount; IList<Int32> itemsCount; IList<BasketItem> allBasketItems; BasketItemRepository basketItemRepository; basketItemRepository = (BasketItemRepository) this._basketItemRepository; allBasketItems = basketItemRepository .FindAllByBasket((Int32)basketItem.BasketId); // Связь 1:1 BookVariableInfo и Book itemsCount = (from searchBasketItem in allBasketItems where searchBasketItem.BookVariableInfoId == basketItem.BookVariableInfoId select searchBasketItem.ItemCount).ToList<Int32>(); return itemsCount.Sum(); }
//Обновление экземпляров книги в корзине покупателя //Должна быть транзакция public bool UpdateBookItem(BasketItem newBasketItem) { Int32 allBasketItemCount; Basket basket; Book book; BookVariableInfo bookVariableInfo; BasketItem oldBasketItem; BasketRepository basketRepository; BasketItemRepository basketItemRepository; if (newBasketItem != null) { basketRepository = (BasketRepository)this._basketRepository; basketItemRepository = (BasketItemRepository) base._basketItemRepository; bookVariableInfo = (BookVariableInfo) base._bookVariableInfoRepository.FindById( newBasketItem.BookVariableInfoId); book = (Book)this._bookRepository.FindById(bookVariableInfo.Id); basket = (Basket)this._basketRepository.FindById( (Int32)newBasketItem.BasketId); oldBasketItem = (BasketItem)base._basketItemRepository .FindById(newBasketItem.Id); //Валидация. Общее число заказанного товара не должно быть >= имеющегося числа allBasketItemCount = BasketItemCounting(newBasketItem); if (allBasketItemCount - oldBasketItem.ItemCount + newBasketItem.ItemCount > bookVariableInfo.ProductCount) return false; // //Обновление корзины basket.TotalAmount -= oldBasketItem.Amount; basket.TotalAmount += newBasketItem.ItemCount * bookVariableInfo.Price; basketRepository.Edit(basket); //Обновление заказанной книги newBasketItem.Amount = bookVariableInfo.Price * newBasketItem.ItemCount; basketItemRepository.Edit(newBasketItem); return true; } else { throw new Exception("Элемент корзины не задан!"); } return false; }
//Извлечение BasketItem из BasketItemView public BasketItem GetBasketItem(BasketItemView basketItemView) { BasketItem basketItem; BasketRepository basketRepository; BookVariableInfoRepository bookVariableInfoRepository; PaymentRepository paymentRepository; basketItem = new BasketItem() { Id = basketItemView.Id, Amount = basketItemView.Amount, BasketId = basketItemView.BasketId, BookVariableInfoId = basketItemView.BookVariableInfoId, ItemCount = basketItemView.ItemCount, PaymentId = basketItemView.PaymentId }; basketItem.BookVariableInfo = (BookVariableInfo) this._bookVariableInfoRepository.FindById( basketItem.BookVariableInfoId); if (basketItem.BasketId != null) basketItem.Basket = (Basket) this._basketRepository.FindById((Int32)basketItem.BasketId); if (basketItem.PaymentId != null) basketItem.Payment = (Payment) this._paymentRepository.FindById((Int32)basketItem.PaymentId); return basketItem; }
//Удаление экземпляров книги в корзине покупателя //Должна быть транзакция public bool DeleteBasketItem(BasketItem basketItem) { Book book; BookVariableInfo bookVariableInfo; Basket basket; BasketItem oldBasketItem; BasketRepository basketRepository; BasketItemRepository basketItemRepository; if (basketItem != null) { basketRepository = (BasketRepository)this._basketRepository; basketItemRepository = (BasketItemRepository) base._basketItemRepository; basket = (Basket)this._basketRepository.FindById( (Int32)basketItem.BasketId); bookVariableInfo = (BookVariableInfo) base._bookVariableInfoRepository.FindById( basketItem.BookVariableInfoId); //Обновление корзины basket.TotalAmount -= basketItem.Amount; basketRepository.Edit(basket); //Удаление заказанной книги basketItemRepository.Delete(basketItem.Id); return true; } else { throw new Exception("Элемент корзины не задан!"); } return false; }