diff --git a/API/Controller/Shockers/GetShockerLogs.cs b/API/Controller/Shockers/GetShockerLogs.cs index a490ddda..c4a246fa 100644 --- a/API/Controller/Shockers/GetShockerLogs.cs +++ b/API/Controller/Shockers/GetShockerLogs.cs @@ -28,7 +28,7 @@ public sealed partial class ShockerController [ProducesResponseType>(StatusCodes.Status200OK, MediaTypeNames.Application.Json)] [ProducesResponseType(StatusCodes.Status404NotFound, MediaTypeNames.Application.ProblemJson)] // ShockerNotFound [MapToApiVersion("1")] - public async Task GetShockerLogs([FromRoute] Guid shockerId, [FromQuery(Name="offset")] uint offset = 0, + public async Task GetShockerLogs([FromRoute] Guid shockerId, [FromQuery(Name = "offset")] uint offset = 0, [FromQuery, Range(1, 500)] uint limit = 100) { var exists = await _db.Shockers.AnyAsync(x => x.Device.OwnerId == CurrentUser.Id && x.Id == shockerId); @@ -66,4 +66,60 @@ public async Task GetShockerLogs([FromRoute] Guid shockerId, [Fro return LegacyDataOk(logs); } + + + /// + /// Get the logs for all shockers + /// + /// + /// + /// The logs + /// Shocker does not exist + [HttpGet("logs")] + [EnableRateLimiting("shocker-logs")] + [ProducesResponseType(StatusCodes.Status200OK, MediaTypeNames.Application.Json)] + [ProducesResponseType(StatusCodes.Status404NotFound, MediaTypeNames.Application.ProblemJson)] + [MapToApiVersion("1")] + public async Task GetAllShockerLogs([FromQuery(Name = "offset")] uint offset = 0, + [FromQuery, Range(1, 500)] uint limit = 100) + { + var logs = await _db.ShockerControlLogs + .Where(x => x.Shocker.Device.OwnerId == CurrentUser.Id) + .OrderByDescending(x => x.CreatedAt) + .Skip((int)offset) + .Take((int)limit) + .Select(x => new LogEntryWithHub + { + Id = x.Id, + HubId = x.Shocker.Device.Id, + HubName = x.Shocker.Device.Name, + ShockerId = x.Shocker.Id, + ShockerName = x.Shocker.Name, + Duration = x.Duration, + Intensity = x.Intensity, + Type = x.Type, + CreatedOn = x.CreatedAt, + ControlledBy = x.ControlledByUser == null + ? new ControlLogSenderLight + { + Id = Guid.Empty, + Name = "Guest", + Image = GravatarUtils.GuestImageUrl, + CustomName = x.CustomName + } + : new ControlLogSenderLight + { + Id = x.ControlledByUser.Id, + Name = x.ControlledByUser.Name, + Image = x.ControlledByUser.GetImageUrl(), + CustomName = x.CustomName + } + }) + .ToListAsync(); + + return Ok(new ShockerLogsResponse + { + Logs = logs + }); + } } \ No newline at end of file diff --git a/API/Models/Response/LogEntryWithHub.cs b/API/Models/Response/LogEntryWithHub.cs new file mode 100644 index 00000000..4eb22469 --- /dev/null +++ b/API/Models/Response/LogEntryWithHub.cs @@ -0,0 +1,29 @@ +using OpenShock.Common.Models; + +namespace OpenShock.API.Models.Response; + +public sealed class ShockerLogsResponse +{ + public required ICollection Logs { get; init; } +} + +public sealed class LogEntryWithHub +{ + public required Guid Id { get; init; } + + public required Guid HubId { get; init; } + public required string HubName { get; init; } + + public required Guid ShockerId { get; init; } + public required string ShockerName { get; init; } + + public required DateTime CreatedOn { get; init; } + + public required ControlType Type { get; init; } + + public required ControlLogSenderLight ControlledBy { get; init; } + + public required byte Intensity { get; init; } + + public required uint Duration { get; init; } +} \ No newline at end of file