Skip to content

Axel Ahlander #108

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 3 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
13 changes: 13 additions & 0 deletions exercise.webapi/DTO/AuthorDTO.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 AuthorDTO
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public List<BookDTONoAuthor>books { get; set; }
}
}
10 changes: 10 additions & 0 deletions exercise.webapi/DTO/AuthorDTOnoBooks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace exercise.webapi.DTO
{
public class AuthorDTOnoBooks
{
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/AuthorPublisherDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace exercise.webapi.DTO
{
public class AuthorPublisherDTO
{
public int Id { get; set; }
public int PublisherId { get; set; }
public int AuthorId { get; set; }
public AuthorDTOnoBooks author { get; set; }
public PublisherDTO publisher { get; set; }
}
}
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 AuthorDTOnoBooks author { get; set; }
public PublisherNoBooks publisher { get; set; }
}
}
9 changes: 9 additions & 0 deletions exercise.webapi/DTO/BookDTONoAuthor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace exercise.webapi.DTO
{
public class BookDTONoAuthor
{
public int id { get; set; }
public string title { get; set; }
public PublisherNoBooks publisher { get; set; }
}
}
9 changes: 9 additions & 0 deletions exercise.webapi/DTO/BookDTONoPublisher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace exercise.webapi.DTO
{
public class BookDTONoPublisher
{
public int id { get; set; }
public string title { get; set; }
public AuthorDTOnoBooks author { get; set; }
}
}
9 changes: 9 additions & 0 deletions exercise.webapi/DTO/Payload.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using exercise.webapi.Models;

namespace exercise.webapi.DTO
{
public class Payload<T> where T : class
{
public T Data {get;set;}
}
}
10 changes: 10 additions & 0 deletions exercise.webapi/DTO/PublisherDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace exercise.webapi.DTO
{
public class PublisherDTO
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public List<BookDTONoPublisher> Books { get; set; }
}
}
9 changes: 9 additions & 0 deletions exercise.webapi/DTO/PublisherNoBooks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace exercise.webapi.DTO
{
public class PublisherNoBooks
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
}
2 changes: 2 additions & 0 deletions exercise.webapi/Data/DataContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)

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

}
public DbSet<Author> Authors { get; set; }
public DbSet<Book> Books { get; set; }
public DbSet<Publisher> Publishers { get; set; }
}
}
18 changes: 16 additions & 2 deletions exercise.webapi/Data/Seeder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,14 @@ public class Seeder

private List<Author> _authors = new List<Author>();
private List<Book> _books = new List<Book>();
private List<Publisher> _publishers = new List<Publisher>();

public Seeder()
{

Random authorRandom = new Random();
Random bookRandom = new Random();

Random publisherRandom = new Random();


for (int x = 1; x < 250; x++)
Expand All @@ -98,20 +99,33 @@ public Seeder()
_authors.Add(author);
}

for (int l = 1; l < 250; l++)
{
Publisher publisher = new Publisher();
publisher.Id = l;
publisher.FirstName = _firstnames[publisherRandom.Next(_firstnames.Count)];
publisher.LastName = _lastnames[publisherRandom.Next(_lastnames.Count)];

_publishers.Add(publisher);
}


for (int y = 1; y < 250; y++)
{
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.PublisherId = _publishers[publisherRandom.Next(_publishers.Count)].Id;
//book.Author = _authors[book.AuthorId-1];

_books.Add(book);
}


}
public List<Author> Authors { get { return _authors; } }
public List<Book> Books { get { return _books; } }
public List<Publisher> Publishers { get { return _publishers; } }
}
}
94 changes: 94 additions & 0 deletions exercise.webapi/Endpoints/AuthorApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@

using System.Reflection.Metadata;
using System.Runtime.CompilerServices;
using exercise.webapi.DTO;
using exercise.webapi.Models;
using exercise.webapi.Repository;
using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Routing.Constraints;
using static System.Reflection.Metadata.BlobBuilder;
namespace exercise.webapi.Endpoints
{
public static class AuthorApi
{
public static void ConfigueAuthorEnpoints(this WebApplication app)
{
app.MapGet("/authors", GetAuthors);
app.MapGet("/authors/{id}", GetAuthor);
}

private static async Task<IResult> GetAuthors(IBookRepository bookRepository)
{
var authors = new Payload<List<AuthorDTO>>()
{
Data = new List<AuthorDTO>()
};

var results = await bookRepository.GetAllAuthor();
var books = await bookRepository.GetAllBooks();
foreach (Author author in results)
{
AuthorDTO authorDTO = new AuthorDTO();

authorDTO.FirstName = author.FirstName;
authorDTO.LastName = author.LastName;
authorDTO.Email = author.Email;
authorDTO.Id = author.Id;
authorDTO.books = new List<BookDTONoAuthor>();


var authorBooks = books.Where(b => b.AuthorId == author.Id).ToList();

foreach(Book book in authorBooks)
{
BookDTONoAuthor dto = new BookDTONoAuthor();
PublisherNoBooks publisherDTO = new PublisherNoBooks();
publisherDTO.Id = book.Publisher.Id;
publisherDTO.FirstName = book.Publisher.FirstName;
publisherDTO.LastName = book.Publisher.LastName;
dto.title = book.Title;

dto.id = book.Id;
dto.publisher = publisherDTO;


authorDTO.books.Add(dto);
}

authors.Data.Add(authorDTO);
}
return TypedResults.Ok(authors);
}
private static async Task<IResult>GetAuthor(IBookRepository bookRepository, int id)
{
var payload = new Payload<AuthorDTO>();

Author author = await bookRepository.GetAuthor(id);
var books = await bookRepository.GetAllBooks();
AuthorDTO authorDTO = new AuthorDTO();
authorDTO.Email = author.Email;
authorDTO.FirstName = author.FirstName;
authorDTO.LastName = author.LastName;
authorDTO.Id = author.Id;
authorDTO.books = new List<BookDTONoAuthor>();
var authorBooks = books.Where(x => x.AuthorId == author.Id).ToList();
foreach(Book book in authorBooks)
{
PublisherNoBooks publisherDTO = new PublisherNoBooks();
publisherDTO.FirstName = book.Publisher.FirstName;
publisherDTO.Id = book.Publisher.Id;
publisherDTO.LastName = book.Publisher.LastName;
BookDTONoAuthor dto = new BookDTONoAuthor();
dto.title = book.Title;
dto.id = book.Id;
dto.publisher = publisherDTO;
authorDTO.books.Add(dto);
}

payload.Data = authorDTO;

return TypedResults.Ok(payload);

}
}
}
Loading