aboutsummaryrefslogtreecommitdiff
path: root/BackEnd/Timeline
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2022-04-18 18:21:50 +0800
committercrupest <crupest@outlook.com>2022-04-18 18:21:50 +0800
commitf840db633076f8bb172beee5f0f8cab2d76ee23a (patch)
treebaada4c47ee001993c920a407ae00cc3ee74e9a5 /BackEnd/Timeline
parentad2829be08116cdb596990c1152e1dfc4f0ffe47 (diff)
downloadtimeline-f840db633076f8bb172beee5f0f8cab2d76ee23a.tar.gz
timeline-f840db633076f8bb172beee5f0f8cab2d76ee23a.tar.bz2
timeline-f840db633076f8bb172beee5f0f8cab2d76ee23a.zip
...
Diffstat (limited to 'BackEnd/Timeline')
-rw-r--r--BackEnd/Timeline/Services/User/RegisterCode/IRegisterCodeService.cs13
-rw-r--r--BackEnd/Timeline/Services/User/RegisterCode/RegisterCodeService.cs42
2 files changed, 39 insertions, 16 deletions
diff --git a/BackEnd/Timeline/Services/User/RegisterCode/IRegisterCodeService.cs b/BackEnd/Timeline/Services/User/RegisterCode/IRegisterCodeService.cs
index 65decefc..e0031a38 100644
--- a/BackEnd/Timeline/Services/User/RegisterCode/IRegisterCodeService.cs
+++ b/BackEnd/Timeline/Services/User/RegisterCode/IRegisterCodeService.cs
@@ -13,44 +13,43 @@ namespace Timeline.Services.User.RegisterCode
/// <param name="code">The register code.</param>
/// <param name="onlyEnabled">If true, only when code is enabled the owner id is returned.</param>
/// <returns>A task contains the owner of the register code. Null of the code does not exist or is not enabled.</returns>
- Task<long?> GetCodeOwner(string code, bool onlyEnabled = true);
+ Task<long?> GetCodeOwnerAsync(string code, bool onlyEnabled = true);
/// <summary>
/// Get the current enabled register code of the user or null if there is none.
/// </summary>
/// <param name="userId">The user id.</param>
/// <returns>A task contains current enabled register code or null if there is none.</returns>
- Task<string?> GetCurrentCode(long userId);
+ Task<string?> GetCurrentCodeAsync(long userId);
/// <summary>
/// Create a new register code for a user, enable it and disable the previous one if there is a previous one.
/// </summary>
/// <param name="userId">The user id.</param>
/// <returns>A task contains the new register code.</returns>
- Task<string> CreateNewCode(long userId);
+ Task<string> CreateNewCodeAsync(long userId);
/// <summary>
/// Record a register info for a user.
/// </summary>
/// <param name="userId">The newly-registered user.</param>
- /// <param name="introducerId">The introducer user id.</param>
/// <param name="registerCode">The register code.</param>
/// <param name="registerTime">The register time.</param>
/// <returns>The created register info.</returns>
- Task<UserRegisterInfo> CreateRegisterInfo(long userId, long introducerId, string registerCode, DateTime registerTime);
+ Task<UserRegisterInfo> CreateRegisterInfoAsync(long userId, string registerCode, DateTime registerTime);
/// <summary>
/// Get register info of a user if there is one.
/// </summary>
/// <param name="userId">The user id.</param>
/// <returns>The user register info if there is one. Or null if there is not.</returns>
- Task<UserRegisterInfo?> GetUserRegisterInfo(long userId);
+ Task<UserRegisterInfo?> GetUserRegisterInfoAsync(long userId);
/// <summary>
/// Get the list of user register info of the specified introducer.
/// </summary>
/// <param name="introducerId"></param>
/// <returns>The list of user register info.</returns>
- Task<List<UserRegisterInfo>> GetUserRegisterInfoOfIntroducer(long introducerId);
+ Task<List<UserRegisterInfo>> GetUserRegisterInfoOfIntroducerAsync(long introducerId);
}
}
diff --git a/BackEnd/Timeline/Services/User/RegisterCode/RegisterCodeService.cs b/BackEnd/Timeline/Services/User/RegisterCode/RegisterCodeService.cs
index eb8bc37a..97a685d6 100644
--- a/BackEnd/Timeline/Services/User/RegisterCode/RegisterCodeService.cs
+++ b/BackEnd/Timeline/Services/User/RegisterCode/RegisterCodeService.cs
@@ -12,16 +12,22 @@ namespace Timeline.Services.User.RegisterCode
public class RegisterCodeService : IRegisterCodeService, IDisposable
{
private readonly DatabaseContext _databaseContext;
+ private readonly IUserService _userService;
+
private readonly RandomNumberGenerator _randomNumberGenerator;
- public RegisterCodeService(DatabaseContext databaseContext)
+ public RegisterCodeService(DatabaseContext databaseContext, IUserService userService)
{
_databaseContext = databaseContext;
+ _userService = userService;
+
_randomNumberGenerator = RandomNumberGenerator.Create();
}
- public async Task<string> CreateNewCode(long userId)
+ public async Task<string> CreateNewCodeAsync(long userId)
{
+ await _userService.CheckUserExistenceAsync(userId);
+
var oldEntity = await _databaseContext.RegisterCodes.Where(r => r.OwnerId == userId && r.Enabled).SingleOrDefaultAsync();
if (oldEntity is not null)
@@ -42,9 +48,24 @@ namespace Timeline.Services.User.RegisterCode
return newEntity.Code;
}
- public Task<UserRegisterInfo> CreateRegisterInfo(long userId, long introducerId, string registerCode, DateTime registerTime)
+ public async Task<UserRegisterInfo> CreateRegisterInfoAsync(long userId, string registerCode, DateTime registerTime)
{
- throw new NotImplementedException();
+ await _userService.CheckUserExistenceAsync(userId);
+
+ var introducerId = await GetCodeOwnerAsync(registerCode, false);
+
+ var entity = new UserRegisterInfo()
+ {
+ UserId = userId,
+ IntroducerId = introducerId,
+ RegisterCode = registerCode,
+ RegisterTime = registerTime
+ };
+
+ _databaseContext.UserRegisterInfos.Add(entity);
+ await _databaseContext.SaveChangesAsync();
+
+ return entity;
}
public void Dispose()
@@ -52,7 +73,7 @@ namespace Timeline.Services.User.RegisterCode
_randomNumberGenerator.Dispose();
}
- public async Task<long?> GetCodeOwner(string code, bool onlyEnabled = true)
+ public async Task<long?> GetCodeOwnerAsync(string code, bool onlyEnabled = true)
{
var entity = await _databaseContext.RegisterCodes.Where(r => r.Code == code).SingleOrDefaultAsync();
if (entity is null) return null;
@@ -60,18 +81,21 @@ namespace Timeline.Services.User.RegisterCode
return entity.OwnerId;
}
- public async Task<string?> GetCurrentCode(long userId)
+ public async Task<string?> GetCurrentCodeAsync(long userId)
{
+ await _userService.CheckUserExistenceAsync(userId);
+
var entity = await _databaseContext.RegisterCodes.Where(r => r.OwnerId == userId && r.Enabled).SingleOrDefaultAsync();
return entity?.Code;
}
- public Task<UserRegisterInfo?> GetUserRegisterInfo(long userId)
+ public async Task<UserRegisterInfo?> GetUserRegisterInfoAsync(long userId)
{
- throw new NotImplementedException();
+ await _userService.CheckUserExistenceAsync(userId);
+ return await _databaseContext.UserRegisterInfos.Where(i => i.UserId == userId).SingleOrDefaultAsync();
}
- public Task<List<UserRegisterInfo>> GetUserRegisterInfoOfIntroducer(long introducerId)
+ public Task<List<UserRegisterInfo>> GetUserRegisterInfoOfIntroducerAsync(long introducerId)
{
throw new NotImplementedException();
}