Skip to content

Abdi Mahamoud #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions exercise.webapi/DTO/AuthorDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace exercise.webapi.DTO
{
public class AuthorDTO
{
public int Id { get; set; }
public string Name { get; set; }
public List<string> BookTitles { get; set; } = new List<string>();

}
}
11 changes: 11 additions & 0 deletions exercise.webapi/DTO/BookDTO.cs
Original file line number Diff line number Diff line change
@@ -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; }

}
}
57 changes: 57 additions & 0 deletions exercise.webapi/Endpoints/AuthorApi.cs
Original file line number Diff line number Diff line change
@@ -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<IResult> 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<IResult> 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);

}
}
}
92 changes: 90 additions & 2 deletions exercise.webapi/Endpoints/BookApi.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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<IResult> 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<IResult> 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<IResult> 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<IResult> 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<IResult> 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);




}




}
}
2 changes: 2 additions & 0 deletions exercise.webapi/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
builder.Services.AddSwaggerGen();
builder.Services.AddDbContext<DataContext>(opt => opt.UseInMemoryDatabase("Library"));
builder.Services.AddScoped<IBookRepository, BookRepository>();
builder.Services.AddScoped<IAuthorRepository, AuthorRepository>();

var app = builder.Build();

Expand All @@ -29,4 +30,5 @@

app.UseHttpsRedirection();
app.ConfigureBooksApi();
app.ConfigureAuthorsApi();
app.Run();
27 changes: 27 additions & 0 deletions exercise.webapi/Repository/AuthorRepository.cs
Original file line number Diff line number Diff line change
@@ -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<IEnumerable<Author>> GetAllAuthors()
{
return await _db.Authors.Include(a => a.Books).ToListAsync();

}

public async Task<Author> GetAuthor(int id)
{
return await _db.Authors.Include(a => a.Books).FirstOrDefaultAsync(a => a.Id == id); // Henter boka basert på ID
}
}
}
63 changes: 63 additions & 0 deletions exercise.webapi/Repository/BookRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,73 @@ public BookRepository(DataContext db)
_db = db;
}

public async Task<Book> 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<bool> 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<IEnumerable<Book>> GetAllBooks()
{
return await _db.Books.Include(b => b.Author).ToListAsync();

}

public async Task<Book> 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<Book> 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;

}

}
}
11 changes: 11 additions & 0 deletions exercise.webapi/Repository/IAuthorRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using exercise.webapi.Models;

namespace exercise.webapi.Repository
{
public interface IAuthorRepository
{
public Task<IEnumerable<Author>> GetAllAuthors();

public Task<Author> GetAuthor(int id);
}
}
13 changes: 13 additions & 0 deletions exercise.webapi/Repository/IBookRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,18 @@ namespace exercise.webapi.Repository
public interface IBookRepository
{
public Task<IEnumerable<Book>> GetAllBooks();

public Task <Book> GetBook(int id);

public Task<Book> UpdateBook(int id,string firstname,string lastname);

public Task<bool> DeleteBook(int id);

public Task<Book> CreateBook(string title,int authorid);



}


}