Skip to content

Giar Rehani #114

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; }

}
}
11 changes: 11 additions & 0 deletions exercise.webapi/DTO/AuthorDTONoID.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace exercise.webapi.DTO
{
public class AuthorDTONoId
{
public string FirstName { get; set; }

public string LastName { get; set; }

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

public AuthorDTO author { get; set; }
}
}
10 changes: 10 additions & 0 deletions exercise.webapi/DTO/BookDTONoAuthorID.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace exercise.webapi.DTO
{
public class BookDTONoAuthorID
{
public int Id { get; set; }
public string Title { get; set; }

public AuthorDTONoId author { get; set; }
}
}
202 changes: 199 additions & 3 deletions exercise.webapi/Endpoints/BookApi.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using exercise.webapi.Models;
using exercise.webapi.DTO;
using exercise.webapi.Models;
using exercise.webapi.Repository;
using Microsoft.AspNetCore.Mvc;
using static System.Reflection.Metadata.BlobBuilder;

namespace exercise.webapi.Endpoints
Expand All @@ -9,12 +11,206 @@ public static class BookApi
public static void ConfigureBooksApi(this WebApplication app)
{
app.MapGet("/books", GetBooks);
app.MapGet("/books/{id}", GetBook);
app.MapPost("/books", AddBook);
app.MapPut("/books/{author.id}", UpdateBook);
app.MapGet("/authors", GetAuthors);
app.MapGet("/authors/{id}", GetAuthorById);

}

private static async Task<IResult> GetBooks(IBookRepository bookRepository)
[ProducesResponseType(StatusCodes.Status200OK)]
public static async Task<IResult> GetBooks(IBookRepository bookRepository)
{
var books = await bookRepository.GetAllBooks();
return TypedResults.Ok(books);
List<BookDTO> bookList = new List<BookDTO>();

foreach (var book in books)
{
Author auth = await bookRepository.GetAuthor(book.AuthorId);

BookDTO book1 = new BookDTO
{
Id = book.Id,
Title = book.Title,
author = new AuthorDTO
{
Id = auth.Id,
FirstName = auth.FirstName,
LastName = auth.LastName,
Email = auth.Email
}
};
bookList.Add(book1);

}

return TypedResults.Ok(bookList);
}

[ProducesResponseType(StatusCodes.Status200OK)]
public static async Task<IResult> GetAuthors(IBookRepository bookRepository)
{
var authors = await bookRepository.GetAllAuthors();
List<AuthorDTO> authorList = new List<AuthorDTO>();

foreach (var author in authors)
{
IEnumerable<Book> books = await bookRepository.GetBooksByAuthorId(author.Id);

// Convert book entities to DTOs
List<BookDTO> bookDTOs = books.Select(b => new BookDTO
{
Id = b.Id,
Title = b.Title
}).ToList();

// Create an AuthorDTO with books
AuthorDTO authorDTO = new AuthorDTO
{
Id = author.Id,
FirstName = author.FirstName,
LastName = author.LastName,
Email = author.Email,
};
authorList.Add(authorDTO);
}

return TypedResults.Ok(authorList);
}

[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public static async Task<IResult> GetAuthorById(IBookRepository bookRepository, int authorId)
{
if (authorId <= 0)
{
return TypedResults.BadRequest("Invalid author ID.");
}

var author = await bookRepository.GetAuthor(authorId);

if (author == null)
{
return TypedResults.NotFound($"Author with ID {authorId} not found.");
}

IEnumerable<Book> books = await bookRepository.GetBooksByAuthorId(author.Id);


List<BookDTO> bookDTOs = books.Select(b => new BookDTO
{
Id = b.Id,
Title = b.Title
}).ToList();

AuthorDTO authorDTO = new AuthorDTO
{
Id = author.Id,
FirstName = author.FirstName,
LastName = author.LastName,
Email = author.Email,
};

return TypedResults.Ok(authorDTO);
}



[ProducesResponseType(StatusCodes.Status200OK)]

public static async Task<IResult> GetBook(IBookRepository bookRepository, int id)
{
var book = await bookRepository.GetBook(id);
if (book == null)
{
return Results.NotFound();
}

Author auth = await bookRepository.GetAuthor(book.AuthorId);

BookDTO book1 = new BookDTO
{
Id = book.Id,
Title = book.Title,
author = new AuthorDTO
{
Id = auth.Id,
FirstName = auth.FirstName,
LastName = auth.LastName,
Email = auth.Email
}
};

return TypedResults.Ok(book1);
}

[ProducesResponseType(StatusCodes.Status201Created)]
public static async Task<IResult> UpdateBook(IBookRepository bookRepository, int id, AuthorDTONoId auth)
{
var book = await bookRepository.GetBook(id);

BookDTONoAuthorID book1 = new BookDTONoAuthorID
{
Id = book.Id,
Title = book.Title,
author = new AuthorDTONoId
{
FirstName = auth.FirstName,
LastName = auth.LastName,
Email = auth.Email
}
};

book.Author.FirstName = auth.FirstName;
book.Author.LastName = auth.LastName;
book.Author.Email = auth.Email;

await bookRepository.UpdateBook(book);

return TypedResults.Created($"/books/{book1.Id}", book1);
}

[ProducesResponseType(StatusCodes.Status201Created)]
public static async Task<IResult> AddBook(IBookRepository bookRepository, string title, int author_id)
{
Author auth = await bookRepository.GetAuthor(author_id);

Book book = new Book
{
Title = title,
Author = auth
};

if (book.Author.Id == 0 || auth == null)
{
return TypedResults.BadRequest("Author ID is invalid");
}

if (string.IsNullOrWhiteSpace(title))
{
return TypedResults.BadRequest("Title cannot be empty.");
}


await bookRepository.AddBook(book);

AuthorDTO authDTO = new AuthorDTO {
Id = book.AuthorId,
FirstName = book.Author.FirstName,
LastName = book.Author.LastName,
Email = book.Author.Email
};

BookDTO bookDTO = new BookDTO
{
Id = book.Id,
Title = title,
author = authDTO
};

return TypedResults.Created($"/books/{book.Id}", bookDTO);

}
}
}
6 changes: 3 additions & 3 deletions exercise.webapi/Models/Author.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Text.Json.Serialization;
using exercise.webapi.DTO;
using System.Text.Json.Serialization;

namespace exercise.webapi.Models
{
Expand All @@ -9,7 +10,6 @@ public class Author
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<BookDTO> Books { get; set; } = new List<BookDTO>();
}
}
34 changes: 34 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,38 @@ 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<IEnumerable<Author>> GetAllAuthors()
{
return await _db.Authors.ToListAsync();
}

public async Task<Author> GetAuthor(int id)
{
return await _db.Authors.FirstOrDefaultAsync(a => a.Id == id);
}
public async Task<Book> UpdateBook(Book book)
{
_db.Books.Update(book);
await _db.SaveChangesAsync();
return book;
}

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

public async Task<IEnumerable<Book>> GetBooksByAuthorId(int id)
{
return await _db.Books.Where(b => b.AuthorId == id).ToListAsync();
}

}
}
14 changes: 13 additions & 1 deletion exercise.webapi/Repository/IBookRepository.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
using exercise.webapi.Models;
using exercise.webapi.DTO;
using exercise.webapi.Models;

namespace exercise.webapi.Repository
{
public interface IBookRepository
{
public Task<IEnumerable<Book>> GetAllBooks();
public Task<Book> GetBook(int id);
public Task<IEnumerable<Author>> GetAllAuthors();


public Task<Author> GetAuthor(int id);
public Task<Book> AddBook(Book book);

public Task<Book> UpdateBook(Book book);

public Task<IEnumerable<Book>> GetBooksByAuthorId(int id);

}
}
9 changes: 7 additions & 2 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="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.4" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="7.2.0" />
</ItemGroup>

</Project>