diff --git a/exercise.webapi/DTO/AuthorDTO.cs b/exercise.webapi/DTO/AuthorDTO.cs new file mode 100644 index 0000000..d886def --- /dev/null +++ b/exercise.webapi/DTO/AuthorDTO.cs @@ -0,0 +1,13 @@ +using exercise.webapi.Models; + +namespace exercise.webapi.DTO +{ + public class AuthorDTO + { + public int Id { get; set; } + public string FirstName { get; set; } + public string LastName { get; set; } + public string Email { get; set; } + public Listbooks { get; set; } + } +} diff --git a/exercise.webapi/DTO/AuthorDTOnoBooks.cs b/exercise.webapi/DTO/AuthorDTOnoBooks.cs new file mode 100644 index 0000000..a91d75d --- /dev/null +++ b/exercise.webapi/DTO/AuthorDTOnoBooks.cs @@ -0,0 +1,10 @@ +namespace exercise.webapi.DTO +{ + public class AuthorDTOnoBooks + { + public int id { get; set; } + public string FirstName { get; set; } + public string LastName { get; set; } + public string Email { get; set; } + } +} diff --git a/exercise.webapi/DTO/AuthorPublisherDTO.cs b/exercise.webapi/DTO/AuthorPublisherDTO.cs new file mode 100644 index 0000000..92a0cae --- /dev/null +++ b/exercise.webapi/DTO/AuthorPublisherDTO.cs @@ -0,0 +1,11 @@ +namespace exercise.webapi.DTO +{ + public class AuthorPublisherDTO + { + public int Id { get; set; } + public int PublisherId { get; set; } + public int AuthorId { get; set; } + public AuthorDTOnoBooks author { get; set; } + public PublisherDTO publisher { get; set; } + } +} diff --git a/exercise.webapi/DTO/BookDTO.cs b/exercise.webapi/DTO/BookDTO.cs new file mode 100644 index 0000000..bba5840 --- /dev/null +++ b/exercise.webapi/DTO/BookDTO.cs @@ -0,0 +1,13 @@ +using exercise.webapi.Models; + +namespace exercise.webapi.DTO +{ + public class BookDTO + { + public int id { get; set; } + public string title { get; set; } + + public AuthorDTOnoBooks author { get; set; } + public PublisherNoBooks publisher { get; set; } + } +} diff --git a/exercise.webapi/DTO/BookDTONoAuthor.cs b/exercise.webapi/DTO/BookDTONoAuthor.cs new file mode 100644 index 0000000..7075c0c --- /dev/null +++ b/exercise.webapi/DTO/BookDTONoAuthor.cs @@ -0,0 +1,9 @@ +namespace exercise.webapi.DTO +{ + public class BookDTONoAuthor + { + public int id { get; set; } + public string title { get; set; } + public PublisherNoBooks publisher { get; set; } + } +} diff --git a/exercise.webapi/DTO/BookDTONoPublisher.cs b/exercise.webapi/DTO/BookDTONoPublisher.cs new file mode 100644 index 0000000..96a9c93 --- /dev/null +++ b/exercise.webapi/DTO/BookDTONoPublisher.cs @@ -0,0 +1,9 @@ +namespace exercise.webapi.DTO +{ + public class BookDTONoPublisher + { + public int id { get; set; } + public string title { get; set; } + public AuthorDTOnoBooks author { get; set; } + } +} diff --git a/exercise.webapi/DTO/Payload.cs b/exercise.webapi/DTO/Payload.cs new file mode 100644 index 0000000..5c21204 --- /dev/null +++ b/exercise.webapi/DTO/Payload.cs @@ -0,0 +1,9 @@ +using exercise.webapi.Models; + +namespace exercise.webapi.DTO +{ + public class Payload where T : class + { + public T Data {get;set;} + } +} diff --git a/exercise.webapi/DTO/PublisherDTO.cs b/exercise.webapi/DTO/PublisherDTO.cs new file mode 100644 index 0000000..29e3138 --- /dev/null +++ b/exercise.webapi/DTO/PublisherDTO.cs @@ -0,0 +1,10 @@ +namespace exercise.webapi.DTO +{ + public class PublisherDTO + { + public int Id { get; set; } + public string FirstName { get; set; } + public string LastName { get; set; } + public List Books { get; set; } + } +} diff --git a/exercise.webapi/DTO/PublisherNoBooks.cs b/exercise.webapi/DTO/PublisherNoBooks.cs new file mode 100644 index 0000000..c517b21 --- /dev/null +++ b/exercise.webapi/DTO/PublisherNoBooks.cs @@ -0,0 +1,9 @@ +namespace exercise.webapi.DTO +{ + public class PublisherNoBooks + { + public int Id { get; set; } + public string FirstName { get; set; } + public string LastName { get; set; } + } +} diff --git a/exercise.webapi/Data/DataContext.cs b/exercise.webapi/Data/DataContext.cs index b6be7a9..3b3828f 100644 --- a/exercise.webapi/Data/DataContext.cs +++ b/exercise.webapi/Data/DataContext.cs @@ -24,9 +24,11 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) modelBuilder.Entity().HasData(seeder.Authors); modelBuilder.Entity().HasData(seeder.Books); + modelBuilder.Entity().HasData(seeder.Publishers); } public DbSet Authors { get; set; } public DbSet Books { get; set; } + public DbSet Publishers { get; set; } } } diff --git a/exercise.webapi/Data/Seeder.cs b/exercise.webapi/Data/Seeder.cs index 955e3c8..9678292 100644 --- a/exercise.webapi/Data/Seeder.cs +++ b/exercise.webapi/Data/Seeder.cs @@ -79,13 +79,14 @@ public class Seeder private List _authors = new List(); private List _books = new List(); + private List _publishers = new List(); public Seeder() { Random authorRandom = new Random(); Random bookRandom = new Random(); - + Random publisherRandom = new Random(); for (int x = 1; x < 250; x++) @@ -98,6 +99,16 @@ public Seeder() _authors.Add(author); } + for (int l = 1; l < 250; l++) + { + Publisher publisher = new Publisher(); + publisher.Id = l; + publisher.FirstName = _firstnames[publisherRandom.Next(_firstnames.Count)]; + publisher.LastName = _lastnames[publisherRandom.Next(_lastnames.Count)]; + + _publishers.Add(publisher); + } + for (int y = 1; y < 250; y++) { @@ -105,7 +116,9 @@ public Seeder() book.Id = y; book.Title = $"{_firstword[bookRandom.Next(_firstword.Count)]} {_secondword[bookRandom.Next(_secondword.Count)]} {_thirdword[bookRandom.Next(_thirdword.Count)]}"; book.AuthorId = _authors[authorRandom.Next(_authors.Count)].Id; - //book.Author = authors[book.AuthorId-1]; + book.PublisherId = _publishers[publisherRandom.Next(_publishers.Count)].Id; + //book.Author = _authors[book.AuthorId-1]; + _books.Add(book); } @@ -113,5 +126,6 @@ public Seeder() } public List Authors { get { return _authors; } } public List Books { get { return _books; } } + public List Publishers { get { return _publishers; } } } } diff --git a/exercise.webapi/Endpoints/AuthorApi.cs b/exercise.webapi/Endpoints/AuthorApi.cs new file mode 100644 index 0000000..9a59c35 --- /dev/null +++ b/exercise.webapi/Endpoints/AuthorApi.cs @@ -0,0 +1,94 @@ + +using System.Reflection.Metadata; +using System.Runtime.CompilerServices; +using exercise.webapi.DTO; +using exercise.webapi.Models; +using exercise.webapi.Repository; +using Microsoft.AspNetCore.Components.Authorization; +using Microsoft.AspNetCore.Routing.Constraints; +using static System.Reflection.Metadata.BlobBuilder; +namespace exercise.webapi.Endpoints +{ + public static class AuthorApi + { + public static void ConfigueAuthorEnpoints(this WebApplication app) + { + app.MapGet("/authors", GetAuthors); + app.MapGet("/authors/{id}", GetAuthor); + } + + private static async Task GetAuthors(IBookRepository bookRepository) + { + var authors = new Payload>() + { + Data = new List() + }; + + var results = await bookRepository.GetAllAuthor(); + var books = await bookRepository.GetAllBooks(); + foreach (Author author in results) + { + AuthorDTO authorDTO = new AuthorDTO(); + + authorDTO.FirstName = author.FirstName; + authorDTO.LastName = author.LastName; + authorDTO.Email = author.Email; + authorDTO.Id = author.Id; + authorDTO.books = new List(); + + + var authorBooks = books.Where(b => b.AuthorId == author.Id).ToList(); + + foreach(Book book in authorBooks) + { + BookDTONoAuthor dto = new BookDTONoAuthor(); + PublisherNoBooks publisherDTO = new PublisherNoBooks(); + publisherDTO.Id = book.Publisher.Id; + publisherDTO.FirstName = book.Publisher.FirstName; + publisherDTO.LastName = book.Publisher.LastName; + dto.title = book.Title; + + dto.id = book.Id; + dto.publisher = publisherDTO; + + + authorDTO.books.Add(dto); + } + + authors.Data.Add(authorDTO); + } + return TypedResults.Ok(authors); + } + private static async TaskGetAuthor(IBookRepository bookRepository, int id) + { + var payload = new Payload(); + + Author author = await bookRepository.GetAuthor(id); + var books = await bookRepository.GetAllBooks(); + AuthorDTO authorDTO = new AuthorDTO(); + authorDTO.Email = author.Email; + authorDTO.FirstName = author.FirstName; + authorDTO.LastName = author.LastName; + authorDTO.Id = author.Id; + authorDTO.books = new List(); + var authorBooks = books.Where(x => x.AuthorId == author.Id).ToList(); + foreach(Book book in authorBooks) + { + PublisherNoBooks publisherDTO = new PublisherNoBooks(); + publisherDTO.FirstName = book.Publisher.FirstName; + publisherDTO.Id = book.Publisher.Id; + publisherDTO.LastName = book.Publisher.LastName; + BookDTONoAuthor dto = new BookDTONoAuthor(); + dto.title = book.Title; + dto.id = book.Id; + dto.publisher = publisherDTO; + authorDTO.books.Add(dto); + } + + payload.Data = authorDTO; + + return TypedResults.Ok(payload); + + } + } +} diff --git a/exercise.webapi/Endpoints/BookApi.cs b/exercise.webapi/Endpoints/BookApi.cs index 6758215..7b66c49 100644 --- a/exercise.webapi/Endpoints/BookApi.cs +++ b/exercise.webapi/Endpoints/BookApi.cs @@ -1,5 +1,9 @@ -using exercise.webapi.Models; +using System.Reflection.Metadata; +using System.Runtime.CompilerServices; +using exercise.webapi.DTO; +using exercise.webapi.Models; using exercise.webapi.Repository; +using Microsoft.AspNetCore.Components.Authorization; using static System.Reflection.Metadata.BlobBuilder; namespace exercise.webapi.Endpoints @@ -9,12 +13,173 @@ public static class BookApi public static void ConfigureBooksApi(this WebApplication app) { app.MapGet("/books", GetBooks); + app.MapGet("books/{id}", GetSingleBook); + app.MapPost("/books/{id}", UpdateBook); + app.MapDelete("/books/{id}", DeleteBook); + app.MapPost("/books", CreateBook); } private static async Task GetBooks(IBookRepository bookRepository) { - var books = await bookRepository.GetAllBooks(); + var books = new Payload>() + { + Data = new List() + }; + + var results = await bookRepository.GetAllBooks(); + foreach (Book book in results) + { + + BookDTO bookDTO = new BookDTO(); + + //author + AuthorDTOnoBooks authorDTO = new AuthorDTOnoBooks(); + authorDTO.FirstName = book.Author.FirstName; + authorDTO.LastName = book.Author.LastName; + authorDTO.Email = book.Author.Email; + authorDTO.id = book.Author.Id; + + //publisher + PublisherNoBooks publisherDTO = new PublisherNoBooks(); + publisherDTO.FirstName = book.Publisher.FirstName; + publisherDTO.LastName = book.Publisher.LastName; + publisherDTO.Id = book.Publisher.Id; + + + + + //bookDTO + bookDTO.title = book.Title; + bookDTO.id = book.Id; + + bookDTO.author = authorDTO; + + bookDTO.publisher = publisherDTO; + + books.Data.Add(bookDTO); + + } return TypedResults.Ok(books); } + + private static async Task GetSingleBook(IBookRepository bookRepository, int id) + { + var book = new Payload(); + + Book result = await bookRepository.GetBook(id); + + BookDTO bookDTO = new BookDTO(); + + //author + AuthorDTOnoBooks authorDTO = new AuthorDTOnoBooks(); + bookDTO.title = result.Title; + bookDTO.id = result.Id; + + authorDTO.FirstName = result.Author.FirstName; + authorDTO.LastName = result.Author.LastName; + authorDTO.Email = result.Author.Email; + authorDTO.id = result.Author.Id; + + //publisher + + PublisherNoBooks publisherDTO = new PublisherNoBooks(); + publisherDTO.FirstName = result.Publisher.FirstName; + publisherDTO.LastName = result.Publisher.LastName; + publisherDTO.Id = result.Publisher.Id; + bookDTO.publisher = publisherDTO; + bookDTO.author = authorDTO; + book.Data = bookDTO; + return TypedResults.Ok(book); + + } + + private static async Task UpdateBook(IBookRepository bookRepository, int id, int author_id) + { + var booker = new Payload(); + Book book = await bookRepository.UpdateBook(id, author_id); + + BookDTO bookDTO = new BookDTO(); + AuthorDTOnoBooks authorDTO = new AuthorDTOnoBooks(); + bookDTO.title = book.Title; + bookDTO.id = book.Id; + + //author + authorDTO.FirstName = book.Author.FirstName; + authorDTO.LastName = book.Author.LastName; + authorDTO.Email = book.Author.Email; + authorDTO.id = book.Author.Id; + + //publisher + PublisherNoBooks publisherDTO = new PublisherNoBooks(); + publisherDTO.FirstName = book.Publisher.FirstName; + publisherDTO.LastName = book.Publisher.LastName; + publisherDTO.Id = book.Publisher.Id; + + bookDTO.publisher = publisherDTO; + bookDTO.author = authorDTO; + booker.Data = bookDTO; + return TypedResults.Ok(booker); + + + } + + private static async Task DeleteBook(IBookRepository bookRepository, int id) + { + Book bookToDelete = await bookRepository.GetBook(id); + + BookDTO bookDTO = new BookDTO(); + + bookDTO.id = bookToDelete.Id; + bookDTO.title = bookToDelete.Title; + + //author + AuthorDTOnoBooks authorDTO = new AuthorDTOnoBooks(); + authorDTO.FirstName = bookToDelete.Author.FirstName; + authorDTO.LastName = bookToDelete.Author.LastName; + authorDTO.id = bookToDelete.Author.Id; + authorDTO.Email = bookToDelete.Author.Email; + + //publisher + PublisherNoBooks publisherDTO = new PublisherNoBooks(); + publisherDTO.FirstName = bookToDelete.Publisher.FirstName; + publisherDTO.LastName = bookToDelete.Publisher.LastName; + publisherDTO.Id = bookToDelete.Publisher.Id; + + bookDTO.publisher = publisherDTO; + bookDTO.author = authorDTO; + + await bookRepository.DeleteBook(id); + + return TypedResults.Ok(bookDTO); + } + + private static async Task CreateBook(IBookRepository bookRepository, string title, int author_id, int publisher_id) + { + Book book = await bookRepository.CreateBook(title, author_id, publisher_id); + Payloadpayload = new Payload(); + BookDTO bookDTO = new BookDTO(); + + bookDTO.id = book.Id; + bookDTO.title = book.Title; + + AuthorDTOnoBooks authorDTO = new AuthorDTOnoBooks(); + authorDTO.FirstName = book.Author.FirstName; + authorDTO.LastName = book.Author.LastName; + authorDTO.id = book.Author.Id; + authorDTO.Email = book.Author.Email; + + PublisherNoBooks publisherDTO = new PublisherNoBooks(); + publisherDTO.FirstName = book.Publisher.FirstName; + publisherDTO.LastName = book.Publisher.LastName; + publisherDTO.Id = book.Publisher.Id; + + bookDTO.publisher = publisherDTO; + bookDTO.author = authorDTO; + payload.Data = bookDTO; + return TypedResults.Ok(payload); + + } + + } -} +} \ No newline at end of file diff --git a/exercise.webapi/Endpoints/PublisherApi.cs b/exercise.webapi/Endpoints/PublisherApi.cs new file mode 100644 index 0000000..bcb9eff --- /dev/null +++ b/exercise.webapi/Endpoints/PublisherApi.cs @@ -0,0 +1,93 @@ +using exercise.webapi.DTO; +using exercise.webapi.Models; +using exercise.webapi.Repository; + +namespace exercise.webapi.Endpoints +{ + public static class PublisherApi + { + public static void ConfigurePublisherApi(this WebApplication app) { + app.MapGet("/publishers", GetPublishers); + app.MapGet("/publishers/{id}", GetPublisher); + } + + private static async Task GetPublishers(IBookRepository bookRepository) + { + var publishers = new Payload>() + { + Data = new List() + }; + + var results = await bookRepository.GetAllPublishers(); + var books = await bookRepository.GetAllBooks(); + foreach (Publisher publisher in results) + { + PublisherDTO publisherDTO = new PublisherDTO(); + + publisherDTO.FirstName = publisher.FirstName; + publisherDTO.LastName = publisher.LastName; + + publisherDTO.Id = publisher.Id; + publisherDTO.Books = new List(); + + + var publisherBooks = books.Where(b => b.PublisherId == publisher.Id).ToList(); + + foreach (Book book in publisherBooks) + { + AuthorDTOnoBooks dtoAuthor = new AuthorDTOnoBooks(); + dtoAuthor.id = book.Author.Id; + dtoAuthor.FirstName = book.Author.FirstName; + dtoAuthor.LastName = book.Author.LastName; + dtoAuthor.Email = book.Author.Email; + BookDTONoPublisher dto = new BookDTONoPublisher(); + dto.title = book.Title; + dto.author = dtoAuthor; + dto.id = book.Id; + + + + publisherDTO.Books.Add(dto); + } + + publishers.Data.Add(publisherDTO); + } + return TypedResults.Ok(publishers); + } + + private static async Task GetPublisher(IBookRepository bookRepository ,int id) + { + var payload = new Payload(); + + Publisher publisher = await bookRepository.GetPublisher(id); + var books = await bookRepository.GetAllBooks(); + PublisherDTO publisherDTO = new PublisherDTO(); + + publisherDTO.FirstName = publisher.FirstName; + publisherDTO.LastName = publisher.LastName; + publisherDTO.Id = publisher.Id; + publisherDTO.Books = new List(); + var publisherBooks = books.Where(x => x.Publisher.Id == publisher.Id).ToList(); + foreach (Book book in publisherBooks) + { + AuthorDTOnoBooks authorDTO = new AuthorDTOnoBooks(); + authorDTO.FirstName = book.Author.FirstName; + authorDTO.id = book.Author.Id; + authorDTO.LastName = book.Author.LastName; + authorDTO.Email = book.Author.Email; + BookDTONoPublisher dto = new BookDTONoPublisher(); + dto.title = book.Title; + dto.id = book.Id; + dto.author = authorDTO; + publisherDTO.Books.Add(dto); + } + + payload.Data = publisherDTO; + + return TypedResults.Ok(payload); + + } + + + } +} diff --git a/exercise.webapi/Models/Author.cs b/exercise.webapi/Models/Author.cs index 9f47878..14d9eb0 100644 --- a/exercise.webapi/Models/Author.cs +++ b/exercise.webapi/Models/Author.cs @@ -9,7 +9,6 @@ public class Author public string LastName { get; set; } public string Email { get; set; } - [JsonIgnore] // Todo: replace this with DTO approach - public ICollection Books { get; set; } = new List(); + public ICollection? Books { get; set; } = new List(); } } diff --git a/exercise.webapi/Models/AuthorPublisher.cs b/exercise.webapi/Models/AuthorPublisher.cs new file mode 100644 index 0000000..ffcc396 --- /dev/null +++ b/exercise.webapi/Models/AuthorPublisher.cs @@ -0,0 +1,12 @@ +namespace exercise.webapi.Models +{ + public class AuthorPublisher + { + public int Id { get; set; } + public int PublisherId { get; set; } + public int AuthorId { get; set; } + public Author author { get; set; } + public Publisher publisher { get; set; } + + } +} diff --git a/exercise.webapi/Models/Book.cs b/exercise.webapi/Models/Book.cs index 9534929..e95ea10 100644 --- a/exercise.webapi/Models/Book.cs +++ b/exercise.webapi/Models/Book.cs @@ -1,4 +1,5 @@ using System.ComponentModel.DataAnnotations.Schema; +using exercise.webapi.DTO; namespace exercise.webapi.Models { @@ -6,8 +7,12 @@ public class Book { public int Id { get; set; } public string Title { get; set; } + public int AuthorId { get; set; } + public int PublisherId { get; set; } public Author Author { get; set; } + public Publisher Publisher { get; set; } + public AuthorPublisher? AuthorPublisher { get; set; } } } diff --git a/exercise.webapi/Models/Publisher.cs b/exercise.webapi/Models/Publisher.cs new file mode 100644 index 0000000..57087d7 --- /dev/null +++ b/exercise.webapi/Models/Publisher.cs @@ -0,0 +1,10 @@ +namespace exercise.webapi.Models +{ + public class Publisher + { + public int Id { get; set; } + public string FirstName { get; set; } + public string LastName { get; set; } + public List Books { get; set; } = new List(); + } +} diff --git a/exercise.webapi/Program.cs b/exercise.webapi/Program.cs index 43dec56..e30cfb6 100644 --- a/exercise.webapi/Program.cs +++ b/exercise.webapi/Program.cs @@ -28,5 +28,7 @@ } app.UseHttpsRedirection(); +app.ConfigurePublisherApi(); +app.ConfigueAuthorEnpoints(); app.ConfigureBooksApi(); app.Run(); diff --git a/exercise.webapi/Repository/BookRepository.cs b/exercise.webapi/Repository/BookRepository.cs index 1f5e64a..97b592e 100644 --- a/exercise.webapi/Repository/BookRepository.cs +++ b/exercise.webapi/Repository/BookRepository.cs @@ -1,10 +1,11 @@ using exercise.webapi.Data; using exercise.webapi.Models; using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.ChangeTracking.Internal; namespace exercise.webapi.Repository { - public class BookRepository: IBookRepository + public class BookRepository : IBookRepository { DataContext _db; @@ -13,10 +14,104 @@ public BookRepository(DataContext db) _db = db; } + public async Task> GetAllAuthor() + { + return await _db.Authors.ToListAsync(); + } + public async Task> GetAllBooks() { - return await _db.Books.Include(b => b.Author).ToListAsync(); + return await _db.Books.Include(x => x.AuthorPublisher).Include(y => y.Publisher).Include(i => i.Author).ToListAsync(); } + public async Task> GetAllPublishers() + { + return await _db.Publishers.ToListAsync(); + } + public async Task GetBook(int id) + { + List books = await _db.Books.Include(b => b.AuthorPublisher).Include(x => x.Publisher).Include(y => y.Author).ToListAsync(); + Book book = books.FirstOrDefault(x => x.Id == id)!; + if (book != null) return book; + return null; + } + + public async TaskGetAuthor(int id) + { + List authors = await _db.Authors.ToListAsync(); + Author author = authors.FirstOrDefault(x => x.Id == id)!; + if (author != null) return author; + return null; + } + + public async Task GetPublisher(int id) + { + + List publishers = await _db.Publishers.ToListAsync(); + Publisher publisher = publishers.FirstOrDefault(x => x.Id == id)!; + if (publisher != null) return publisher; + return null; + } + + public async Task UpdateBook(int id,int author_id) + { + Book book = await GetBook(id); + + Author author = await GetAuthor(author_id); + + + if (book != null && author != null) + { + book.Author.Books.Remove(book); + author.Books.Add(book); + book.Author = author; + book.AuthorId = author.Id; + _db.Books.Update(book); + _db.SaveChanges(); + return book; + } + + + return null; + } + + public async TaskDeleteBook(int id) + { + Book book = await GetBook(id); + Book temp = book; + Author author = await GetAuthor(book.AuthorId); + Publisher publisher = await GetPublisher(book.PublisherId); + if (book != null) + { + _db.Books.Remove(book); + author.Books.Remove(book); + publisher.Books.Remove(book); + _db.SaveChanges(); + return temp; + } + return null; + } + + public async Task CreateBook(string title, int author_id, int publisher_id) + { + Author author = await GetAuthor(author_id); + Publisher publisher = await GetPublisher(publisher_id); + AuthorPublisher authorPublisher = new AuthorPublisher() { publisher = publisher , PublisherId = publisher.Id, author = author, AuthorId = author.Id}; + if (author != null) + { + Book book = new Book() { Title = title, AuthorId = author_id, PublisherId = publisher_id, Author = author, Publisher = publisher, AuthorPublisher = authorPublisher }; + _db.Books.Add(book); + author.Books.Add(book); + publisher.Books.Add(book); + _db.SaveChanges(); + return book; + } + return null; + } + + + + + } } diff --git a/exercise.webapi/Repository/IBookRepository.cs b/exercise.webapi/Repository/IBookRepository.cs index f860016..00dd01b 100644 --- a/exercise.webapi/Repository/IBookRepository.cs +++ b/exercise.webapi/Repository/IBookRepository.cs @@ -1,9 +1,19 @@ using exercise.webapi.Models; +using Microsoft.AspNetCore.Mvc.ModelBinding; namespace exercise.webapi.Repository { public interface IBookRepository { public Task> GetAllBooks(); + public TaskGetBook(int id); + public Task UpdateBook(int id, int author_id); + public Task> GetAllAuthor(); + public Task GetAuthor(int id); + public Task DeleteBook(int id); + public Task CreateBook(string title, int author_id, int publisher_id); + public Task> GetAllPublishers(); + public Task GetPublisher(int id); + } }