Skip to content

Commit

Permalink
logger for tag CUD
Browse files Browse the repository at this point in the history
  • Loading branch information
EdiWang committed May 8, 2024
1 parent 17bfb68 commit 9905ced
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
8 changes: 6 additions & 2 deletions src/Moonglade.Core/TagFeature/CreateTagCommand.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
using Moonglade.Data;
using Microsoft.Extensions.Logging;
using Moonglade.Data;
using Moonglade.Data.Specifications;
using Moonglade.Utils;

namespace Moonglade.Core.TagFeature;

public record CreateTagCommand(string Name) : IRequest;

public class CreateTagCommandHandler(MoongladeRepository<TagEntity> repo) : IRequestHandler<CreateTagCommand>
public class CreateTagCommandHandler(
MoongladeRepository<TagEntity> repo,
ILogger<CreateTagCommandHandler> logger) : IRequestHandler<CreateTagCommand>
{
public async Task Handle(CreateTagCommand request, CancellationToken ct)
{
Expand All @@ -22,5 +25,6 @@ public async Task Handle(CreateTagCommand request, CancellationToken ct)
};

await repo.AddAsync(newTag, ct);
logger.LogInformation("Tag created: {TagName}", request.Name);
}
}
7 changes: 5 additions & 2 deletions src/Moonglade.Core/TagFeature/DeleteTagCommand.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Moonglade.Data;
using Microsoft.Extensions.Logging;
using Moonglade.Data;
using Moonglade.Data.Specifications;

namespace Moonglade.Core.TagFeature;
Expand All @@ -7,7 +8,8 @@ public record DeleteTagCommand(int Id) : IRequest<OperationCode>;

public class DeleteTagCommandHandler(
MoongladeRepository<TagEntity> tagRepo,
MoongladeRepository<PostTagEntity> postTagRepo)
MoongladeRepository<PostTagEntity> postTagRepo,
ILogger<DeleteTagCommandHandler> logger)
: IRequestHandler<DeleteTagCommand, OperationCode>
{
public async Task<OperationCode> Handle(DeleteTagCommand request, CancellationToken ct)
Expand All @@ -22,6 +24,7 @@ public async Task<OperationCode> Handle(DeleteTagCommand request, CancellationTo
// 2. Delte Tag itslef
await tagRepo.DeleteAsync(tag, ct);

logger.LogInformation("Deleted tag: {TagId}", request.Id);
return OperationCode.Done;
}
}
8 changes: 6 additions & 2 deletions src/Moonglade.Core/TagFeature/UpdateTagCommand.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using Moonglade.Data;
using Microsoft.Extensions.Logging;
using Moonglade.Data;
using Moonglade.Utils;

namespace Moonglade.Core.TagFeature;

public record UpdateTagCommand(int Id, string Name) : IRequest<OperationCode>;

public class UpdateTagCommandHandler(MoongladeRepository<TagEntity> repo) : IRequestHandler<UpdateTagCommand, OperationCode>
public class UpdateTagCommandHandler(
MoongladeRepository<TagEntity> repo,
ILogger<UpdateTagCommandHandler> logger) : IRequestHandler<UpdateTagCommand, OperationCode>
{
public async Task<OperationCode> Handle(UpdateTagCommand request, CancellationToken ct)
{
Expand All @@ -17,6 +20,7 @@ public async Task<OperationCode> Handle(UpdateTagCommand request, CancellationTo
tag.NormalizedName = Helper.NormalizeName(name, Helper.TagNormalizationDictionary);
await repo.UpdateAsync(tag, ct);

logger.LogInformation("Updated tag: {TagId}", request.Id);
return OperationCode.Done;
}
}

0 comments on commit 9905ced

Please sign in to comment.