blob: eb8bc37ade336f9983ad6d222ba3fe7d36c324f4 (
plain)
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
71
72
73
74
75
76
77
78
79
80
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Timeline.Entities;
using Timeline.Helpers;
namespace Timeline.Services.User.RegisterCode
{
public class RegisterCodeService : IRegisterCodeService, IDisposable
{
private readonly DatabaseContext _databaseContext;
private readonly RandomNumberGenerator _randomNumberGenerator;
public RegisterCodeService(DatabaseContext databaseContext)
{
_databaseContext = databaseContext;
_randomNumberGenerator = RandomNumberGenerator.Create();
}
public async Task<string> CreateNewCode(long userId)
{
var oldEntity = await _databaseContext.RegisterCodes.Where(r => r.OwnerId == userId && r.Enabled).SingleOrDefaultAsync();
if (oldEntity is not null)
{
oldEntity.Enabled = false;
}
var newEntity = new Entities.RegisterCode()
{
Code = _randomNumberGenerator.GenerateAlphaDigitString(6),
OwnerId = userId,
Enabled = true
};
_databaseContext.RegisterCodes.Add(newEntity);
await _databaseContext.SaveChangesAsync();
return newEntity.Code;
}
public Task<UserRegisterInfo> CreateRegisterInfo(long userId, long introducerId, string registerCode, DateTime registerTime)
{
throw new NotImplementedException();
}
public void Dispose()
{
_randomNumberGenerator.Dispose();
}
public async Task<long?> GetCodeOwner(string code, bool onlyEnabled = true)
{
var entity = await _databaseContext.RegisterCodes.Where(r => r.Code == code).SingleOrDefaultAsync();
if (entity is null) return null;
if (onlyEnabled && !entity.Enabled) return null;
return entity.OwnerId;
}
public async Task<string?> GetCurrentCode(long userId)
{
var entity = await _databaseContext.RegisterCodes.Where(r => r.OwnerId == userId && r.Enabled).SingleOrDefaultAsync();
return entity?.Code;
}
public Task<UserRegisterInfo?> GetUserRegisterInfo(long userId)
{
throw new NotImplementedException();
}
public Task<List<UserRegisterInfo>> GetUserRegisterInfoOfIntroducer(long introducerId)
{
throw new NotImplementedException();
}
}
}
|