예제 #1
0
        public async Task <Book> AddBook(BookDTO bookDto)
        {
            Book newBook = new Book();

            newBook.Title        = bookDto.Title;
            newBook.ISBN         = bookDto.ISBN;
            newBook.YearReleased = bookDto.YearReleased;

            var result = await context.Books.AddAsync(newBook);

            context.SaveChanges();
            newBook = result.Entity;
            if (newBook.Id != 0)
            {
                if (bookDto.AuthorIds != null && bookDto.AuthorIds.Length > 0)
                {
                    foreach (int aid in bookDto.AuthorIds)
                    {
                        BookAuthor ba = new BookAuthor();
                        ba.AuthorId = aid;
                        ba.BookId   = newBook.Id;
                        var res = await context.BookAuthors.AddAsync(ba);

                        await context.SaveChangesAsync();
                    }
                }
                await context.SaveChangesAsync();
            }
            return(newBook);
        }
예제 #2
0
        public async Task <Book> UpdateBook(BookDTO bookDto)
        {
            var book = await context.Books.FirstOrDefaultAsync(e => e.Id == bookDto.Id);

            if (book != null)
            {
                book.Title        = bookDto.Title;
                book.ISBN         = bookDto.ISBN;
                book.YearReleased = bookDto.YearReleased;

                var result = context.Books.Update(book);
                context.SaveChanges();

                book = result.Entity;
                if (book.Id != 0)
                {
                    if (bookDto.AuthorIds != null && bookDto.AuthorIds.Length > 0)
                    {
                        foreach (int aid in bookDto.AuthorIds)
                        {
                            // ja tem essx autorx
                            var ba = book.BookAuthors.FirstOrDefault(ba => ba.AuthorId == aid);
                            if (ba != null)
                            {
                                continue;
                            }

                            // novo autor
                            ba          = new BookAuthor();
                            ba.AuthorId = aid;
                            ba.BookId   = book.Id;
                            var res = await context.BookAuthors.AddAsync(ba);
                        }
                    }
                    // apagar bookAuthors que nao estejam nesses ids
                    var outdatedAuthors = book.BookAuthors.Where(ba => bookDto.AuthorIds.Any(id => id != ba.AuthorId));
                    Console.WriteLine("OUTDATED");
                    foreach (var ba in outdatedAuthors)
                    {
                        Console.WriteLine(ba.Author.Name);
                        //context.BookAuthors.Remove(ba);
                    }


                    await context.SaveChangesAsync();
                }
                return(book);
            }

            try
            {
                await context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                ;
            }

            return(book);
        }
예제 #3
0
        public async Task <BookAuthor> AddBookAuthor(BookAuthor bookAuthor)
        {
            //if (bookAuthor.BookId != 0)
            //{
            //    bookAuthor.Book = await context.Books.FirstOrDefaultAsync(e => e.Id == bookAuthor.BookId);
            //}
            //if (bookAuthor.AuthorId != 0)
            //{
            //    bookAuthor.Author = await context.Authors.FirstOrDefaultAsync(e => e.Id == bookAuthor.AuthorId);
            //}
            Console.WriteLine("_____________" + bookAuthor.ToString());
            var result = await context.BookAuthors.AddAsync(bookAuthor);

            await context.SaveChangesAsync();

            return(result.Entity);
        }