Skip to content

Commit acd0de6

Browse files
authored
feat: add v1 endpoint /shockers/logs (#260)
* feat: add v2 endpoint /shockers/logs * fix: use v1 api, "Hub" * fix: use ShockerLogsResponse
1 parent 999229d commit acd0de6

File tree

2 files changed

+86
-1
lines changed

2 files changed

+86
-1
lines changed

API/Controller/Shockers/GetShockerLogs.cs

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public sealed partial class ShockerController
2828
[ProducesResponseType<LegacyDataResponse<LogEntry[]>>(StatusCodes.Status200OK, MediaTypeNames.Application.Json)]
2929
[ProducesResponseType<OpenShockProblem>(StatusCodes.Status404NotFound, MediaTypeNames.Application.ProblemJson)] // ShockerNotFound
3030
[MapToApiVersion("1")]
31-
public async Task<IActionResult> GetShockerLogs([FromRoute] Guid shockerId, [FromQuery(Name="offset")] uint offset = 0,
31+
public async Task<IActionResult> GetShockerLogs([FromRoute] Guid shockerId, [FromQuery(Name = "offset")] uint offset = 0,
3232
[FromQuery, Range(1, 500)] uint limit = 100)
3333
{
3434
var exists = await _db.Shockers.AnyAsync(x => x.Device.OwnerId == CurrentUser.Id && x.Id == shockerId);
@@ -66,4 +66,60 @@ public async Task<IActionResult> GetShockerLogs([FromRoute] Guid shockerId, [Fro
6666

6767
return LegacyDataOk(logs);
6868
}
69+
70+
71+
/// <summary>
72+
/// Get the logs for all shockers
73+
/// </summary>
74+
/// <param name="offset"></param>
75+
/// <param name="limit"></param>
76+
/// <response code="200">The logs</response>
77+
/// <response code="404">Shocker does not exist</response>
78+
[HttpGet("logs")]
79+
[EnableRateLimiting("shocker-logs")]
80+
[ProducesResponseType<ShockerLogsResponse>(StatusCodes.Status200OK, MediaTypeNames.Application.Json)]
81+
[ProducesResponseType<OpenShockProblem>(StatusCodes.Status404NotFound, MediaTypeNames.Application.ProblemJson)]
82+
[MapToApiVersion("1")]
83+
public async Task<IActionResult> GetAllShockerLogs([FromQuery(Name = "offset")] uint offset = 0,
84+
[FromQuery, Range(1, 500)] uint limit = 100)
85+
{
86+
var logs = await _db.ShockerControlLogs
87+
.Where(x => x.Shocker.Device.OwnerId == CurrentUser.Id)
88+
.OrderByDescending(x => x.CreatedAt)
89+
.Skip((int)offset)
90+
.Take((int)limit)
91+
.Select(x => new LogEntryWithHub
92+
{
93+
Id = x.Id,
94+
HubId = x.Shocker.Device.Id,
95+
HubName = x.Shocker.Device.Name,
96+
ShockerId = x.Shocker.Id,
97+
ShockerName = x.Shocker.Name,
98+
Duration = x.Duration,
99+
Intensity = x.Intensity,
100+
Type = x.Type,
101+
CreatedOn = x.CreatedAt,
102+
ControlledBy = x.ControlledByUser == null
103+
? new ControlLogSenderLight
104+
{
105+
Id = Guid.Empty,
106+
Name = "Guest",
107+
Image = GravatarUtils.GuestImageUrl,
108+
CustomName = x.CustomName
109+
}
110+
: new ControlLogSenderLight
111+
{
112+
Id = x.ControlledByUser.Id,
113+
Name = x.ControlledByUser.Name,
114+
Image = x.ControlledByUser.GetImageUrl(),
115+
CustomName = x.CustomName
116+
}
117+
})
118+
.ToListAsync();
119+
120+
return Ok(new ShockerLogsResponse
121+
{
122+
Logs = logs
123+
});
124+
}
69125
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using OpenShock.Common.Models;
2+
3+
namespace OpenShock.API.Models.Response;
4+
5+
public sealed class ShockerLogsResponse
6+
{
7+
public required ICollection<LogEntryWithHub> Logs { get; init; }
8+
}
9+
10+
public sealed class LogEntryWithHub
11+
{
12+
public required Guid Id { get; init; }
13+
14+
public required Guid HubId { get; init; }
15+
public required string HubName { get; init; }
16+
17+
public required Guid ShockerId { get; init; }
18+
public required string ShockerName { get; init; }
19+
20+
public required DateTime CreatedOn { get; init; }
21+
22+
public required ControlType Type { get; init; }
23+
24+
public required ControlLogSenderLight ControlledBy { get; init; }
25+
26+
public required byte Intensity { get; init; }
27+
28+
public required uint Duration { get; init; }
29+
}

0 commit comments

Comments
 (0)