Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/dotnetLab.Database.SampleDb/dotnetLab.Database.SampleDb.csproj
#	src/dotnetLab.Repository/dotnetLab.Repository.csproj
#	src/dotnetLab.WebApi/dotnetLab.WebApi.csproj
  • Loading branch information
YuChia-Wei committed Jul 17, 2024
2 parents e43db58 + dfd8bd9 commit f205227
Show file tree
Hide file tree
Showing 15 changed files with 204 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Mediator;

namespace dotnetLab.DomainEntity.Events;

public record SimpleDocumentDescriptionUpdatedEvent : INotification
{
public int SerialId { get; set; }
public string? OldDescription { get; set; }
public string? NewDescription { get; set; }
}
16 changes: 15 additions & 1 deletion src/dotnetLab.DomainEntity/SimpleDocumentEntity.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace dotnetLab.DomainEntity;
using dotnetLab.DomainEntity.Events;

namespace dotnetLab.DomainEntity;

public class SimpleDocumentEntity
{
Expand Down Expand Up @@ -26,4 +28,16 @@ public SimpleDocumentEntity()
/// 文件編號
/// </summary>
public string DocumentNum { get; set; } = string.Empty;

public SimpleDocumentDescriptionUpdatedEvent UpdateDescription(string? description)
{
var simpleDocumentDescriptionUpdatedEvent = new SimpleDocumentDescriptionUpdatedEvent
{
SerialId = this.SerialId,
OldDescription = this.Description,
NewDescription = description
};
this.Description = description;
return simpleDocumentDescriptionUpdatedEvent;
}
}
4 changes: 4 additions & 0 deletions src/dotnetLab.DomainEntity/dotnetLab.DomainEntity.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Mediator.Abstractions" Version="2.1.7" />
</ItemGroup>

</Project>
10 changes: 10 additions & 0 deletions src/dotnetLab.Repository/Implements/GrpcSampleDataRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,14 @@ public async Task<int> SaveAsync(SimpleDocumentEntity simpleDocument)
{
throw new NotImplementedException();
}

/// <summary>
/// 儲存文件
/// </summary>
/// <param name="sampleTable"></param>
/// <returns></returns>
public async Task<bool> UpdateAsync(SimpleDocumentEntity sampleTable)
{
throw new NotImplementedException();
}
}
10 changes: 10 additions & 0 deletions src/dotnetLab.Repository/Implements/MockDataRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,14 @@ public Task<int> SaveAsync(SimpleDocumentEntity simpleDocument)
{
return Task.FromResult(123);
}

/// <summary>
/// 儲存文件
/// </summary>
/// <param name="sampleTable"></param>
/// <returns></returns>
public Task<bool> UpdateAsync(SimpleDocumentEntity sampleTable)
{
return Task.FromResult(true);
}
}
10 changes: 10 additions & 0 deletions src/dotnetLab.Repository/Implements/SimpleDocumentRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,14 @@ public async Task<int> SaveAsync(SimpleDocumentEntity simpleDocument)

return simpleDocument.SerialId;
}

/// <summary>
/// 儲存文件
/// </summary>
/// <param name="sampleTable"></param>
/// <returns></returns>
public async Task<bool> UpdateAsync(SimpleDocumentEntity sampleTable)
{
throw new NotImplementedException();
}
}
4 changes: 2 additions & 2 deletions src/dotnetLab.Repository/dotnetLab.Repository.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<Protobuf Include="Protos\greet.proto" GrpcServices="Client" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Dapper" Version="2.1.35" />
<PackageReference Include="Google.Protobuf" Version="3.27.2" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace dotnetLab.UseCase.SimpleDocument.Commands;

/// <summary>
/// sample command
/// </summary>
public class UpdateSimpleDocumentDescriptionCommand : IRequest<bool>
{
/// <summary>
/// 序號
/// </summary>
public int SerialId { get; set; }

/// <summary>
/// 敘述
/// </summary>
public string? Description { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using dotnetLab.UseCase.SimpleDocument.Ports.Out;

namespace dotnetLab.UseCase.SimpleDocument.Commands;

/// <summary>
/// command handler
/// </summary>
public class UpdateSimpleDocumentDescriptionCommandHandler : IRequestHandler<UpdateSimpleDocumentDescriptionCommand, bool>
{
private readonly IMediator _mediator;
private readonly ISimpleDocumentRepository _simpleDocumentRepository;

public UpdateSimpleDocumentDescriptionCommandHandler(
ISimpleDocumentRepository simpleDocumentRepository,
IMediator mediator)
{
this._simpleDocumentRepository = simpleDocumentRepository;
this._mediator = mediator;
}

/// <summary>Handles a request</summary>
/// <param name="request">The request</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>Response from the request</returns>
public async ValueTask<bool> Handle(UpdateSimpleDocumentDescriptionCommand request, CancellationToken cancellationToken)
{
var simpleDocumentEntity = await this._simpleDocumentRepository.GetAsync(request.SerialId);

if (simpleDocumentEntity is null)
{
return false;
}

var @event = simpleDocumentEntity.UpdateDescription(request.Description);

await this._mediator.Publish(@event, cancellationToken);

return await this._simpleDocumentRepository.UpdateAsync(simpleDocumentEntity);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using dotnetLab.DomainEntity.Events;
using Microsoft.Extensions.Logging;

namespace dotnetLab.UseCase.SimpleDocument.Events;

public class PublishUpdatedInformation : INotificationHandler<SimpleDocumentDescriptionUpdatedEvent>
{
private readonly ILogger<PublishUpdatedInformation> _logger;

public PublishUpdatedInformation(ILogger<PublishUpdatedInformation> logger)
{
this._logger = logger;
}

public ValueTask Handle(SimpleDocumentDescriptionUpdatedEvent notification, CancellationToken cancellationToken)
{
this._logger.LogInformation($"{DateTime.Now} Simple Document Description was updated!");
return ValueTask.CompletedTask;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using dotnetLab.DomainEntity.Events;
using Microsoft.Extensions.Logging;

namespace dotnetLab.UseCase.SimpleDocument.Events;

public class SimpleDocDescUpdatedEventHandler : INotificationHandler<SimpleDocumentDescriptionUpdatedEvent>
{
private readonly ILogger<SimpleDocDescUpdatedEventHandler> _logger;

public SimpleDocDescUpdatedEventHandler(ILogger<SimpleDocDescUpdatedEventHandler> logger)
{
this._logger = logger;
}

public ValueTask Handle(SimpleDocumentDescriptionUpdatedEvent notification, CancellationToken cancellationToken)
{
this._logger.LogInformation($"{DateTime.Now} Simple Document Description was updated!");
return ValueTask.CompletedTask;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,11 @@ public interface ISimpleDocumentRepository
/// <param name="sampleTable"></param>
/// <returns></returns>
Task<int> SaveAsync(SimpleDocumentEntity sampleTable);

/// <summary>
/// 儲存文件
/// </summary>
/// <param name="sampleTable"></param>
/// <returns></returns>
Task<bool> UpdateAsync(SimpleDocumentEntity sampleTable);
}
5 changes: 1 addition & 4 deletions src/dotnetLab.UseCase/dotnetLab.UseCase.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<Folder Include="SimpleDocument\" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
</ItemGroup>

</Project>
30 changes: 22 additions & 8 deletions src/dotnetLab.WebApi/Controllers/SampleController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,43 @@ public SampleController(IMediator mediator)
}

/// <summary>
/// 僅需要 Client Credentials 認證
/// get simple doc
/// </summary>
/// <param name="dataCommand"></param>
/// <param name="command"></param>
/// <returns></returns>
[HttpGet]
[ParameterValidator<SimpleDocQueryValidator>]
// [Authorize]
public async Task<IActionResult> Get([FromQuery] SimpleDocQuery dataCommand)
public async Task<IActionResult> Get([FromQuery] SimpleDocQuery command)
{
return this.Ok(await this._mediator.Send(dataCommand));
return this.Ok(await this._mediator.Send(command));
}

/// <summary>
/// 需要 LoginUser 的認證 (使用有身分的 OAuth 流程)
/// create simple doc
/// </summary>
/// <param name="dataCommand"></param>
/// <param name="command"></param>
/// <returns></returns>
[HttpPost]
[ParameterValidator<InputSimpleDocumentCommandValidator>]
// [Authorize(nameof(LoginUserRequestedPolicy))]
public async Task<IActionResult> Post([FromBody] InputSimpleDocumentCommand dataCommand)
public async Task<IActionResult> Post([FromBody] InputSimpleDocumentCommand command)
{
var send = await this._mediator.Send(dataCommand);
var send = await this._mediator.Send(command);
return this.Ok(send);
}

/// <summary>
/// update simple doc Description
/// </summary>
/// <param name="command"></param>
/// <returns></returns>
[HttpPatch]
[ParameterValidator<UpdateSimpleDocumentDescriptionCommandValidator>]
// [Authorize(nameof(LoginUserRequestedPolicy))]
public async Task<IActionResult> Post([FromBody] UpdateSimpleDocumentDescriptionCommand command)
{
var send = await this._mediator.Send(command);
return this.Ok(send);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using dotnetLab.UseCase.SimpleDocument.Commands;
using FluentValidation;

namespace dotnetLab.WebApi.Controllers.Validator;

/// <summary>
/// UpdateSimpleDocumentDescriptionCommand 的參數驗證器
/// </summary>
public class UpdateSimpleDocumentDescriptionCommandValidator : AbstractValidator<UpdateSimpleDocumentDescriptionCommand>
{
public UpdateSimpleDocumentDescriptionCommandValidator()
{
this.RuleFor(o => o.Description)
.NotEmpty();
}
}

0 comments on commit f205227

Please sign in to comment.