Пример #1
0
        public async Task RemoveBasketBookAsync(int basketId, int bookId)
        {
            Basket basket = await _basketRepository.GetByIdAsync(basketId);

            if (basket == null)
            {
                throw new NotFoundException(nameof(Basket), basket);
            }

            Book book = await _bookRepository.GetByIdAsync(bookId);

            if (book == null)
            {
                throw new NotFoundException(nameof(Book), book);
            }

            var bookBasket = basket.BasketBooks.ToList();
            BookBasketJunction bookBasketJunction = bookBasket.FirstOrDefault(p => p.BookId == bookId);

            if (bookBasketJunction != default)
            {
                bookBasket.Remove(bookBasketJunction);
                basket.BasketBooks = bookBasket;

                await _basketRepository.UpdateAsync(basket);

                _cacheManager.Remove(CacheKeys.GetBasketKey(basketId));
            }
        }
Пример #2
0
        public async Task AddBasketBookAsync(int basketId, int bookId)
        {
            Basket basket = await _basketRepository.GetByIdAsync(basketId);

            if (basket == null)
            {
                throw new NotFoundException(nameof(Basket), basket);
            }

            Book book = await _bookRepository.GetByIdAsync(bookId);

            if (book == null)
            {
                throw new NotFoundException(nameof(Book), book);
            }

            var bookBasket = basket.BasketBooks.ToList();

            bookBasket.Add(new BookBasketJunction(basketId, bookId));
            basket.BasketBooks = bookBasket;

            await _basketRepository.UpdateAsync(basket);

            _cacheManager.Remove(CacheKeys.GetBasketKey(basketId));
        }
Пример #3
0
        public async Task <BasketDTO> GetBasketByIdAsync(int basketId)
        {
            if (_cacheManager.IsSet(CacheKeys.GetBasketKey(basketId)))
            {
                return(_mapper.Map <BasketDTO>(_cacheManager.Get <Basket>(CacheKeys.GetBasketKey(basketId))));
            }

            var basket = await _basketRepository.GetByIdAsync(basketId);

            if (basket == null)
            {
                throw new NotFoundException(nameof(Basket), basket);
            }

            _cacheManager.Set <Basket>(CacheKeys.GetBasketKey(basket.Id), basket, CacheTimes.BasketCacheTime);
            return(_mapper.Map <BasketDTO>(basket));
        }
Пример #4
0
        public async Task RemoveAllBasketBooksAsync(int basketId)
        {
            Basket basket = new Basket();

            if ((basket = await _basketRepository.GetByIdAsync(basketId)) == null)
            {
                throw new NotFoundException(nameof(Basket), basket);
            }

            var bookBasket = basket.BasketBooks.ToList();

            bookBasket.Clear();
            basket.BasketBooks = bookBasket;

            await _basketRepository.UpdateAsync(basket);

            _cacheManager.Remove(CacheKeys.GetBasketKey(basketId));
        }