From c919538d3ca45b9ab5cf656c71deb8f2f4b30846 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 22 Sep 2024 17:10:19 -0500 Subject: add Get --- src/slskd/Events/EventService.cs | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/slskd/Events/EventService.cs b/src/slskd/Events/EventService.cs index 85f82238..7f09010f 100644 --- a/src/slskd/Events/EventService.cs +++ b/src/slskd/Events/EventService.cs @@ -15,13 +15,15 @@ // along with this program. If not, see https://www.gnu.org/licenses/. // +namespace slskd.Events; + using System; +using System.Collections.Generic; using System.Linq; +using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using Serilog; -namespace slskd.Events; - /// /// Manages events. /// @@ -39,6 +41,39 @@ public class EventService private IDbContextFactory ContextFactory { get; } private ILogger Log { get; } = Serilog.Log.ForContext(); + /// + /// Gets a paginated list of events. + /// + /// The beginning offset for the page. + /// The page size limit. + /// The retrieved list. + /// Thrown when the specified is less than zero. + /// Thrown when the specified is zero. + public virtual async Task> Get(int offset = 0, int limit = 100) + { + if (offset < 0) + { + throw new ArgumentOutOfRangeException(nameof(offset), "Offset must be greater than or equal to zero"); + } + + if (limit <= 0) + { + throw new ArgumentOutOfRangeException(nameof(limit), "Limit must be greater than zero"); + } + + try + { + using var context = ContextFactory.CreateDbContext(); + var events = context.Events.Skip(offset).Take(limit).ToList(); + return events; + } + catch (Exception ex) + { + Log.Error(ex, "Failed to get event records with options {Options}: {Message}", new { offset, limit }, ex.Message); + throw; + } + } + /// /// Adds the specified event . /// -- cgit v1.2.3