예제 #1
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            if ((await _context.Authors.FirstOrDefaultAsync(author =>
                                                            author.LastName == LastNameOfAuthor && author.FirstName == FirstNameOfAuthor)) == null)
            {
                await _context.Authors.AddAsync(new Author()
                {
                    FirstName = FirstNameOfAuthor,
                    LastName  = LastNameOfAuthor
                });

                await _context.SaveChangesAsync();
            }

            Book.Author = await _context.Authors.FirstOrDefaultAsync(author =>
                                                                     author.LastName == LastNameOfAuthor && author.FirstName == FirstNameOfAuthor);

            await _context.Books.AddAsync(Book);

            await _context.SaveChangesAsync();

            LogMessage = $"Book {Book.Title} added to the database.";

            return(RedirectToPage("/Catalog"));
        }
예제 #2
0
        public async Task <Book> AddAsync(Book newBook)
        {
            await _context.AddAsync(newBook);

            await _context.SaveChangesAsync();

            return(newBook);
        }
예제 #3
0
        public async Task <Author> AddAsync(Author newAuthor)
        {
            await _context.Authors.AddAsync(newAuthor);

            await _context.SaveChangesAsync();

            return(await _context.Authors.FirstOrDefaultAsync(author =>
                                                              author.LastName == newAuthor.LastName && author.FirstName == newAuthor.FirstName));
        }