Skip to content

Espen Solhaug #96

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
11 changes: 11 additions & 0 deletions exercise.webapi/DTO/AuthorDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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 List<MinimalBookDTO> Books { get; set; }
}
}
15 changes: 15 additions & 0 deletions exercise.webapi/DTO/BookDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using exercise.webapi.Models;

namespace exercise.webapi.DTO
{
public class BookDTO
{
public int Id { get; set; }
public string Title { get; set; }
public int AuthorId { get; set; }
public MinimalAuthorDTO Author { get; set; }


}
}

10 changes: 10 additions & 0 deletions exercise.webapi/DTO/MinimalAuthorDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace exercise.webapi.DTO
{
public class MinimalAuthorDTO
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
}
}
8 changes: 8 additions & 0 deletions exercise.webapi/DTO/MinimalBookDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace exercise.webapi.DTO
{
public class MinimalBookDTO
{
public int Id { get; set; }
public string Title { get; set; }
}
}
8 changes: 8 additions & 0 deletions exercise.webapi/DTO/PostBook.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace exercise.webapi.DTO
{
public class PostBook
{
public string Title { get; set; }
public int AuthorId { get; set; }
}
}
8 changes: 8 additions & 0 deletions exercise.webapi/DTO/PutBook.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace exercise.webapi.DTO
{
public class PutBook
{
public string? Title { get; set; }
public int? AuthorId { get; set; }
}
}
52 changes: 52 additions & 0 deletions exercise.webapi/Endpoints/AuthorAPI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using exercise.webapi.DTO;
using exercise.webapi.Models;
using exercise.webapi.Repository;

namespace exercise.webapi.Endpoints
{
public static class AuthorAPI
{
public static void ConfigureAuthorAPI(this WebApplication app)
{
app.MapGet("/authors", GetAuthors);
app.MapGet("/authors/{id}", Get);
}

private static async Task<IResult> GetAuthors(IAuthorRepository authorRepository)
{
var authors = await authorRepository.GetAllAuthors();
List<AuthorDTO> authorsDTOList = authors.Select(authors => new AuthorDTO
{
Id = authors.Id,
FirstName = authors.FirstName,
LastName = authors.LastName,
Email = authors.Email,
Books = authors.Books.Select(books => new MinimalBookDTO
{
Id = books.Id,
Title = books.Title,
}).ToList()
}).ToList();
return TypedResults.Ok(authorsDTOList);
}

private static async Task<IResult> Get(IAuthorRepository authorRepository, int id)
{
var author = await authorRepository.Get(id);
if (author == null) { return TypedResults.NotFound("Author Not Found"); }
var AuthorDTO = new AuthorDTO
{
Id = author.Id,
FirstName = author.FirstName,
LastName = author.LastName,
Email = author.Email,
Books = author.Books.Select(book => new MinimalBookDTO
{
Id = book.Id,
Title = book.Title,
}).ToList()
};
return TypedResults.Ok(AuthorDTO);
}
}
}
136 changes: 134 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,143 @@ public static class BookApi
public static void ConfigureBooksApi(this WebApplication app)
{
app.MapGet("/books", GetBooks);
app.MapGet("/books/{id}", Get);
app.MapPost("/books", Create);
app.MapPut("/books/{id}", Put);
app.MapDelete("/books/{id}", Delete);

}

private static async Task<IResult> GetBooks(IBookRepository bookRepository)
{
var books = await bookRepository.GetAllBooks();
return TypedResults.Ok(books);
List<BookDTO> bookDTOList = books.Select(book => new BookDTO
{
Id = book.Id,
Title = book.Title,
AuthorId = book.AuthorId,
Author = new MinimalAuthorDTO
{
Id = book.Author.Id,
FirstName = book.Author.FirstName,
LastName = book.Author.LastName,
Email = book.Author.Email
}
}).ToList();
return TypedResults.Ok(bookDTOList);
}


private static async Task<IResult> Get(IBookRepository bookRepository, int id)
{
var book = await bookRepository.Get(id);
if (book == null) { return TypedResults.NotFound("Book Not Found"); }
var bookDTO = new BookDTO
{
Id = book.Id,
Title = book.Title,
AuthorId = book.AuthorId,
Author = new MinimalAuthorDTO
{
Id = book.Author.Id,
FirstName = book.Author.FirstName,
LastName = book.Author.LastName,
Email = book.Author.Email
}
};
return TypedResults.Ok(bookDTO);
}

private static async Task<IResult> Create(IBookRepository bookRepository, IAuthorRepository authorRepository, PostBook book)
{
if(String.IsNullOrEmpty(book.Title))
{
return TypedResults.BadRequest();
}
Author authorFound = await authorRepository.Get(book.AuthorId);
if(authorFound == null) { return TypedResults.NotFound(); }

Book newBook = new Book
{
Title = book.Title,
AuthorId = book.AuthorId,

};
bookRepository.Create(newBook);
return TypedResults.Ok(new BookDTO
{
Id = newBook.Id,
Title = newBook.Title,
AuthorId = newBook.AuthorId,
Author = new MinimalAuthorDTO
{
Id = authorFound.Id,
FirstName = authorFound.FirstName,
LastName = authorFound.LastName,
Email = authorFound.Email
}
});


}

private static async Task<IResult> Put(IBookRepository bookRepository, IAuthorRepository authorRepository, int id, PutBook book)
{
Author foundAuthor = null;
Book bookToBeUpdated = null;
if(book.AuthorId.HasValue) {
foundAuthor = await authorRepository.Get(book.AuthorId.Value);
}
if (String.IsNullOrEmpty(book.Title) && !book.AuthorId.HasValue)
{
return TypedResults.BadRequest("Empty payload");
}
if(foundAuthor == null && book.AuthorId.HasValue) { return TypedResults.NotFound("Author not found"); };
bookToBeUpdated = bookRepository.Get(id).Result;
if (bookToBeUpdated == null)
{
return TypedResults.NotFound("Book not found");
}
bookToBeUpdated.Title = !string.IsNullOrEmpty(book.Title) ? book.Title : bookToBeUpdated.Title;
if (foundAuthor != null)
{
bookToBeUpdated.AuthorId = (int)book.AuthorId;
bookToBeUpdated.Author = foundAuthor;
}
bookRepository.Update(bookToBeUpdated);
return TypedResults.Ok(new BookDTO
{
Id = bookToBeUpdated.Id,
Title = bookToBeUpdated.Title,
AuthorId = bookToBeUpdated.AuthorId,
Author = new MinimalAuthorDTO
{
Id = bookToBeUpdated.Author.Id,
FirstName = bookToBeUpdated.Author.FirstName,
LastName = bookToBeUpdated.Author.LastName,
Email = bookToBeUpdated.Author.Email
}
});

}

private static async Task<IResult> Delete(IBookRepository bookRepository, IAuthorRepository authorRepository, int id)
{
Book book = bookRepository.Get(id).Result;
bool isDeleted = await bookRepository.Delete(book);
return TypedResults.Ok(new BookDTO
{
Id = book.Id,
Title = book.Title,
AuthorId = book.AuthorId,
Author = new MinimalAuthorDTO
{
Id = book.Author.Id,
FirstName = book.Author.FirstName,
LastName = book.Author.LastName,
Email = book.Author.Email
}
});
}
}
}
2 changes: 0 additions & 2 deletions exercise.webapi/Models/Author.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ public class Author
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }

[JsonIgnore] // Todo: replace this with DTO approach
public ICollection<Book> Books { get; set; } = new List<Book>();
}
}
2 changes: 1 addition & 1 deletion exercise.webapi/Models/Book.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ public class Book

public int AuthorId { get; set; }
public Author Author { get; set; }
}
}
}
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.ConfigureAuthorAPI();
app.Run();
24 changes: 24 additions & 0 deletions exercise.webapi/Repository/AuthorRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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(b => b.Books).ToListAsync();
}
public async Task<Author> Get(int id)
{
return await _db.Authors.Include(b => b.Books).FirstOrDefaultAsync(x => x.Id == id);
}
}
}
27 changes: 27 additions & 0 deletions exercise.webapi/Repository/BookRepository.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using exercise.webapi.Data;
using exercise.webapi.DTO;
using exercise.webapi.Models;
using Microsoft.EntityFrameworkCore;

Expand All @@ -18,5 +19,31 @@ public async Task<IEnumerable<Book>> GetAllBooks()
return await _db.Books.Include(b => b.Author).ToListAsync();

}

public async Task<Book> Get(int id)
{
return await _db.Books.Include(b => b.Author).FirstOrDefaultAsync(x => x.Id == id);
}

public async Task<Book> Create(Book book)
{
_db.Books.Add(book);
await _db.SaveChangesAsync();
return book;
}

public async Task<bool> Update(Book book)
{
_db.Update(book);
_db.SaveChangesAsync();
return true;
}

public async Task<bool> Delete(Book book)
{
_db.Books.Remove(book);
_db.SaveChangesAsync();
return true;
}
}
}
10 changes: 10 additions & 0 deletions exercise.webapi/Repository/IAuthorRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using exercise.webapi.Models;

namespace exercise.webapi.Repository
{
public interface IAuthorRepository
{
public Task<IEnumerable<Author>> GetAllAuthors();
public Task<Author> Get(int id);
}
}
4 changes: 4 additions & 0 deletions exercise.webapi/Repository/IBookRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@ namespace exercise.webapi.Repository
public interface IBookRepository
{
public Task<IEnumerable<Book>> GetAllBooks();
public Task<Book> Get(int id);
public Task<Book> Create(Book book);
public Task<bool> Update(Book book);
public Task<bool> Delete(Book book);
}
}