-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCopyData.cs
70 lines (60 loc) · 2.34 KB
/
CopyData.cs
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
namespace api.multifol.io
{
public class CopyData
{
private readonly ILogger<CopyData> _logger;
public CopyData(ILogger<CopyData> logger)
{
_logger = logger;
}
private static Dictionary<string, string> DataStorage = new();
[Function("CopyData")]
public async Task<IActionResult> RunAsync([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post")] HttpRequest req)
{
_logger.LogInformation("CopyData processed a request.");
string copyCode = req.Query["copyCode"];
string profileData = req.Query["profileData"];
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
copyCode = copyCode ?? data?.copyCode;
profileData = profileData ?? data?.profileData;
if (profileData != null)
{
bool foundUniqueCopyCode = false;
string newCopyCode = null;
int maxCount = 10;
while (!foundUniqueCopyCode && maxCount > 0)
{
maxCount--;
long security = (long)(Random.Shared.NextDouble() * 999999999.0);
newCopyCode = security.ToString();
if (!DataStorage.ContainsKey(newCopyCode)) {
foundUniqueCopyCode = true;
DataStorage.Add(newCopyCode, profileData);
return new OkObjectResult(newCopyCode);
}
}
return new BadRequestObjectResult("no security string possible");
}
else if (copyCode != null)
{
if (DataStorage.ContainsKey(copyCode))
{
var result = DataStorage[copyCode];
DataStorage.Remove(copyCode);
return new OkObjectResult(result);
}
else
{
return new BadRequestObjectResult(null);
}
}
return new BadRequestObjectResult(null);
}
}
}