diff options
| -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> |
