diff options
author | crupest <crupest@outlook.com> | 2022-04-18 18:38:32 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2022-04-18 18:38:32 +0800 |
commit | 9f189100c93c43372459c25984cb56ed00165f39 (patch) | |
tree | b6b379a2e2a99a273e72c5361e0bb86652afa84c /BackEnd | |
parent | f840db633076f8bb172beee5f0f8cab2d76ee23a (diff) | |
download | timeline-9f189100c93c43372459c25984cb56ed00165f39.tar.gz timeline-9f189100c93c43372459c25984cb56ed00165f39.tar.bz2 timeline-9f189100c93c43372459c25984cb56ed00165f39.zip |
...
Diffstat (limited to 'BackEnd')
5 files changed, 91 insertions, 3 deletions
diff --git a/BackEnd/Timeline.Tests/ServiceTests/User/RegisterCode/RegisterCodeServiceTest.cs b/BackEnd/Timeline.Tests/ServiceTests/User/RegisterCode/RegisterCodeServiceTest.cs index a0e3d20e..1afc94a1 100644 --- a/BackEnd/Timeline.Tests/ServiceTests/User/RegisterCode/RegisterCodeServiceTest.cs +++ b/BackEnd/Timeline.Tests/ServiceTests/User/RegisterCode/RegisterCodeServiceTest.cs @@ -1,6 +1,7 @@ using System;
using System.Threading.Tasks; using FluentAssertions; +using Timeline.Services.User; using Timeline.Services.User.RegisterCode; using Xunit; @@ -12,7 +13,7 @@ namespace Timeline.Tests.ServiceTests.User.RegisterCode protected override void OnInitialize() { - _registerCodeService = new RegisterCodeService(Database, UserService); + _registerCodeService = new RegisterCodeService(Database, UserService, Clock); } protected override void OnDispose() @@ -89,5 +90,20 @@ namespace Timeline.Tests.ServiceTests.User.RegisterCode c.IntroducerId.Should().Be(AdminId); c.RegisterTime.Should().Be(dateTime); } + + [Fact] + public async Task RegisterUserTest() + { + var registerCode = await _registerCodeService.CreateNewCodeAsync(AdminId); + + var a = await _registerCodeService.RegisterUserWithCode(new CreateUserParams("user2", "user2pw"), registerCode); + a.Should().NotBeNull(); + + await _registerCodeService.CreateNewCodeAsync(AdminId); + + await _registerCodeService.Awaiting(s => s.RegisterUserWithCode(new CreateUserParams("user3", "user3pw"), registerCode)) + .Should().ThrowAsync<InvalidRegisterCodeException>(); + + } } } diff --git a/BackEnd/Timeline/Services/User/CreateUserParams.cs b/BackEnd/Timeline/Services/User/CreateUserParams.cs index e66f83dc..994268a0 100644 --- a/BackEnd/Timeline/Services/User/CreateUserParams.cs +++ b/BackEnd/Timeline/Services/User/CreateUserParams.cs @@ -3,7 +3,7 @@ namespace Timeline.Services.User
{
public class CreateUserParams
- {
+ { public CreateUserParams(string username, string password)
{
Username = username ?? throw new ArgumentNullException(nameof(username));
diff --git a/BackEnd/Timeline/Services/User/RegisterCode/IRegisterCodeService.cs b/BackEnd/Timeline/Services/User/RegisterCode/IRegisterCodeService.cs index e0031a38..fe1188ea 100644 --- a/BackEnd/Timeline/Services/User/RegisterCode/IRegisterCodeService.cs +++ b/BackEnd/Timeline/Services/User/RegisterCode/IRegisterCodeService.cs @@ -46,6 +46,18 @@ namespace Timeline.Services.User.RegisterCode Task<UserRegisterInfo?> GetUserRegisterInfoAsync(long userId); /// <summary> + /// Create a user with register code. + /// </summary> + /// <param name="userParams">The params to create user with.</param> + /// <param name="registerCode">The user code.</param> + /// <returns>The created user.</returns> + /// <exception cref="ArgumentNullException">Thrown when <paramref name="userParams"/> or <paramref name="registerCode"/> is null.</exception> + /// <exception cref="ArgumentException">Thrown when <paramref name="userParams"/> is invalid.</exception> + /// <exception cref="EntityAlreadyExistException">Thrown when username already exist.</exception> + /// <exception cref="InvalidRegisterCodeException">Thrown when register code is invalid.</exception> + Task<UserEntity> RegisterUserWithCode(CreateUserParams userParams, string registerCode); + + /// <summary> /// Get the list of user register info of the specified introducer. /// </summary> /// <param name="introducerId"></param> diff --git a/BackEnd/Timeline/Services/User/RegisterCode/InvalidRegisterCodeException.cs b/BackEnd/Timeline/Services/User/RegisterCode/InvalidRegisterCodeException.cs new file mode 100644 index 00000000..d779609b --- /dev/null +++ b/BackEnd/Timeline/Services/User/RegisterCode/InvalidRegisterCodeException.cs @@ -0,0 +1,42 @@ +using System; +namespace Timeline.Services.User.RegisterCode +{ + + [Serializable] + public class InvalidRegisterCodeException : Exception + { + /// <summary> + /// Initializes a new instance of the <see cref="T:InvalidRegisterCodeException"/> class + /// </summary> + public InvalidRegisterCodeException() + { + } + + /// <summary> + /// Initializes a new instance of the <see cref="T:InvalidRegisterCodeException"/> class + /// </summary> + /// <param name="message">A <see cref="T:System.String"/> that describes the exception. </param> + public InvalidRegisterCodeException(string message) : base(message) + { + } + + /// <summary> + /// Initializes a new instance of the <see cref="T:InvalidRegisterCodeException"/> class + /// </summary> + /// <param name="message">A <see cref="T:System.String"/> that describes the exception. </param> + /// <param name="inner">The exception that is the cause of the current exception. </param> + public InvalidRegisterCodeException(string message, System.Exception inner) : base(message, inner) + { + } + + /// <summary> + /// Initializes a new instance of the <see cref="T:InvalidRegisterCodeException"/> class + /// </summary> + /// <param name="context">The contextual information about the source or destination.</param> + /// <param name="info">The object that holds the serialized object data.</param> + protected InvalidRegisterCodeException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) : base(info, context) + { + } + } +} + diff --git a/BackEnd/Timeline/Services/User/RegisterCode/RegisterCodeService.cs b/BackEnd/Timeline/Services/User/RegisterCode/RegisterCodeService.cs index 97a685d6..254d3747 100644 --- a/BackEnd/Timeline/Services/User/RegisterCode/RegisterCodeService.cs +++ b/BackEnd/Timeline/Services/User/RegisterCode/RegisterCodeService.cs @@ -14,13 +14,17 @@ namespace Timeline.Services.User.RegisterCode private readonly DatabaseContext _databaseContext; private readonly IUserService _userService; + private readonly IClock _clock; + private readonly RandomNumberGenerator _randomNumberGenerator; - public RegisterCodeService(DatabaseContext databaseContext, IUserService userService) + public RegisterCodeService(DatabaseContext databaseContext, IUserService userService, IClock clock) { _databaseContext = databaseContext; _userService = userService; + _clock = clock; + _randomNumberGenerator = RandomNumberGenerator.Create(); } @@ -99,6 +103,20 @@ namespace Timeline.Services.User.RegisterCode { throw new NotImplementedException(); } + + public async Task<UserEntity> RegisterUserWithCode(CreateUserParams userParams, string registerCode) + { + var registerCodeOwner = await GetCodeOwnerAsync(registerCode); + if (registerCodeOwner is null) + { + throw new InvalidRegisterCodeException("Register code is invalid."); + } + + var user = await _userService.CreateUserAsync(userParams); + await CreateRegisterInfoAsync(user.Id, registerCode, _clock.GetCurrentTime()); + + return user; + } } } |