Skip to content

Egeland #115

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
12 changes: 12 additions & 0 deletions exercise.webapi/DTO/AuthorDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
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<BookDTO> Books { get; set; }
}
}
10 changes: 10 additions & 0 deletions exercise.webapi/DTO/AuthorPost.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace exercise.webapi.DTO
{
public class AuthorPost
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
}
}
12 changes: 12 additions & 0 deletions exercise.webapi/DTO/BookDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
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 AuthorDTO Author { get; set; }
}
}
10 changes: 10 additions & 0 deletions exercise.webapi/DTO/BookPost.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using exercise.webapi.Models;

namespace exercise.webapi.DTO
{
public class BookPost
{
public string Title { get; set; }
public int AuthorId { get; set; }
}
}
10 changes: 10 additions & 0 deletions exercise.webapi/DTO/BookPut.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using exercise.webapi.Models;

namespace exercise.webapi.DTO
{
public class BookPut
{
public string? Title { get; set; }
public int? AuthorId { get; set; }
}
}
54 changes: 54 additions & 0 deletions exercise.webapi/Endpoints/AuthorApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using exercise.webapi.DTO;
using exercise.webapi.Models;
using exercise.webapi.Repository;
using static System.Reflection.Metadata.BlobBuilder;

namespace exercise.webapi.Endpoints
{
public static class AuthorApi
{
public static void ConfigureAuthorsApi(this WebApplication app)
{
var authors = app.MapGroup("/authors");
authors.MapGet("/", GetAuthors);
authors.MapGet("/{id}", GetAuthor);
}

private static async Task<IResult> GetAuthors(IAuthorRepository repository)
{
var authors = await repository.GetAllAuthors();
List<AuthorDTO> result = authors.Select(a => new AuthorDTO
{
Id = a.Id,
FirstName = a.FirstName,
LastName = a.LastName,
Email = a.Email,
Books = a.Books.Select(b => new BookDTO
{
Id = b.Id,
Title = b.Title,
}).ToList()
}).ToList();
return TypedResults.Ok(result);
}

private static async Task<IResult> GetAuthor(IAuthorRepository repository, int id)
{
var author = await repository.GetAuthor(id);
if (author == null) return TypedResults.NotFound("Not Found");
AuthorDTO result = new AuthorDTO
{
Id = author.Id,
FirstName = author.FirstName,
LastName = author.LastName,
Email = author.Email,
Books = author.Books.Select(b => new BookDTO
{
Id = b.Id,
Title = b.Title,
}).ToList()
};
return TypedResults.Ok(result);
}
}
}
124 changes: 119 additions & 5 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 @@ -8,13 +9,126 @@ public static class BookApi
{
public static void ConfigureBooksApi(this WebApplication app)
{
app.MapGet("/books", GetBooks);
var books = app.MapGroup("/books");
books.MapGet("/", GetBooks);
books.MapGet("/{id}", GetBook);
books.MapPost("/", AddBook);
books.MapPut("/{id}", UpdateBook);
books.MapDelete("/{id}", DeleteBook);
}

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

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

private static async Task<IResult> AddBook(IBookRepository bookRepository, IAuthorRepository authorRepository, BookPost model)
{
if ( model.Title == null || model.AuthorId == null) return TypedResults.BadRequest();
Author author = await authorRepository.GetAuthor(model.AuthorId);
if (author == null) return TypedResults.NotFound("Author not found");

Book book = new Book
{
Title = model.Title,
AuthorId = model.AuthorId,
};
await bookRepository.AddBook(book);

BookDTO result = new BookDTO
{
Id = book.Id,
Title = book.Title,
AuthorId = book.AuthorId,
Author = new AuthorDTO
{
Id = book.Author.Id,
FirstName = book.Author.FirstName,
LastName = book.Author.LastName,
Email = book.Author.Email
}
};
return TypedResults.Ok(result);
}

private static async Task<IResult> UpdateBook(IBookRepository bookRepository, IAuthorRepository authorRepository, int id, BookPut model)
{
if (model.Title == null && model.AuthorId == null) return TypedResults.BadRequest();
Author author = await authorRepository.GetAuthor((int)model.AuthorId);
if (author == null) return TypedResults.NotFound("Author not found");
Book book = await bookRepository.GetBook(id);

book.AuthorId = (int)author.Id;
await bookRepository.UpdateBook(book);

BookDTO result = new BookDTO
{
Id = book.Id,
Title = book.Title,
Author = new AuthorDTO
{
Id = book.Author.Id,
FirstName = book.Author.FirstName,
LastName = book.Author.LastName,
Email = book.Author.Email
}
};
return TypedResults.Ok(result);
}

private static async Task<IResult> DeleteBook(IBookRepository repository, int id)
{
var book = await repository.GetBook(id);
if (book == null) return TypedResults.NotFound("Not Found");
if (!await repository.DeleteBook(id)) return TypedResults.NotFound("Not Found");
BookDTO result = new BookDTO
{
Id = book.Id,
Title = book.Title,
AuthorId = book.AuthorId,
Author = new AuthorDTO
{
Id = book.Author.Id,
FirstName = book.Author.FirstName,
LastName = book.Author.LastName,
Email = book.Author.Email
}
};
return TypedResults.Ok(result);
}

}
}
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>();
}
}
1 change: 0 additions & 1 deletion exercise.webapi/Models/Book.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ public class Book
{
public int Id { get; set; }
public string Title { get; set; }

public int AuthorId { get; set; }
public Author Author { get; set; }
}
Expand Down
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();
28 changes: 28 additions & 0 deletions exercise.webapi/Repository/AuthorRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using exercise.webapi.Data;
using exercise.webapi.DTO;
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);
}

}
}
28 changes: 28 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 @@ -16,7 +17,34 @@ public BookRepository(DataContext db)
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).FirstOrDefaultAsync(b => b.Id == id);
}

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

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

public async Task<bool> DeleteBook(int id)
{
var target = await _db.Books.Include(b => b.Author).FirstOrDefaultAsync(b => b.Id == id);
if (target == null) return false;
_db.Books.Remove(target);
await _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> GetAuthor(int id);
}
}
6 changes: 6 additions & 0 deletions exercise.webapi/Repository/IBookRepository.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
using exercise.webapi.Models;
using exercise.webapi.DTO;

namespace exercise.webapi.Repository
{
public interface IBookRepository
{
public Task<IEnumerable<Book>> GetAllBooks();
public Task<Book> GetBook(int id);
public Task<Book> AddBook(Book book);
public Task<Book> UpdateBook(Book book);
public Task<bool> DeleteBook(int id);

}
}
11 changes: 8 additions & 3 deletions exercise.webapi/exercise.webapi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.1" />
<PackageReference Include="Scalar.AspNetCore" Version="2.0.4" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="9.0.1" />
<PackageReference Include="Scalar.AspNetCore" Version="2.0.11" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.2.0" />
</ItemGroup>

</Project>
Loading