From 47069f5ece85b76685c933cc9c4d11c87c5afb2f Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Thu, 14 Dec 2023 16:31:55 +0000 Subject: [PATCH] fix: amend 5cb61d275d52370279d417b436669374a0835b35 Add missing services --- OliverBooth/Services/IReadingListService.cs | 16 +++++++++++ OliverBooth/Services/ReadingListService.cs | 31 +++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 OliverBooth/Services/IReadingListService.cs create mode 100644 OliverBooth/Services/ReadingListService.cs diff --git a/OliverBooth/Services/IReadingListService.cs b/OliverBooth/Services/IReadingListService.cs new file mode 100644 index 0000000..78cc4f2 --- /dev/null +++ b/OliverBooth/Services/IReadingListService.cs @@ -0,0 +1,16 @@ +using OliverBooth.Data.Web; + +namespace OliverBooth.Services; + +/// +/// Represents a service which fetches books from the reading list. +/// +public interface IReadingListService +{ + /// + /// Gets the books in the reading list with the specified state. + /// + /// The state. + /// A collection of books in the specified state. + IReadOnlyCollection GetBooks(BookState state); +} diff --git a/OliverBooth/Services/ReadingListService.cs b/OliverBooth/Services/ReadingListService.cs new file mode 100644 index 0000000..54da5fb --- /dev/null +++ b/OliverBooth/Services/ReadingListService.cs @@ -0,0 +1,31 @@ +using Microsoft.EntityFrameworkCore; +using OliverBooth.Data.Web; + +namespace OliverBooth.Services; + +internal sealed class ReadingListService : IReadingListService +{ + private readonly IDbContextFactory _dbContextFactory; + + /// + /// Initializes a new instance of the class. + /// + /// The database context factory. + public ReadingListService(IDbContextFactory dbContextFactory) + { + _dbContextFactory = dbContextFactory; + } + + /// + /// Gets the books in the reading list with the specified state. + /// + /// The state. + /// A collection of books in the specified state. + public IReadOnlyCollection GetBooks(BookState state) + { + using WebContext context = _dbContextFactory.CreateDbContext(); + return state == (BookState)(-1) + ? context.Books.ToArray() + : context.Books.Where(b => b.State == state).ToArray(); + } +}