Skip to content

Commit 6b31664

Browse files
authored
Merge branch 'OpenShock:develop' into fix/dotnet-10-openapi-2
2 parents 217122f + acd0de6 commit 6b31664

File tree

4 files changed

+105
-10
lines changed

4 files changed

+105
-10
lines changed

API/Controller/Shares/UserShares/Invites.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,13 @@ public async Task<IActionResult> RedeemInvite([FromRoute] Guid inviteId, [FromSe
122122
.FirstOrDefaultAsync(x => x.Id == inviteId && (x.RecipientUserId == null || x.RecipientUserId == CurrentUser.Id));
123123

124124
if (shareRequest is null) return Problem(ShareError.ShareRequestNotFound);
125-
125+
126+
var isShareOwner = shareRequest.OwnerId == CurrentUser.Id;
127+
if (isShareOwner)
128+
{
129+
return Problem(ShareError.ShareRequestCreateCannotShareWithSelf);
130+
}
131+
126132
var alreadySharedShockers = await _db.UserShares.Where(x => x.Shocker.Device.Owner.Id == shareRequest.OwnerId && x.SharedWithUserId == CurrentUser.Id).ToListAsync();
127133

128134
foreach (var shareInvitationShocker in shareRequest.ShockerMappings)

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+
}

Dev/testData.sql

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
11
-- Insert test user with password OpenShock123!
22

3-
INSERT INTO public.users (id, name, email, password_hash, email_activated, roles)
3+
INSERT INTO public.users (id, name, email, password_hash, activated_at, roles, created_at)
44
VALUES ('50e14f43-dd4e-412f-864d-78943ea28d91',
55
'OpenShock-Test',
66
'test@openshock.org',
77
'bcrypt:$2a$11$bCkcqpsNgFt1.DB33OuLhOsqVbDUp.BIvKVOIYvEO8Hyf26fV6B4y', --- OpenShock123!
8-
true,
9-
ARRAY['admin']::role_type[]);
8+
now(),
9+
ARRAY['admin']::role_type[],
10+
now());
1011

11-
INSERT INTO public.devices (id, owner, name, token)
12+
INSERT INTO public.devices (id, owner_id, name, token, created_at)
1213
VALUES ('7472cba2-6037-488f-b5aa-53b1c39fe450',
1314
'50e14f43-dd4e-412f-864d-78943ea28d91',
1415
'Test Hub',
15-
'ro6DglfhzM@hH1*P5&TOBsY4ipLMSEI4CbY!yNit4V%W&nO*Z9N@H$JzO$mh3D2PvpKL7Sde#6azOs7lBCQq0CovcCg#pX*m&Gt^4S$gCDP@f8eBPB8*q^q*dgdECXKRro6DglfhzM@hH1*P5&TOBsY4ipLMSEI4CbY!yNit4V%W&nO*Z9N@H$JzO$mh3D2PvpKL7Sde#6azOs7lBCQq0CovcCg#pX*m&Gt^4S$gCDP@f8eBPB8*q^q*dgdECXKR');
16+
'ro6DglfhzM@hH1*P5&TOBsY4ipLMSEI4CbY!yNit4V%W&nO*Z9N@H$JzO$mh3D2PvpKL7Sde#6azOs7lBCQq0CovcCg#pX*m&Gt^4S$gCDP@f8eBPB8*q^q*dgdECXKRro6DglfhzM@hH1*P5&TOBsY4ipLMSEI4CbY!yNit4V%W&nO*Z9N@H$JzO$mh3D2PvpKL7Sde#6azOs7lBCQq0CovcCg#pX*m&Gt^4S$gCDP@f8eBPB8*q^q*dgdECXKR',
17+
now());
1618

1719

18-
INSERT INTO public.shockers (id, name, rf_id, device, model)
20+
INSERT INTO public.shockers (id, name, rf_id, device_id, model, is_paused, created_at)
1921
VALUES ('f73b3d99-44f4-4fbc-9e23-17a310202b07',
2022
'Test Shocker',
21-
'12345',
23+
12345,
2224
'7472cba2-6037-488f-b5aa-53b1c39fe450',
23-
'caiXianlin'::shocker_model_type);
25+
'caiXianlin'::shocker_model_type,
26+
false,
27+
now());

0 commit comments

Comments
 (0)