-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathPair.cs
More file actions
43 lines (38 loc) · 1.76 KB
/
Pair.cs
File metadata and controls
43 lines (38 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using OpenShock.Common.Redis;
using Redis.OM;
using System.Net.Mime;
using Asp.Versioning;
using Microsoft.AspNetCore.RateLimiting;
using OpenShock.Common.Errors;
using OpenShock.Common.Problems;
using OpenShock.Common.Models;
namespace OpenShock.API.Controller.Device;
public sealed partial class DeviceController
{
/// <summary>
/// Pair a device with a pair code.
/// </summary>
/// <param name="pairCode">The pair code to pair with.</param>
/// <response code="200">Successfully assigned LCG node</response>
/// <response code="404">No such pair code exists</response>
[AllowAnonymous]
[MapToApiVersion("1")]
[HttpGet("pair/{pairCode}", Name = "Pair")]
[HttpGet("~/{version:apiVersion}/pair/{pairCode}", Name = "Pair_DEPRECATED")] // Backwards compatibility
[EnableRateLimiting("auth")]
[ProducesResponseType<LegacyDataResponse<string>>(StatusCodes.Status200OK, MediaTypeNames.Application.Json)]
[ProducesResponseType<OpenShockProblem>(StatusCodes.Status404NotFound, MediaTypeNames.Application.ProblemJson)] // PairCodeNotFound
public async Task<IActionResult> Pair([FromRoute] string pairCode)
{
var devicePairs = _redis.RedisCollection<DevicePair>();
var pair = await devicePairs.FirstOrDefaultAsync(x => x.PairCode == pairCode);
if (pair is null) return Problem(PairError.PairCodeNotFound);
await devicePairs.DeleteAsync(pair);
var deviceToken = await _db.Devices.Where(x => x.Id == pair.Id).Select(x => x.Token).FirstOrDefaultAsync();
if (deviceToken is null) throw new Exception("Device not found for pair code");
return LegacyDataOk(deviceToken);
}
}