예제 #1
0
        public async Task <ServiceResponse <Book> > AddBookToAuthor(BooksToAuthors newBookToAuthor)
        {
            ServiceResponse <Book> response = new ServiceResponse <Book>();

            using (var context = ContextFactory.CreateDbContext(ConnectionString))
            {
                try
                {
                    Book book = await context.Books
                                .Include(c => c.Publisher)
                                .Include(c => c.BooksToAuthors).ThenInclude(cs => cs.Author)
                                .FirstOrDefaultAsync(c => c.Id == newBookToAuthor.BookId);

                    if (book == null)
                    {
                        response.Success      = false;
                        response.ErrorMessage = "Book not found.";
                        return(response);
                    }
                    Author author = await context.Authors
                                    .FirstOrDefaultAsync(s => s.Id == newBookToAuthor.AuthorId);

                    if (author == null)
                    {
                        response.Success      = false;
                        response.ErrorMessage = "Book not found.";
                        return(response);
                    }

                    BooksToAuthors booksToAuthors = new BooksToAuthors
                    {
                        Book   = book,
                        Author = author
                    };

                    await context.AddAsync(booksToAuthors);

                    await context.SaveChangesAsync();

                    response.Success = true;
                    response.Data    = book;
                }
                catch (Exception ex)
                {
                    response.Success      = false;
                    response.ErrorMessage = ex.Message;
                }
                return(response);
            }
        }
예제 #2
0
        public ActionResult AddAuthorToBook(int AuthorId, int BookId)
        {
            try
            {
                BooksToAuthors booksToAuthors = new BooksToAuthors();
                booksToAuthors.IdAuthor = AuthorId;
                booksToAuthors.IdBook   = BookId;
                var transaction = unitOfWork.BeginTransaction();
                unitOfWork.BooksToAuthorsRep.Create(booksToAuthors);
                transaction.Commit();

                return(Json(new { isOk = true, Errors = "" }));
            }
            catch (Exception exc)
            {
                return(Json(new { isOk = false, Errors = exc.Message }));
            }
        }
예제 #3
0
 public void Update(BooksToAuthors booksToAuthors)
 {
     session.Update(booksToAuthors);
 }
예제 #4
0
 public void Create(BooksToAuthors booksToAuthors)
 {
     session.Save(booksToAuthors);
 }