diff options
| author | JP Dillingham <jp@dillingham.me> | 2024-09-22 17:10:19 -0500 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2024-09-22 17:10:19 -0500 |
| commit | c919538d3ca45b9ab5cf656c71deb8f2f4b30846 (patch) | |
| tree | a19749e122e3afdb29d83d4f10ea9b7ae9952840 /src/slskd/Events/EventService.cs | |
| parent | a01994b6c6dd2a798cf2fe23706dacb894c807c1 (diff) | |
| download | slskd-c919538d3ca45b9ab5cf656c71deb8f2f4b30846.tar.gz | |
add Get
Diffstat (limited to 'src/slskd/Events/EventService.cs')
| -rw-r--r-- | src/slskd/Events/EventService.cs | 39 |
1 files changed, 37 insertions, 2 deletions
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/. // </copyright> +namespace slskd.Events; + using System; +using System.Collections.Generic; using System.Linq; +using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using Serilog; -namespace slskd.Events; - /// <summary> /// Manages events. /// </summary> @@ -40,6 +42,39 @@ public class EventService private ILogger Log { get; } = Serilog.Log.ForContext<EventService>(); /// <summary> + /// Gets a paginated list of events. + /// </summary> + /// <param name="offset">The beginning offset for the page.</param> + /// <param name="limit">The page size limit.</param> + /// <returns>The retrieved list.</returns> + /// <exception cref="ArgumentOutOfRangeException">Thrown when the specified <paramref name="offset"/> is less than zero.</exception> + /// <exception cref="ArgumentOutOfRangeException">Thrown when the specified <paramref name="limit"/> is zero.</exception> + public virtual async Task<IReadOnlyCollection<EventRecord>> 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; + } + } + + /// <summary> /// Adds the specified event <paramref name="eventRecord"/>. /// </summary> /// <remarks> |
