Skip to content

Andreas Conradi Nitschke #102

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 6 commits 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
9 changes: 7 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -367,5 +367,10 @@ FodyWeavers.xsd
*/**/obj/Debug
*/**/obj/Release
*/Migrations
/exercise.wwwapi/appsettings.json
/exercise.wwwapi/appsettings.Development.json
/exercise.webapi/appsettings.json
/exercise.webapi/appsettings.Development.json

*/**/appsettings.json
*/**/appsettings.Development.json
exercise.webapi/appsettings.json
exercise.webapi/appsettings.Development.json
18 changes: 18 additions & 0 deletions exercise.webapi/DTO/AuthorDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using exercise.webapi.Models;

namespace exercise.webapi.DTO
{
public class AuthorDto
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }

public AuthorDto(Author author)
{
FirstName = author.FirstName;
LastName = author.LastName;
Email = author.Email;
}
}
}
19 changes: 19 additions & 0 deletions exercise.webapi/DTO/AuthorResponseDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using exercise.webapi.Models;

namespace exercise.webapi.DTO
{
public class AuthorResponseDto
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public List<BookPublisherDto> Books { get; set; }
public AuthorResponseDto(Author author)
{
FirstName = author.FirstName;
LastName = author.LastName;
Email = author.Email;
Books = author.Books.Select(book => new BookPublisherDto(book)).ToList();
}
}
}
15 changes: 15 additions & 0 deletions exercise.webapi/DTO/BookAuthorDto.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 BookAuthorDto
{
public string Title { get; set; }
public string Author { get; set; }
public BookAuthorDto(string title, string author)
{
Title = title;
Author = author;
}
}
}
19 changes: 19 additions & 0 deletions exercise.webapi/DTO/BookDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using exercise.webapi.Models;

namespace exercise.webapi.DTO
{
public class BookDto
{
public string Title { get; set; }
public List<AuthorDto> Authors { get; set; }
public string Publisher { get; set; }

public BookDto(Book book)
{
Title = book.Title;
Authors = book.Authors.Select(a => new AuthorDto(a)).ToList();
Publisher = book.Publisher.Name;
}

}
}
15 changes: 15 additions & 0 deletions exercise.webapi/DTO/BookPublisherDto.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 BookPublisherDto
{
public string Title { get; set; }
public string Publisher { get; set; }
public BookPublisherDto(Book book)
{
Title = book.Title;
Publisher = book.Publisher.Name;
}
}
}
9 changes: 9 additions & 0 deletions exercise.webapi/DTO/BookUpdateDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace exercise.webapi.DTO
{
public class BookUpdateDto
{
public string? Title { get; set; }
//public List<int>? AuthorIds { get; set; }
public int? PublisherId { get; set; }
}
}
18 changes: 18 additions & 0 deletions exercise.webapi/DTO/CheckoutDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using exercise.webapi.Models;

namespace exercise.webapi.DTO
{
public class CheckoutDto
{
public BookDto Book { get; set; }
public DateTime CheckoutDate { get; set; }
public DateTime DueDate { get; set; }

public CheckoutDto(Checkout checkout)
{
Book = new BookDto(checkout.Book);
CheckoutDate = checkout.CheckoutDate;
DueDate = checkout.DueDate;
}
}
}
9 changes: 9 additions & 0 deletions exercise.webapi/DTO/OverdueDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace exercise.webapi.DTO
{
public class OverdueDto
{
public BookDto Book { get; set; }
public DateTime DueDate { get; set; }
public int DaysOverdue { get; set; }
}
}
11 changes: 11 additions & 0 deletions exercise.webapi/DTO/UnavailableDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using exercise.webapi.Models;

namespace exercise.webapi.DTO
{
public class UnavailableDto
{
public BookDto Book { get; set; }
public DateTime AvailableBy { get; set; }

}
}
6 changes: 6 additions & 0 deletions exercise.webapi/Data/DataContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,15 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)

modelBuilder.Entity<Author>().HasData(seeder.Authors);
modelBuilder.Entity<Book>().HasData(seeder.Books);
modelBuilder.Entity<Publisher>().HasData(seeder.Publishers);
modelBuilder.Entity<BookAuthor>().HasData(seeder.BookAuthors);
modelBuilder.Entity<Checkout>().HasData(seeder.Checkouts);

}
public DbSet<Author> Authors { get; set; }
public DbSet<Book> Books { get; set; }
public DbSet<Publisher> Publishers { get; set; }
public DbSet<BookAuthor> BookAuthors { get; set; }
public DbSet<Checkout> Checkouts { get; set; }
}
}
62 changes: 60 additions & 2 deletions exercise.webapi/Data/Seeder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,34 @@ public class Seeder
"Flowers",
"Leopards"
};
private List<string> _publisherStrings = new List<string>()
{
"Penguin",
"Random House",
"Harper Collins",
"Simon & Schuster",
"Macmillan",
"Hachette",
"Pearson",
"Scholastic",
"Bloomsbury",
"Oxford University Press"
};

private List<Author> _authors = new List<Author>();
private List<Book> _books = new List<Book>();
private List<Publisher> _publishers = new List<Publisher>();
private List<BookAuthor> _bookAuthors = new List<BookAuthor>();
private List<Checkout> _checkouts = new List<Checkout>();


public Seeder()
{

Random authorRandom = new Random();
Random bookRandom = new Random();
Random publisherRand = new Random();
//Random bookAuthorRandom = new Random();



Expand All @@ -104,14 +123,53 @@ public Seeder()
Book book = new Book();
book.Id = y;
book.Title = $"{_firstword[bookRandom.Next(_firstword.Count)]} {_secondword[bookRandom.Next(_secondword.Count)]} {_thirdword[bookRandom.Next(_thirdword.Count)]}";
book.AuthorId = _authors[authorRandom.Next(_authors.Count)].Id;
//book.Author = authors[book.AuthorId-1];
//book.Authors = new List<Author> { _authors[authorRandom.Next(_authors.Count)] };
book.PublisherId = publisherRand.Next(1, 11);
_books.Add(book);
}

for(int z = 1; z < 11; z++)
{
Publisher publisher = new Publisher();
publisher.Id = z;
publisher.Name = _publisherStrings[z - 1];
_publishers.Add(publisher);
}

//Randomly assign authors to books, they should have at least one author, but can have more
foreach (var book in _books)
{
int numberOfAuthors = authorRandom.Next(1, 4);
for (int i = 0; i < numberOfAuthors; i++)
{
BookAuthor bookAuthor = new BookAuthor();
bookAuthor.Id = _bookAuthors.Count + 1;
bookAuthor.AuthorId = authorRandom.Next(1, 250);
bookAuthor.BookId = book.Id;
_bookAuthors.Add(bookAuthor);
}
}


//Randomly check some books out. They should all be from various previous dates, some should be overdue, but not most.
Random checkoutRandom = new Random();
for (int i = 1; i < 100; i++)
{
Checkout checkout = new Checkout();
checkout.Id = i;
checkout.BookId = bookRandom.Next(1, 250);
checkout.CheckoutDate = DateTime.Now.AddDays(-checkoutRandom.Next(1, 365));
checkout.DueDate = checkout.CheckoutDate.AddDays(14);
checkout.IsReturned = checkoutRandom.Next(0, 2) == 1 ? true : false;
_checkouts.Add(checkout);
}


}
public List<Author> Authors { get { return _authors; } }
public List<Book> Books { get { return _books; } }
public List<Publisher> Publishers { get { return _publishers; } }
public List<BookAuthor> BookAuthors { get { return _bookAuthors; } }
public List<Checkout> Checkouts { get { return _checkouts; } }
}
}
34 changes: 34 additions & 0 deletions exercise.webapi/Endpoints/AuthorApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using exercise.webapi.DTO;
using exercise.webapi.Repository;

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


private static async Task<IResult> GetAuthor(IAuthorRepository authorRepository,IPublisherRepository publisherRepository, int id)
{
var author = await authorRepository.GetAuthor(id);
if (author == null)
{
return Results.NotFound();
}

return TypedResults.Ok(new AuthorResponseDto(author));
}

private static async Task<IResult> GetAuthors(IAuthorRepository authorRepository)
{
var authors = await authorRepository.GetAuthors();
var authorDtos = authors.Select(author => new AuthorResponseDto(author));
return TypedResults.Ok(authorDtos);
}
}
}
Loading