コード例 #1
0
        public async Task AddAsync(Book newBook)
        {
            var entity = Mapper.Map <Entities.Book>(newBook);
            await _context.AddAsync(entity);

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException e)
            {
                if (e.InnerException is SqlException sqlException)
                {
                    if (sqlException.Number == 547)
                    {
                        throw new MemberRelationException("Check entered value. ",
                                                          sqlException.Message);
                    }
                    else
                    {
                        throw;
                    }
                }
                else
                {
                    throw;
                }
            }
        }
コード例 #2
0
        public async Task AddAsync(User newUser)
        {
            var entity = Mapper.Map <Entities.User>(newUser);
            await _context.AddAsync(entity);

            await _context.SaveChangesAsync();
        }
コード例 #3
0
        public async Task AddAsync(Author newAuthor)
        {
            var entity = Mapper.Map <Entities.Author>(newAuthor);
            await _context.AddAsync(entity);

            await _context.SaveChangesAsync();
        }
コード例 #4
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();
        }
コード例 #5
0
        public async Task AddAsync(User user)
        {
            await _context.AddAsync(user);

            await _context.SaveChangesAsync();
        }