Skip to content

Steven Phung #100

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; }
}
}
12 changes: 12 additions & 0 deletions exercise.webapi/DTO/AuthorListDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace exercise.webapi.DTO
{
public class AuthorListDTO
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public List<BookListDTO> BookList { get; set; } = new List<BookListDTO>();

}
}
13 changes: 13 additions & 0 deletions exercise.webapi/DTO/BookDTO.cs
Original file line number Diff line number Diff line change
@@ -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 int AuthorId { get; set; }
public AuthorDTO Author { get; set; }
}
}
8 changes: 8 additions & 0 deletions exercise.webapi/DTO/BookListDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace exercise.webapi.DTO
{
public class BookListDTO
{
public int Id { get; set; }
public string Title { get; set; }
}
}
8 changes: 8 additions & 0 deletions exercise.webapi/DTO/BookPost.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace exercise.webapi.DTO
{
public class BookPost
{
public string Title { get; set; }
public int AuthorId { get; set; }
}
}
8 changes: 8 additions & 0 deletions exercise.webapi/DTO/BookPut.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace exercise.webapi.DTO
{
public class BookPut
{
public string? Title { get; set; }
public int? AuthorId { get; set; }
}
}
66 changes: 66 additions & 0 deletions exercise.webapi/Endpoints/AuthorApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
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)
{
var authors = app.MapGroup("authors");
authors.MapGet("/", GetAuthors);
authors.MapGet("/{id}", GetAuthor);
}


#region Get authors
private static async Task<IResult> GetAuthors(IAuthorRepository authorRepository)
{
//Payload<List<Book>> payload = new Payload<List<Book>>();


var results = await authorRepository.GetAllAuthors();
var resultsDTO = results.Select(a => new AuthorListDTO
{
Id = a.Id,
FirstName = a.FirstName,
LastName = a.LastName,
Email = a.Email,
BookList = a.Books.Select(b => new BookListDTO
{
Id = b.Id,
Title = b.Title,
}).ToList()
});

return TypedResults.Ok(resultsDTO);
}

public static async Task<IResult> GetAuthor(IAuthorRepository authorRepository, int id)
{
var author = await authorRepository.GetAuthor(id);
if (author == null)
{
return Results.NotFound();
}
var authorDTO = new AuthorListDTO
{
Id = author.Id,
FirstName = author.FirstName,
LastName = author.LastName,
Email = author.Email,
BookList = author.Books.Select(b => new BookListDTO
{
Id = b.Id,
Title = b.Title,

}).ToList()
};
return Results.Ok(authorDTO);

}
#endregion

}
}
142 changes: 138 additions & 4 deletions exercise.webapi/Endpoints/BookApi.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using exercise.webapi.Models;
using System;
using exercise.webapi.DTO;
using exercise.webapi.Models;
using exercise.webapi.Repository;
using static System.Reflection.Metadata.BlobBuilder;

Expand All @@ -8,13 +10,145 @@ 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.MapDelete("/{id}", DeleteBook);
books.MapPut("/{id}", UpdateBook);
}

#region Get books
private static async Task<IResult> GetBooks(IBookRepository bookRepository)
{
var books = await bookRepository.GetAllBooks();
return TypedResults.Ok(books);
//Payload<List<Book>> payload = new Payload<List<Book>>();


var results = await bookRepository.GetAllBooks();
var resultsDTO = results.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
}
});

return TypedResults.Ok(resultsDTO);
}

public static async Task<IResult> GetBook(IBookRepository bookRepository, int id)
{
var book = await bookRepository.GetBook(id);
if (book == null)
{
return Results.NotFound();
}
var bookDTO = 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 Results.Ok(bookDTO);

}
#endregion

#region Add book
public static async Task<IResult> AddBook(IBookRepository bookRepository,IAuthorRepository authorRepository, BookPost model)
{
try
{
var author = await authorRepository.GetAuthor(model.AuthorId);
Book book = new Book
{
Title = model.Title,
AuthorId = model.AuthorId
};


await bookRepository.AddBook(book);

BookDTO book2 = new BookDTO
{
Id = book.Id,
Title = model.Title,
AuthorId = model.AuthorId,
Author = new AuthorDTO
{
Id = author.Id,
FirstName = author.FirstName,
LastName = author.LastName,
Email = author.Email
}
};

return TypedResults.Ok(book2);
}

catch (Exception e)
{
return TypedResults.Problem(e.Message);
}

}
#endregion

#region Delete book
public static async Task<IResult> DeleteBook(IBookRepository bookRepository, int id)
{
try
{
var book = await bookRepository.GetBook(id);
var result = await bookRepository.DeleteBook(id);
if (result)
{
return Results.Ok(new { When = DateTime.Now, Status = "Deleted", Title = book.Title, AuthorId = book.AuthorId });
}
return Results.NotFound();
}
catch (Exception e)
{
return TypedResults.Problem(e.Message);
}

}
#endregion

#region Update book
public static async Task<IResult> UpdateBook(IBookRepository bookRepository, IAuthorRepository authorRepository, int id, BookPut model)
{
try
{
var book = await bookRepository.GetBook(id);
if (book == null)
{
return Results.NotFound();
}
if (model.Title != null) book.Title = model.Title;
if (model.AuthorId != null) book.AuthorId = model.AuthorId.Value;
await bookRepository.UpdateBook(id, book);
return Results.Ok(book);
}
catch (Exception e)
{
return TypedResults.Problem(e.Message);
}
}
#endregion

}
}
4 changes: 1 addition & 3 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>();
public List<Book> Books { get; set; } = new List<Book>();
}
}
1 change: 1 addition & 0 deletions exercise.webapi/Models/Book.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations.Schema;
using exercise.webapi.DTO;

namespace exercise.webapi.Models
{
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.ConfigureAuthorApi();
app.Run();
29 changes: 29 additions & 0 deletions exercise.webapi/Repository/AuthorRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using exercise.webapi.Data;
using exercise.webapi.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Internal;

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(b => b.Books).FirstAsync(b => b.Id == id);
}
}
}
34 changes: 34 additions & 0 deletions exercise.webapi/Repository/BookRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,44 @@ public BookRepository(DataContext db)
_db = db;
}

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

public async Task<bool> DeleteBook(int id)
{
var target = await _db.Books.FindAsync(id);
_db.Books.Remove(target);
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).FirstAsync(b => b.Id == id);
}

public async Task<Book> UpdateBook(int id, Book book)
{
var existingBook = _db.Books.Find(id);
if (existingBook != null)
{
existingBook.Title = book.Title;
existingBook.AuthorId = book.AuthorId;
_db.Books.Update(book);
_db.SaveChanges();
}
return book;

}
}
}
Loading