예제 #1
0
        public async Task CanLoanBook()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString())
                          .EnableSensitiveDataLogging()
                          .Options;

            await using var context = new ApplicationDbContext(options);

            var       userId = Guid.NewGuid().ToString();
            const int bookId = -1;

            context.Books.Add(new Book("foo", "bar", "test book")
            {
                ID = bookId
            });
            context.Users.Add(new User {
                UserName = "******", ID = userId
            });
            await context.SaveChangesAsync();

            var service = new BookShelfService(context);
            var user    = await service.FindUserAsync(userId);

            var book = await service.FindBookAsync(bookId);

            await service.RegisterLoanAsync(book, user);

            var exists = await context.Loans.AnyAsync(l => l.Book == book && l.User == user);

            Assert.True(exists);
        }
예제 #2
0
        public async Task Loan([FromRoute] int bookId)
        {
            var currentUserName = User.FindFirst(ClaimTypes.Name).Value;

            if (currentUserName == null)
            {
                throw new InvalidOperationException("User does not have a Name/ID");
            }

            var user = await _bookShelfService.FindUserAsync(currentUserName);

            var book = await _bookShelfService.FindBookAsync(bookId);

            if (book.Loan != null)
            {
                throw new InvalidOperationException($"Book is already loaned by {book.Loan.User.Name}.");
            }

            await _bookShelfService.RegisterLoanAsync(book, user);
        }