I'm working on a library project, where you should be able to filter books by publisher, category, stars, sites, etc.
I have come up with a solution however it consists of multiple if statements, and I don't find it very scalable if I would like to add more filters in the future.
This is one of the methods in my BookController
which handles it:
public IActionResult Index(int? id, string title, string forlag, int rating)
{
//Search by title given by category id
var list = (from ep in _db.Books
join e in _db.BookCategories
on ep.BookID equals e.BookID
where e.CategoryID == id && ep.Title.Contains($"{title}")
select new Book
{
BookID = ep.BookID,
Title = ep.Title,
Author = ep.Author,
Isbn = ep.Isbn,
Publisher = ep.Publisher,
Sites = ep.Sites,
ReleaseDate = ep.ReleaseDate,
Summary = ep.Summary,
Picture = ep.Picture,
AddedDate = ep.AddedDate,
Stars = ep.Stars,
}).ToList();
var queryID = (from u in _db.Books
join e in _db.BookCategories
on u.BookID equals e.BookID
where e.CategoryID == id
select new Book
{
BookID = u.BookID,
Title = u.Title,
Author = u.Author,
Isbn = u.Isbn,
Publisher = u.Publisher,
Sites = u.Sites,
ReleaseDate = u.ReleaseDate,
Summary = u.Summary,
Picture = u.Picture,
AddedDate = u.AddedDate,
Stars = (from c in _db.bookComments where c.BookID == u.BookID && c.IsApproved == true select c.Stars).Average(),
}).ToList();
var queryID2 = (from u in _db.Books
select new Book
{
BookID = u.BookID,
Title = u.Title,
Author = u.Author,
Isbn = u.Isbn,
Publisher = u.Publisher,
Sites = u.Sites,
ReleaseDate = u.ReleaseDate,
Summary = u.Summary,
Picture = u.Picture,
AddedDate = u.AddedDate,
Stars = (from c in _db.bookComments where c.BookID == u.BookID && c.IsApproved == true select c.Stars).Average(),
}).ToList();
var sortedID = queryID.Where(o => o.Stars >= rating);
var sortedID2 = queryID2.Where(o => o.Stars >= rating);
//Search by title if category id equals 0
var bookList = _db.Books.Where(o => o.Title.Contains($"{title}"));
//Sort by category, publisher & star rating
if (id > 0 && forlag != null && rating > 0)
{
var newListus = sortedID.Where(o => o.Publisher == forlag);
return View(newListus);
}
//Sort by publisher & star rating
if (id == 0 && forlag != null && rating > 0)
{
var peter = sortedID2.Where(o => o.Publisher == forlag);
return View(peter);
}
//Sort by category & publisher
if (id > 0 && forlag != null)
{
var newList = list.Where(o => o.Publisher == forlag);
return View(newList);
}
//Sort by category & star rating
if (id > 0 && rating > 0)
{
return View(sortedID);
}
//Sort by star rating
if (id == 0 && rating > 0)
{
return View(sortedID2);
}
//Sort by publisher
if (id == 0 && forlag != null)
{
var bookListSorted = _db.Books.Where(o => o.Publisher == forlag);
return View(bookListSorted);
}
if (id == 0)
{
return View(bookList);
}
return View(list);
}
Any more scalable and easier solution would be appreciated. Please let me know if further information is needed.
Matteo
CodePudding user response:
You can use LINQ's feature query composition.
I Have defined intermediate anonymous class to handle Book and it's stars.
public IActionResult Index(int? id, string title, string forlag, int rating)
{
// filterout by title and set stars to default value
var books = _db.Books
.Where(b => b.Title.Contains(title))
.Select(b => new { Book = b, Stars = 0.0 });
var isStarsDefined = false;
if (id.HasValue)
{
//Search by title given by category id and assign category's Starts
books =
from b in books
join c in _db.BookCategories on b.Book.BookID equals c.BookID
where c.CategoryID == id
select new { Book = b.Book, Stars = c.Starts };
isStarsDefined = true;
}
// additional filter by publisher
if (forlag != null)
{
books = books.Where(b => b.Book.Publisher == forlag);
}
// well probably for result we need stars
if (!isStarsDefined)
{
books = books.Select(b => new
{
Book = b.Book,
Stars = _db.bookComments
.Where(c => c.BookID == b.Book.BookID && c.IsApproved == true)
.Select(c => c.Stars)
.Average()
})
}
// filter by rating
if (rating > 0)
{
books = books.Where(b => b.Stars >= rating);
}
// make final projection to DTO
var result = books
.Select(b => new Book
{
BookID = b.Book.BookID,
Title = b.Book.Title,
Author = b.Book.Author,
Isbn = b.Book.Isbn,
Publisher = b.Book.Publisher,
Sites = b.Book.Sites,
ReleaseDate = b.Book.ReleaseDate,
Summary = b.Book.Summary,
Picture = b.Book.Picture,
AddedDate = b.Book.AddedDate,
Stars = b.Stars
})
.ToList();
result View(result);
}
Note, that I have written everything from memory and there can be minor compilation issues. Also if you use ToList
in the middle you will fail, everything should work via IQueryable
.