From 9f189100c93c43372459c25984cb56ed00165f39 Mon Sep 17 00:00:00 2001 From: crupest Date: Mon, 18 Apr 2022 18:38:32 +0800 Subject: ... --- BackEnd/Timeline/Services/User/CreateUserParams.cs | 2 +- .../User/RegisterCode/IRegisterCodeService.cs | 12 +++++++ .../RegisterCode/InvalidRegisterCodeException.cs | 42 ++++++++++++++++++++++ .../User/RegisterCode/RegisterCodeService.cs | 20 ++++++++++- 4 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 BackEnd/Timeline/Services/User/RegisterCode/InvalidRegisterCodeException.cs (limited to 'BackEnd/Timeline') 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 @@ -45,6 +45,18 @@ namespace Timeline.Services.User.RegisterCode /// The user register info if there is one. Or null if there is not. Task GetUserRegisterInfoAsync(long userId); + /// + /// Create a user with register code. + /// + /// The params to create user with. + /// The user code. + /// The created user. + /// Thrown when or is null. + /// Thrown when is invalid. + /// Thrown when username already exist. + /// Thrown when register code is invalid. + Task RegisterUserWithCode(CreateUserParams userParams, string registerCode); + /// /// Get the list of user register info of the specified introducer. /// 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 + { + /// + /// Initializes a new instance of the class + /// + public InvalidRegisterCodeException() + { + } + + /// + /// Initializes a new instance of the class + /// + /// A that describes the exception. + public InvalidRegisterCodeException(string message) : base(message) + { + } + + /// + /// Initializes a new instance of the class + /// + /// A that describes the exception. + /// The exception that is the cause of the current exception. + public InvalidRegisterCodeException(string message, System.Exception inner) : base(message, inner) + { + } + + /// + /// Initializes a new instance of the class + /// + /// The contextual information about the source or destination. + /// The object that holds the serialized object data. + 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 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; + } } } -- cgit v1.2.3