protected override void Dispose(bool disposing)
 {
     if (disposing)
     {
         db.Dispose();
     }
     base.Dispose(disposing);
 }
Esempio n. 2
0
        public async Task AddRangeAsync(IEnumerable <UserBook> userBooks)
        {
            var bookIds = userBooks.Select(u => u.BookId);

            var books = await _context.Book.Where(b => bookIds.Contains(b.Id)).ToListAsync();

            foreach (var item in userBooks)
            {
                var book = books.Find(b => b.Id == item.BookId);
                if (book.AvailableCount >= item.Count)
                {
                    var entity = Mapper.Map <Entities.UserBook>(item);
                    await _context.AddAsync(entity);

                    book.AvailableCount -= item.Count;
                }
                else
                {
                    _context.Dispose();
                    throw new OrderedBookAvailabilityException("Books available count has been updated!");
                }
            }
            await _context.SaveChangesAsync();
        }