Add project files.
This commit is contained in:
85
Books.ConsoleApp/SearchByAuthor.cs
Normal file
85
Books.ConsoleApp/SearchByAuthor.cs
Normal file
@ -0,0 +1,85 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlTypes;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace Books.ConsoleApp
|
||||
{
|
||||
public static class SearchByAuthor
|
||||
{
|
||||
public static IEnumerable<string> MatchBy(
|
||||
IEnumerable<Book> books, string searchCriteria)
|
||||
{
|
||||
return books
|
||||
.ExtractAuthors()
|
||||
.ListAuthorsMatching(searchCriteria);
|
||||
}
|
||||
|
||||
public static IEnumerable<string> SuggestAuthors(
|
||||
IEnumerable<Book> books, int suggestionLength = 5)
|
||||
{
|
||||
return books
|
||||
.ExtractAuthors()
|
||||
.RandomizeOrder()
|
||||
.Take(suggestionLength);
|
||||
}
|
||||
|
||||
public static IEnumerable<Book> ListBooks(IEnumerable<Book> books, string author)
|
||||
{
|
||||
return books
|
||||
.CatalogueByAuthor()
|
||||
.BooksBy(author);
|
||||
}
|
||||
|
||||
public static AuthorCatalogue CatalogueByAuthor(this IEnumerable<Book> books)
|
||||
{
|
||||
return books
|
||||
.ToLookup(b => b.author)
|
||||
// Mark Twain : {The Adventures of Huckleberry Finn},
|
||||
// Shakespeare: {Othello, Hamlet, ...}
|
||||
// ...
|
||||
;
|
||||
}
|
||||
|
||||
|
||||
public static IEnumerable<Book> BooksBy(this AuthorCatalogue catalogue, string author)
|
||||
{
|
||||
return catalogue.Contains(author)
|
||||
? catalogue[author]
|
||||
: Enumerable.Empty<Book>();
|
||||
}
|
||||
|
||||
public static IEnumerable<string> ExtractAuthors(this IEnumerable<Book> books)
|
||||
{
|
||||
return books.Select(b =>
|
||||
{
|
||||
//do stuff here
|
||||
return b.author;
|
||||
|
||||
}).Distinct();
|
||||
}
|
||||
|
||||
public static IEnumerable<string> ListAuthorsMatching(
|
||||
this IEnumerable<string> authors, string searchCriteria)
|
||||
{
|
||||
var partOfNameLower = searchCriteria.ToLower();
|
||||
return authors.Where(a => a.ToLower().Contains(partOfNameLower));
|
||||
}
|
||||
|
||||
private static IOrderedEnumerable<string> RandomizeOrder(this IEnumerable<string> authors)
|
||||
{
|
||||
return authors.OrderBy(_ => Guid.NewGuid());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public class AuthorCatalogue
|
||||
{
|
||||
}
|
||||
|
||||
public class Book
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user