Skip to content

Enock Ladu #111

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 4 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
14 changes: 14 additions & 0 deletions exercise.webapi/DTO/AuthorGetDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Text.Json.Serialization;

namespace exercise.webapi.DTO
{
public class AuthorGetDTO
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public List<BookGetDTO>? Books { get; set; }
public List<PublisherGetDTO>? Publishers { get; set; }
}
}
11 changes: 11 additions & 0 deletions exercise.webapi/DTO/AuthorPostDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace exercise.webapi.DTO
{
public class AuthorPostDTO
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public List<BookPostDTO> Books { get; set; }
}
}
10 changes: 10 additions & 0 deletions exercise.webapi/DTO/AuthorPutDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace exercise.webapi.DTO
{
public class AuthorPutDTO
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public List<BookPutDTO> Books { get; set; }
}
}
18 changes: 18 additions & 0 deletions exercise.webapi/DTO/AuthorReponseDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using exercise.webapi.Models;
using System.Text.Json.Serialization;

namespace exercise.webapi.DTO
{
public class AuthorReponseDTO
{
public DateTime When { get; set; } = DateTime.Now;
public string Status { get; set; } = "";

public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }

public List<BookResponseDTO> Books { get; set; } = new List<BookResponseDTO>();
}
}
19 changes: 19 additions & 0 deletions exercise.webapi/DTO/BookGetDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Text.Json.Serialization;

namespace exercise.webapi.DTO
{
public class BookGetDTO
{
public int Id { get; set; }
public string Title { get; set; }

[JsonIgnore]
public int AuthorId { get; set; }
public AuthorGetDTO? Author { get; set; }

[JsonIgnore]
public int PublisherId { get; set; }

public PublisherGetDTO? Publisher { get; set; }
}
}
11 changes: 11 additions & 0 deletions exercise.webapi/DTO/BookPostDTO.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 BookPostDTO
{
public string Title { get; set; }
public int AuthorId { get; set; }
public int PublisherId { get; set; }
}
}
10 changes: 10 additions & 0 deletions exercise.webapi/DTO/BookPutDTO.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 BookPutDTO
{
public int AuthorId { get; set; }
public int PublisherId { get; set; }
}
}
7 changes: 7 additions & 0 deletions exercise.webapi/DTO/BookResponseDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace exercise.webapi.DTO
{
public class BookResponseDTO
{
public string Title { get; set; }
}
}
12 changes: 12 additions & 0 deletions exercise.webapi/DTO/PublisherGetDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Text.Json.Serialization;

namespace exercise.webapi.DTO
{
public class PublisherGetDTO
{
public int Id { get; set; }
public string Name { get; set; }

public List<BookGetDTO>? Books { 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; }
}
}
134 changes: 60 additions & 74 deletions exercise.webapi/Data/Seeder.cs
Original file line number Diff line number Diff line change
@@ -1,117 +1,103 @@
using exercise.webapi.Models;
using Microsoft.EntityFrameworkCore;

namespace exercise.webapi.Data
{
public class Seeder
{
private List<string> _firstnames = new List<string>()
{
"Audrey",
"Donald",
"Elvis",
"Barack",
"Oprah",
"Jimi",
"Mick",
"Kate",
"Charles",
"Kate"
"Audrey", "Donald", "Elvis", "Barack", "Oprah",
"Jimi", "Mick", "Kate", "Charles", "Kate"
};

private List<string> _lastnames = new List<string>()
{
"Hepburn",
"Trump",
"Presley",
"Obama",
"Winfrey",
"Hendrix",
"Jagger",
"Winslet",
"Windsor",
"Middleton"

"Hepburn", "Trump", "Presley", "Obama", "Winfrey",
"Hendrix", "Jagger", "Winslet", "Windsor", "Middleton"
};

private List<string> _domain = new List<string>()
{
"bbc.co.uk",
"google.com",
"theworld.ca",
"something.com",
"tesla.com",
"nasa.org.us",
"gov.us",
"gov.gr",
"gov.nl",
"gov.ru"
"bbc.co.uk", "google.com", "theworld.ca", "something.com",
"tesla.com", "nasa.org.us", "gov.us", "gov.gr", "gov.nl", "gov.ru"
};

private List<string> _firstword = new List<string>()
{
"The",
"Two",
"Several",
"Fifteen",
"A bunch of",
"An army of",
"A herd of"


"The", "Two", "Several", "Fifteen", "A bunch of", "An army of", "A herd of"
};

private List<string> _secondword = new List<string>()
{
"Orange",
"Purple",
"Large",
"Microscopic",
"Green",
"Transparent",
"Rose Smelling",
"Bitter"
"Orange", "Purple", "Large", "Microscopic", "Green", "Transparent", "Rose Smelling", "Bitter"
};

private List<string> _thirdword = new List<string>()
{
"Buildings",
"Cars",
"Planets",
"Houses",
"Flowers",
"Leopards"
"Buildings", "Cars", "Planets", "Houses", "Flowers", "Leopards"
};

private List<string> _publisherNames = new List<string>()
{
"Penguin Random House", "Simon & Schuster", "HarperCollins",
"Hachette Livre", "Scholastic", "Macmillan Publishers",
"Bloomsbury Publishing", "DK Publishing", "Wiley", "McGraw-Hill Education"
};

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

public Seeder()
{
Random random = new Random();

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



for (int x = 1; x < 250; x++)
// Create Authors
for (int x = 1; x <= 50; x++)
{
Author author = new Author();
author.Id = x;
author.FirstName = _firstnames[authorRandom.Next(_firstnames.Count)];
author.LastName = _lastnames[authorRandom.Next(_lastnames.Count)];
author.Email = $"{author.FirstName}.{author.LastName}@{_domain[authorRandom.Next(_domain.Count)]}".ToLower();
Author author = new Author
{
Id = x,
FirstName = _firstnames[random.Next(_firstnames.Count)],
LastName = _lastnames[random.Next(_lastnames.Count)],
Email = $"{_firstnames[random.Next(_firstnames.Count)]}.{_lastnames[random.Next(_lastnames.Count)]}@{_domain[random.Next(_domain.Count)]}".ToLower()
};
_authors.Add(author);
}

// Create Publishers
for (int x = 1; x <= _publisherNames.Count; x++)
{
Publisher publisher = new Publisher
{
Id = x,
Name = _publisherNames[x - 1]
};
_publishers.Add(publisher);
}

for (int y = 1; y < 250; y++)
// Create Books
for (int x = 1; x <= 50; x++)
{
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];
Author randomAuthor = _authors[random.Next(_authors.Count)];
Publisher randomPublisher = _publishers[random.Next(_publishers.Count)];

Book book = new Book
{
Id = x,
Title = $"{_firstword[random.Next(_firstword.Count)]} {_secondword[random.Next(_secondword.Count)]} {_thirdword[random.Next(_thirdword.Count)]}",
AuthorId = randomAuthor.Id,
PublisherId = randomPublisher.Id
};

_books.Add(book);
}


}
public List<Author> Authors { get { return _authors; } }
public List<Book> Books { get { return _books; } }
public List<Author> Authors => _authors;
public List<Book> Books => _books;
public List<Publisher> Publishers => _publishers;
}

}
68 changes: 68 additions & 0 deletions exercise.webapi/Endpoints/AuthorApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using exercise.webapi.DTO;
using exercise.webapi.Repository;
using Microsoft.AspNetCore.Mvc;

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}", GetSingleAuthor);
}

[ProducesResponseType(StatusCodes.Status200OK)]
public static async Task<IResult> GetAuthors(IAuthorRepository authorRepository)
{
var authors = await authorRepository.GetAllAuthors();

// Handle null or empty list
if (authors == null || !authors.Any())
{
return Results.Ok(new List<AuthorReponseDTO>());
}

var authorResponses = authors.Select(author => new AuthorReponseDTO
{
Id = author.Id,
FirstName = author.FirstName,
LastName = author.LastName,
Email = author.Email,
Status = "Success",
Books = author.Books?.Select(book => new BookResponseDTO
{
Title = book.Title
}).ToList() ?? new List<BookResponseDTO>()
}).ToList();

return Results.Ok(authorResponses);
}

[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public static async Task<IResult> GetSingleAuthor(IAuthorRepository authorRepository, int id)
{
var author = await authorRepository.GetAuthorById(id);
if (author == null)
{
return Results.NotFound();
}
var authorResponse = new AuthorReponseDTO
{
Id = author.Id,
FirstName = author.FirstName,
LastName = author.LastName,
Email = author.Email,
Status = "Success",
Books = author.Books.Select(book => new BookResponseDTO
{
Title = book.Title
}).ToList()
};
return Results.Ok(authorResponse);
}
}
}
Loading