diff --git a/exercise.webapi/DTO/AuthorDTO.cs b/exercise.webapi/DTO/AuthorDTO.cs new file mode 100644 index 0000000..c5d7893 --- /dev/null +++ b/exercise.webapi/DTO/AuthorDTO.cs @@ -0,0 +1,10 @@ +namespace exercise.webapi.DTO +{ + public class AuthorDTO + { + public int Id { get; set; } + public string Name { get; set; } + public List BookTitles { get; set; } = new List(); + + } +} diff --git a/exercise.webapi/DTO/BookDTO.cs b/exercise.webapi/DTO/BookDTO.cs new file mode 100644 index 0000000..50ec2c3 --- /dev/null +++ b/exercise.webapi/DTO/BookDTO.cs @@ -0,0 +1,11 @@ +namespace exercise.webapi.DTO +{ + public class BookDTO + { + + public int Id { get; set; } + public string Title { get; set; } + public string AuthorName { get; set; } + + } +} diff --git a/exercise.webapi/Endpoints/AuthorApi.cs b/exercise.webapi/Endpoints/AuthorApi.cs new file mode 100644 index 0000000..2901ad9 --- /dev/null +++ b/exercise.webapi/Endpoints/AuthorApi.cs @@ -0,0 +1,57 @@ + +using exercise.webapi.DTO; +using exercise.webapi.Models; +using exercise.webapi.Repository; + + +namespace exercise.webapi.Endpoints +{ + public static class AuthorApi + { + public static void ConfigureAuthorsApi(this WebApplication app) + { + app.MapGet("/authors", GetAuthors); + app.MapGet("/author", GetAuthor); + + + + } + + private static async Task GetAuthor(IAuthorRepository authorRepository, int id) + { + var author = await authorRepository.GetAuthor(id); + if (author == null) + { + return TypedResults.NotFound(new { Message = $"authors with {id} is not found." }); + + } + var authorDto = new AuthorDTO + { + Id = author.Id, + Name = $"{author.FirstName} {author.LastName}", + BookTitles = author.Books.Select(b => b.Title).ToList() + + }; + return TypedResults.Ok(authorDto); + + } + private static async Task GetAuthors(IAuthorRepository authorRepository) + { + var authors = await authorRepository.GetAllAuthors(); + if (authors == null) { + + return TypedResults.NotFound(new { Message = $"authors list is empty." }); + } + + var authorDtos = authors.Select(a => new AuthorDTO + { + Id = a.Id, + Name = $"{a.FirstName} {a.LastName}", + BookTitles = a.Books.Select(b => b.Title).ToList() + }).ToList(); + + return TypedResults.Ok(authorDtos); + + } + } +} diff --git a/exercise.webapi/Endpoints/BookApi.cs b/exercise.webapi/Endpoints/BookApi.cs index 6758215..2a044de 100644 --- a/exercise.webapi/Endpoints/BookApi.cs +++ b/exercise.webapi/Endpoints/BookApi.cs @@ -1,4 +1,5 @@ -using exercise.webapi.Models; +using exercise.webapi.DTO; +using exercise.webapi.Models; using exercise.webapi.Repository; using static System.Reflection.Metadata.BlobBuilder; @@ -9,12 +10,99 @@ public static class BookApi public static void ConfigureBooksApi(this WebApplication app) { app.MapGet("/books", GetBooks); + app.MapGet("/book", GetBook); + app.MapPut("/updateBook", UpdateBook); + app.MapDelete("/deleteBook", DeleteBook); + app.MapPost("/CreateBook", CreateBook); + + + } + + private static async Task CreateBook(IBookRepository bookRepository, string title,int id) + { + Book book = await bookRepository.CreateBook(title ,id); + BookDTO bookDTO = new BookDTO(); + + if (book == null) { + + return TypedResults.NotFound(new { Message = $"author with ID {id} not found." }); + } + + + bookDTO.Title = book.Title; + bookDTO.AuthorName = $"{book.Author.FirstName} {book.Author.LastName}"; + + return TypedResults.Ok(bookDTO); + + + + + + } + + private static async Task DeleteBook(IBookRepository bookRepository, int id) + { + bool check= await bookRepository.DeleteBook(id); + if (!check) { + + return TypedResults.NotFound(new { Message = $"Book with ID {id} not found." }); + + } + return TypedResults.Ok(new { Message = $"Book with ID {id} was succesfully removed!."}); + + } private static async Task GetBooks(IBookRepository bookRepository) { var books = await bookRepository.GetAllBooks(); - return TypedResults.Ok(books); + var bookDtos = books.Select(b => new BookDTO + { + Id = b.Id, + Title = b.Title, + AuthorName = $"{b.Author.FirstName} {b.Author.LastName}" + }); + + return TypedResults.Ok(bookDtos); + } + + private static async Task GetBook(IBookRepository bookRepository, int id) + { + var book = await bookRepository.GetBook(id); + if (book == null) + { + return TypedResults.NotFound(new { Message = $"Book with ID {id} not found." }); + } + BookDTO bookDTO = new BookDTO(); + bookDTO.Id = id; + bookDTO.Title = book.Title; + bookDTO.AuthorName = $"{book.Author.FirstName} {book.Author.LastName}"; + + + return TypedResults.Ok(bookDTO); } + + public static async Task UpdateBook(IBookRepository bookRepository, int id, string firstname, string lastname) + { + Book book = await bookRepository.UpdateBook(id,firstname,lastname); + if (book == null) + { + return TypedResults.NotFound(new { Message = $"Book with ID {id} not found." }); + } + BookDTO bookDTO = new BookDTO(); + bookDTO.Id = book.Id; + bookDTO.Title = book.Title; + bookDTO.AuthorName = $"{book.Author.FirstName} {book.Author.LastName}"; + + return TypedResults.Ok(bookDTO); + + + + + } + + + + } } diff --git a/exercise.webapi/Program.cs b/exercise.webapi/Program.cs index 43dec56..c0ed639 100644 --- a/exercise.webapi/Program.cs +++ b/exercise.webapi/Program.cs @@ -11,6 +11,7 @@ builder.Services.AddSwaggerGen(); builder.Services.AddDbContext(opt => opt.UseInMemoryDatabase("Library")); builder.Services.AddScoped(); +builder.Services.AddScoped(); var app = builder.Build(); @@ -29,4 +30,5 @@ app.UseHttpsRedirection(); app.ConfigureBooksApi(); +app.ConfigureAuthorsApi(); app.Run(); diff --git a/exercise.webapi/Repository/AuthorRepository.cs b/exercise.webapi/Repository/AuthorRepository.cs new file mode 100644 index 0000000..cdda12b --- /dev/null +++ b/exercise.webapi/Repository/AuthorRepository.cs @@ -0,0 +1,27 @@ +using exercise.webapi.Data; +using exercise.webapi.Models; +using Microsoft.EntityFrameworkCore; + +namespace exercise.webapi.Repository +{ + public class AuthorRepository : IAuthorRepository + { + DataContext _db; + public AuthorRepository(DataContext db) + { + _db = db; + + } + + public async Task> GetAllAuthors() + { + return await _db.Authors.Include(a => a.Books).ToListAsync(); + + } + + public async Task GetAuthor(int id) + { + return await _db.Authors.Include(a => a.Books).FirstOrDefaultAsync(a => a.Id == id); // Henter boka basert på ID + } + } +} diff --git a/exercise.webapi/Repository/BookRepository.cs b/exercise.webapi/Repository/BookRepository.cs index 1f5e64a..f04d1fe 100644 --- a/exercise.webapi/Repository/BookRepository.cs +++ b/exercise.webapi/Repository/BookRepository.cs @@ -13,10 +13,73 @@ public BookRepository(DataContext db) _db = db; } + public async Task CreateBook(string title, int authorid) + { + Book book = new Book(); + book.Title = title; + + Author author = await _db.Authors.FirstOrDefaultAsync(b => b.Id == authorid); + + if (author == null) + { + return null; + } + author.Books.Add(book); + + book.Author = author; + + await _db.Books.AddAsync(book); + await _db.SaveChangesAsync(); + + return book; + + + + } + + public async Task DeleteBook(int id) + { + Book book =await _db.Books.Include(b => b.Author).FirstOrDefaultAsync(b => b.Id == id); + + if (book == null) + { + return false; + } + _db.Books.Remove(book); + await _db.SaveChangesAsync(); + + return true; + } + public async Task> GetAllBooks() { return await _db.Books.Include(b => b.Author).ToListAsync(); } + + public async Task GetBook(int id) + { + return await _db.Books + .Include(b => b.Author) // Inkluderer relasjonen til forfatter + .FirstOrDefaultAsync(b => b.Id == id); // Henter boka basert på ID + + } + + public async Task UpdateBook(int id,string firstname,string lastname) + { + Book book = await _db.Books.Include(b=> b.Author).FirstOrDefaultAsync(b => b.Id == id); + if (book == null) + { + return null; + } + book.Author.FirstName = firstname; + book.Author.LastName = lastname; + await _db.SaveChangesAsync(); + + + return book; + + } + } } diff --git a/exercise.webapi/Repository/IAuthorRepository.cs b/exercise.webapi/Repository/IAuthorRepository.cs new file mode 100644 index 0000000..b505c26 --- /dev/null +++ b/exercise.webapi/Repository/IAuthorRepository.cs @@ -0,0 +1,11 @@ +using exercise.webapi.Models; + +namespace exercise.webapi.Repository +{ + public interface IAuthorRepository + { + public Task> GetAllAuthors(); + + public Task GetAuthor(int id); + } +} diff --git a/exercise.webapi/Repository/IBookRepository.cs b/exercise.webapi/Repository/IBookRepository.cs index f860016..12e6f68 100644 --- a/exercise.webapi/Repository/IBookRepository.cs +++ b/exercise.webapi/Repository/IBookRepository.cs @@ -5,5 +5,18 @@ namespace exercise.webapi.Repository public interface IBookRepository { public Task> GetAllBooks(); + + public Task GetBook(int id); + + public Task UpdateBook(int id,string firstname,string lastname); + + public Task DeleteBook(int id); + + public Task CreateBook(string title,int authorid); + + + } + + }