diff --git a/Source/Starfish.Service/Application/ApplicationServiceModule.cs b/Source/Starfish.Service/Application/ApplicationServiceModule.cs index 26e53bc..842d90a 100644 --- a/Source/Starfish.Service/Application/ApplicationServiceModule.cs +++ b/Source/Starfish.Service/Application/ApplicationServiceModule.cs @@ -49,6 +49,8 @@ public override void ConfigureServices(ServiceConfigurationContext context) ConfigureCachingServices(context.Services); ConfigureBusServices(context.Services); + + context.Services.AddHostedService(); } private void ConfigureCachingServices(IServiceCollection services) diff --git a/Source/Starfish.Service/Application/Handlers/UserCommandHandler.cs b/Source/Starfish.Service/Application/Handlers/UserCommandHandler.cs index 5b57a24..9a35d2d 100644 --- a/Source/Starfish.Service/Application/Handlers/UserCommandHandler.cs +++ b/Source/Starfish.Service/Application/Handlers/UserCommandHandler.cs @@ -54,7 +54,11 @@ public Task HandleAsync(UserUpdateCommand message, MessageContext context, Cance business.Email = message.Item2.Email; business.NickName = message.Item2.NickName; + business.Phone = message.Item2.Phone; + business.IsAdmin = message.Item2.IsAdmin; + business.MarkAsUpdate(); + await business.SaveAsync(true, cancellationToken); }); } diff --git a/Source/Starfish.Service/Application/Services/UserInitializeService.cs b/Source/Starfish.Service/Application/Services/UserInitializeService.cs new file mode 100644 index 0000000..4fff946 --- /dev/null +++ b/Source/Starfish.Service/Application/Services/UserInitializeService.cs @@ -0,0 +1,28 @@ +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; + +namespace Nerosoft.Starfish.Application; + +internal class UserInitializeService : BackgroundService +{ + private readonly IUserApplicationService _service; + private readonly ILogger _logger; + + public UserInitializeService(IUserApplicationService service, ILoggerFactory logger) + { + _service = service; + _logger = logger.CreateLogger(); + } + + protected override async Task ExecuteAsync(CancellationToken stoppingToken) + { + try + { + await _service.InitializeAsync(stoppingToken); + } + catch (Exception exception) + { + _logger.LogError(exception, exception.Message); + } + } +} diff --git a/Source/Starfish.Service/Domain/Business/UserGeneralBusiness.cs b/Source/Starfish.Service/Domain/Business/UserGeneralBusiness.cs index 2c7dd20..44ed584 100644 --- a/Source/Starfish.Service/Domain/Business/UserGeneralBusiness.cs +++ b/Source/Starfish.Service/Domain/Business/UserGeneralBusiness.cs @@ -140,6 +140,11 @@ protected override Task InsertAsync(CancellationToken cancellationToken = defaul [FactoryUpdate] protected override Task UpdateAsync(CancellationToken cancellationToken = default) { + if (!HasChangedProperties) + { + return Task.CompletedTask; + } + if (ChangedProperties.Contains(EmailProperty)) { Aggregate.SetEmail(Email); diff --git a/Source/Starfish.Service/UseCases/Identity/UserInitializeUseCase.cs b/Source/Starfish.Service/UseCases/Identity/UserInitializeUseCase.cs index d8543f8..bf41a8f 100644 --- a/Source/Starfish.Service/UseCases/Identity/UserInitializeUseCase.cs +++ b/Source/Starfish.Service/UseCases/Identity/UserInitializeUseCase.cs @@ -1,8 +1,8 @@ +using Microsoft.Extensions.Configuration; using Nerosoft.Euonia.Application; using Nerosoft.Euonia.Bus; -using Nerosoft.Euonia.Mapping; using Nerosoft.Starfish.Application; -using Nerosoft.Starfish.Transit; +using Nerosoft.Starfish.Domain; namespace Nerosoft.Starfish.UseCases; @@ -11,23 +11,38 @@ internal interface IUserInitializeUseCase : IParameterlessUseCase; internal class UserInitializeUseCase : IUserInitializeUseCase { private readonly IBus _bus; + private readonly IUserRepository _repository; + private readonly IConfiguration _configuration; - public UserInitializeUseCase(IBus bus) + public UserInitializeUseCase(IBus bus, IUserRepository repository, IConfiguration configuration) { _bus = bus; + _repository = repository; + _configuration = configuration; } public async Task ExecuteAsync(CancellationToken cancellationToken = default) { - var userCreateDto = new UserCreateDto + var username = _configuration["InitializeUser:UserName"]; + + if (string.IsNullOrWhiteSpace(username)) + { + return; + } + + var exists = await _repository.CheckUserNameExistsAsync(username, cancellationToken); + if (exists) + { + return; + } + + var command = new UserCreateCommand { - UserName = "starfish", - Password = "starfish.888", - NickName = "admin", - IsAdmin = true + UserName = _configuration["InitializeUser:UserName"], + Password = _configuration["InitializeUser:Password"], + IsAdmin = true, + Reserved = true }; - var command = TypeAdapter.ProjectedAs(userCreateDto); - command.Reserved = true; - await _bus.SendAsync(command, cancellationToken); + await _bus.SendAsync(command, cancellationToken).ContinueWith(task => task.WaitAndUnwrapException()); } } \ No newline at end of file diff --git a/Source/Starfish.Webapi/Controllers/UserController.cs b/Source/Starfish.Webapi/Controllers/UserController.cs index bb92aa6..b43c90e 100644 --- a/Source/Starfish.Webapi/Controllers/UserController.cs +++ b/Source/Starfish.Webapi/Controllers/UserController.cs @@ -128,16 +128,4 @@ public async Task ChangePasswordAsync([FromBody] ChangePasswordRe await _service.ChangePasswordAsync(data.OldPassword, data.NewPassword, HttpContext.RequestAborted); return Ok(); } - - /// - /// 初始化用户数据 - /// - /// - [HttpPost("init")] - [AllowAnonymous] - public async Task InitializeAsync() - { - await _service.InitializeAsync(HttpContext.RequestAborted); - return Ok(); - } } \ No newline at end of file diff --git a/Source/Starfish.Webapi/appsettings.json b/Source/Starfish.Webapi/appsettings.json index 1a11702..85142d8 100644 --- a/Source/Starfish.Webapi/appsettings.json +++ b/Source/Starfish.Webapi/appsettings.json @@ -27,7 +27,10 @@ "TopicName": "nerosoft.starfish.topic" } }, - "InitializeUsername": "starfish", + "InitializeUser": { + "UserName": "admin", + "Password": "Starfish.8888" + }, "ReservedUsernames": [ "starfish", "admin",