Skip to content

Commit

Permalink
修复已知bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Codespilot committed Jan 16, 2024
1 parent fbc3612 commit 1611a33
Show file tree
Hide file tree
Showing 27 changed files with 235 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ protected SettingAbstractCommand(long appId, string environment)
public long AppId { get; set; }

/// <summary>
/// 环境名称
/// 应用环境
/// </summary>
public string Environment { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ namespace Nerosoft.Starfish.Application;
public class SettingArchiveCreateCommand : Command
{
/// <summary>
/// 配置根节点Id
/// 应用Id
/// </summary>
public long RootId { get; set; }
public long AppId { get; set; }

/// <summary>
/// 应用环境
/// </summary>
public string Environment { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ namespace Nerosoft.Starfish.Application;
public class SettingRevisionCreateCommand : Command
{
/// <summary>
/// 配置根节点Id
/// 应用Id
/// </summary>
public long SettingId { get; set; }
public long AppId { get; set; }

/// <summary>
/// 说明
Expand All @@ -21,4 +21,9 @@ public class SettingRevisionCreateCommand : Command
/// 版本号
/// </summary>
public string Version { get; set; }

/// <summary>
/// 应用环境
/// </summary>
public string Environment { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Nerosoft.Starfish.Application;

public class SettingValueUpdateCommand : SettingAbstractCommand
{
public SettingValueUpdateCommand(long appId, string environment, string key, string value)
: base(appId, environment)
{
AppId = appId;
Environment = environment;
Key = key;
Value = value;
}

public string Key { get; set; }

public string Value { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public Task HandleAsync(SettingArchiveCreateCommand message, MessageContext cont
{
return ExecuteAsync(async () =>
{
var (appId, appCode, environment, items) = await GetItemsAsync(message.RootId, cancellationToken);
var (appId, appCode, environment, items) = await GetItemsAsync(message.AppId, message.Environment, cancellationToken);
var data = items.ToDictionary(t => t.Key, t => t.Value);
var json = JsonConvert.SerializeObject(data);

Expand All @@ -30,15 +30,15 @@ public Task HandleAsync(SettingArchiveCreateCommand message, MessageContext cont
});
}

private async Task<Tuple<long, string, string, List<SettingItem>>> GetItemsAsync(long id, CancellationToken cancellationToken = default)
private async Task<Tuple<long, string, string, List<SettingItem>>> GetItemsAsync(long appId, string environment, CancellationToken cancellationToken = default)
{
var repository = UnitOfWork.Current.GetService<ISettingRepository>();

var aggregate = await repository.GetAsync(id, false, [nameof(Setting.Items)], cancellationToken);
var aggregate = await repository.GetAsync(appId, environment, false, [nameof(Setting.Items)], cancellationToken);

if (aggregate == null)
{
throw new SettingNotFoundException(id);
throw new SettingNotFoundException(appId, environment);
}

return Tuple.Create(aggregate.AppId, aggregate.AppCode, aggregate.Environment, aggregate.Items.ToList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ public class SettingCommandHandler : CommandHandlerBase,
IHandler<SettingCreateCommand>,
IHandler<SettingUpdateCommand>,
IHandler<SettingDeleteCommand>,
IHandler<SettingPublishCommand>
IHandler<SettingPublishCommand>,
IHandler<SettingValueUpdateCommand>
{
public SettingCommandHandler(IUnitOfWorkManager unitOfWork, IObjectFactory factory)
: base(unitOfWork, factory)
Expand Down Expand Up @@ -62,4 +63,15 @@ public Task HandleAsync(SettingPublishCommand message, MessageContext context, C
await Factory.ExecuteAsync<SettingPublishBusiness>(message.AppId, message.Environment, cancellationToken);
});
}

public Task HandleAsync(SettingValueUpdateCommand message, MessageContext context, CancellationToken cancellationToken = default)
{
return ExecuteAsync(async () =>
{
var business = await Factory.FetchAsync<SettingGeneralBusiness>(message.AppId, message.Environment, cancellationToken);
business.Key = message.Key;
business.Value = message.Value;
_ = await business.SaveAsync(true, cancellationToken);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ public Task HandleAsync(SettingRevisionCreateCommand message, MessageContext con
{
var repository = UnitOfWork.Current.GetService<ISettingRepository>();

var aggregate = await repository.GetAsync(message.SettingId, false, [nameof(Setting.Items), nameof(Setting.Revisions)], cancellationToken);
var aggregate = await repository.GetAsync(message.AppId, message.Environment, false, [nameof(Setting.Items), nameof(Setting.Revisions)], cancellationToken);

if (aggregate == null)
{
throw new SettingNotFoundException(message.SettingId);
throw new SettingNotFoundException(message.AppId, message.Environment);
}

aggregate.CreateRevision(message.Version, message.Comment, context.User?.Identity?.Name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public Task<SettingDetailDto> GetDetailAsync(long appId, string environment, Can
public Task<long> CreateAsync(long appId, string environment, string format, SettingEditDto data, CancellationToken cancellationToken = default)
{
var useCase = LazyServiceProvider.GetRequiredService<ISettingCreateUseCase>();
var input = new SettingCreateInput(appId, environment,format, data);
var input = new SettingCreateInput(appId, environment, format, data);
return useCase.ExecuteAsync(input, cancellationToken)
.ContinueWith(t => t.Result, cancellationToken);
}
Expand All @@ -63,7 +63,9 @@ public Task DeleteAsync(long appId, string environment, CancellationToken cancel

public Task UpdateAsync(long appId, string environment, string key, string value, CancellationToken cancellationToken = default)
{
throw new NotImplementedException();
var useCase = LazyServiceProvider.GetRequiredService<ISettingValueUpdateUseCase>();
var input = new SettingValueUpdateInput(appId, environment, key, value);
return useCase.ExecuteAsync(input, cancellationToken);
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,27 @@ namespace Nerosoft.Starfish.Application;
public sealed class LoggingEventSubscriber
{
private readonly IBus _bus;
private readonly IServiceProvider _provider;

public LoggingEventSubscriber(IBus bus, IServiceProvider provider)
public LoggingEventSubscriber(IBus bus)
{
_bus = bus;
_provider = provider;
}

/// <summary>
/// 处理用户认证成功事件
/// </summary>
/// <param name="message"></param>
/// <param name="event"></param>
/// <param name="context"></param>
/// <param name="cancellationToken"></param>
/// <exception cref="NotImplementedException"></exception>
[Subscribe]
public Task HandleAsync(UserAuthSucceedEvent message, MessageContext context, CancellationToken cancellationToken = default)
public Task HandleAsync(UserAuthSucceedEvent @event, MessageContext context, CancellationToken cancellationToken = default)
{
var command = new OperateLogCreateCommand
{
Module = "auth",
Type = message.AuthType,
UserName = message.UserName,
Type = @event.AuthType,
UserName = @event.UserName,
OperateTime = DateTime.Now,
Description = Resources.IDS_MESSAGE_LOGS_AUTH_SUCCEED,
RequestTraceId = context.RequestTraceId
Expand All @@ -42,21 +40,21 @@ public Task HandleAsync(UserAuthSucceedEvent message, MessageContext context, Ca
/// <summary>
/// 处理用户认证失败事件
/// </summary>
/// <param name="message"></param>
/// <param name="event"></param>
/// <param name="context"></param>
/// <param name="cancellationToken"></param>
/// <exception cref="NotImplementedException"></exception>
[Subscribe]
public Task HandleAsync(UserAuthFailedEvent message, MessageContext context, CancellationToken cancellationToken = default)
public Task HandleAsync(UserAuthFailedEvent @event, MessageContext context, CancellationToken cancellationToken = default)
{
var command = new OperateLogCreateCommand
{
Module = "auth",
Type = message.AuthType,
Type = @event.AuthType,
Description = Resources.IDS_MESSAGE_LOGS_AUTH_FAILED,
OperateTime = DateTime.Now,
RequestTraceId = context.RequestTraceId,
Error = message.Error
Error = @event.Error
};

return _bus.SendAsync(command, new SendOptions { RequestTraceId = context.RequestTraceId }, null, cancellationToken);
Expand All @@ -65,13 +63,13 @@ public Task HandleAsync(UserAuthFailedEvent message, MessageContext context, Can
/// <summary>
/// 处理应用创建事件
/// </summary>
/// <param name="message"></param>
/// <param name="event"></param>
/// <param name="context"></param>
/// <param name="cancellationToken"></param>
[Subscribe]
public Task HandleAsync(AppInfoCreatedEvent message, MessageContext context, CancellationToken cancellationToken = default)
public Task HandleAsync(AppInfoCreatedEvent @event, MessageContext context, CancellationToken cancellationToken = default)
{
var aggregate = message.GetAggregate<AppInfo>();
var aggregate = @event.GetAggregate<AppInfo>();
var command = new OperateLogCreateCommand
{
Module = "apps",
Expand All @@ -88,14 +86,14 @@ public Task HandleAsync(AppInfoCreatedEvent message, MessageContext context, Can
/// <summary>
/// 处理应用启用事件
/// </summary>
/// <param name="message"></param>
/// <param name="event"></param>
/// <param name="context"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
[Subscribe]
public Task HandleAsync(AppInfoEnabledEvent message, MessageContext context, CancellationToken cancellationToken = default)
public Task HandleAsync(AppInfoEnabledEvent @event, MessageContext context, CancellationToken cancellationToken = default)
{
var aggregate = message.GetAggregate<AppInfo>();
var aggregate = @event.GetAggregate<AppInfo>();
var command = new OperateLogCreateCommand
{
Module = "apps",
Expand All @@ -112,14 +110,14 @@ public Task HandleAsync(AppInfoEnabledEvent message, MessageContext context, Can
/// <summary>
/// 处理应用禁用事件
/// </summary>
/// <param name="message"></param>
/// <param name="event"></param>
/// <param name="context"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
[Subscribe]
public Task HandleAsync(AppInfoDisableEvent message, MessageContext context, CancellationToken cancellationToken = default)
public Task HandleAsync(AppInfoDisableEvent @event, MessageContext context, CancellationToken cancellationToken = default)
{
var aggregate = message.GetAggregate<AppInfo>();
var aggregate = @event.GetAggregate<AppInfo>();
var command = new OperateLogCreateCommand
{
Module = "appinfo",
Expand Down Expand Up @@ -183,12 +181,9 @@ public Task HandleAsync(SettingDeletedEvent @event, MessageContext context, Canc
}

[Subscribe]
public async Task HandleAsync(SettingPublishedEvent @event, MessageContext context, CancellationToken cancellationToken = default)
public Task HandleAsync(SettingPublishedEvent @event, MessageContext context, CancellationToken cancellationToken = default)
{
var repository = _provider.GetService<ISettingRepository>();
var setting = await repository.GetAsync(@event.AppId, false, [], cancellationToken);

var description = string.Format(Resources.IDS_MESSAGE_LOGS_SETTING_PUBLISH, setting.AppId, setting.Environment);
var description = string.Format(Resources.IDS_MESSAGE_LOGS_SETTING_PUBLISH, @event.AppId, @event.Environment);

var command = new OperateLogCreateCommand
{
Expand All @@ -199,6 +194,6 @@ public async Task HandleAsync(SettingPublishedEvent @event, MessageContext conte
RequestTraceId = context.RequestTraceId,
UserName = context.User?.Identity?.Name
};
await _bus.SendAsync(command, new SendOptions { RequestTraceId = context.RequestTraceId }, null, cancellationToken);
return _bus.SendAsync(command, new SendOptions { RequestTraceId = context.RequestTraceId }, null, cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public Task HandleAsync(SettingPublishedEvent message, MessageContext context, C
{
var command = new SettingArchiveCreateCommand
{
RootId = message.AppId
AppId = message.AppId,
Environment = message.Environment,
};

return _bus.SendAsync(command, new SendOptions { RequestTraceId = context.RequestTraceId }, null, cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public Task HandleAsync(SettingPublishedEvent message, MessageContext context, C
{
var command = new SettingRevisionCreateCommand
{
SettingId = message.AppId,
AppId = message.AppId,
Environment = message.Environment,
Version = message.Version,
Comment = message.Comment
};
Expand Down
23 changes: 22 additions & 1 deletion Source/Starfish.Service/Domain/Aggregates/Setting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ private Setting()
public string AppCode { get; set; }

/// <summary>
/// 环境名称
/// 应用环境
/// </summary>
public string Environment { get; set; }

Expand Down Expand Up @@ -113,6 +113,27 @@ internal void AddOrUpdateItem(IDictionary<string, string> items)
Status = SettingStatus.Pending;
}

internal void UpdateItem(string key, string value)
{
if (Status == SettingStatus.Disabled)
{
throw new InvalidOperationException(Resources.IDS_ERROR_SETTING_DISABLED);
}

Items ??= [];

var item = Items.FirstOrDefault(t => string.Equals(t.Key, key));

if (item == null)
{
throw new InvalidOperationException(string.Format(Resources.IDS_ERROR_SETTING_KEY_NOT_EXISTS, key));
}

item.Value = value;

Status = SettingStatus.Pending;
}

internal void SetStatus(SettingStatus status)
{
if (Status == status)
Expand Down
Loading

0 comments on commit 1611a33

Please sign in to comment.