From 95d701818b6342ef317f9d78cb046482bb35648e Mon Sep 17 00:00:00 2001 From: StarryFolf Date: Tue, 11 Jul 2023 15:32:46 +0700 Subject: [PATCH 1/2] fix: regex validation and change command name. --- src/Api/Controllers/EntriesController.cs | 8 ++++---- .../Commands/{UploadDigitalFile.cs => UploadEntry.cs} | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) rename src/Application/Entries/Commands/{UploadDigitalFile.cs => UploadEntry.cs} (94%) diff --git a/src/Api/Controllers/EntriesController.cs b/src/Api/Controllers/EntriesController.cs index 12ea545d..afed86a0 100644 --- a/src/Api/Controllers/EntriesController.cs +++ b/src/Api/Controllers/EntriesController.cs @@ -29,7 +29,7 @@ public EntriesController(ICurrentUserService currentUserService) /// [RequiresRole(IdentityData.Roles.Staff, IdentityData.Roles.Employee)] [HttpPost] - public async Task>> UploadDigitalFile( + public async Task>> UploadEntry( [FromForm] UploadDigitalFileRequest request) { var currentUser = _currentUserService.GetCurrentUser(); @@ -50,11 +50,11 @@ public async Task>> UploadDigitalFile( }); } - UploadDigitalFile.Command command; + UploadEntry.Command command; if (request.IsDirectory) { - command = new UploadDigitalFile.Command() + command = new UploadEntry.Command() { CurrentUser = currentUser, Path = request.Path, @@ -72,7 +72,7 @@ public async Task>> UploadDigitalFile( var lastDotIndex = request.File.FileName.LastIndexOf(".", StringComparison.Ordinal); var extension = request.File.FileName.Substring(lastDotIndex + 1, request.File.FileName.Length - lastDotIndex - 1); - command = new UploadDigitalFile.Command() + command = new UploadEntry.Command() { CurrentUser = currentUser, Path = request.Path, diff --git a/src/Application/Entries/Commands/UploadDigitalFile.cs b/src/Application/Entries/Commands/UploadEntry.cs similarity index 94% rename from src/Application/Entries/Commands/UploadDigitalFile.cs rename to src/Application/Entries/Commands/UploadEntry.cs index eb6107c7..01694e20 100644 --- a/src/Application/Entries/Commands/UploadDigitalFile.cs +++ b/src/Application/Entries/Commands/UploadEntry.cs @@ -14,7 +14,7 @@ namespace Application.Entries.Commands; -public class UploadDigitalFile { +public class UploadEntry { public class Validator : AbstractValidator { public Validator() @@ -26,7 +26,7 @@ public Validator() RuleFor(x => x.Path) .NotEmpty().WithMessage("File's path is required.") - .Matches("^(/(?!/)[a-z_.\\-0-9]*)+(? private readonly IApplicationDbContext _context; private readonly IMapper _mapper; private readonly IDateTimeProvider _dateTimeProvider; - private readonly ILogger _logger; + private readonly ILogger _logger; - public Handler(IApplicationDbContext context, IMapper mapper, IDateTimeProvider dateTimeProvider, ILogger logger) + public Handler(IApplicationDbContext context, IMapper mapper, IDateTimeProvider dateTimeProvider, ILogger logger) { _context = context; _mapper = mapper; From 1c6d53d5a49793200e68c0b31888334bd21e382a Mon Sep 17 00:00:00 2001 From: StarryFolf Date: Tue, 11 Jul 2023 16:20:28 +0700 Subject: [PATCH 2/2] refactoring --- src/Api/Controllers/EntriesController.cs | 6 +++--- src/Api/Controllers/SharedController.cs | 6 +++--- .../Commands/{UploadEntry.cs => CreateEntry.cs} | 11 ++++++----- .../{UploadSharedEntry.cs => CreateSharedEntry.cs} | 8 ++++---- src/Application/Entries/Commands/UpdateEntry.cs | 13 +++++++++++++ src/Application/Entries/EntryLogExtension.cs | 4 ++-- .../Entries/Queries/GetAllEntriesPaginated.cs | 2 +- 7 files changed, 32 insertions(+), 18 deletions(-) rename src/Application/Entries/Commands/{UploadEntry.cs => CreateEntry.cs} (92%) rename src/Application/Entries/Commands/{UploadSharedEntry.cs => CreateSharedEntry.cs} (95%) diff --git a/src/Api/Controllers/EntriesController.cs b/src/Api/Controllers/EntriesController.cs index afed86a0..53556d74 100644 --- a/src/Api/Controllers/EntriesController.cs +++ b/src/Api/Controllers/EntriesController.cs @@ -50,11 +50,11 @@ public async Task>> UploadEntry( }); } - UploadEntry.Command command; + CreateEntry.Command command; if (request.IsDirectory) { - command = new UploadEntry.Command() + command = new CreateEntry.Command() { CurrentUser = currentUser, Path = request.Path, @@ -72,7 +72,7 @@ public async Task>> UploadEntry( var lastDotIndex = request.File.FileName.LastIndexOf(".", StringComparison.Ordinal); var extension = request.File.FileName.Substring(lastDotIndex + 1, request.File.FileName.Length - lastDotIndex - 1); - command = new UploadEntry.Command() + command = new CreateEntry.Command() { CurrentUser = currentUser, Path = request.Path, diff --git a/src/Api/Controllers/SharedController.cs b/src/Api/Controllers/SharedController.cs index 2c10fb15..4e514ed3 100644 --- a/src/Api/Controllers/SharedController.cs +++ b/src/Api/Controllers/SharedController.cs @@ -99,11 +99,11 @@ public async Task>> UploadSharedEntry([ }); } - UploadSharedEntry.Command command; + CreateSharedEntry.Command command; if (request.IsDirectory) { - command = new UploadSharedEntry.Command() + command = new CreateSharedEntry.Command() { Name = request.Name, CurrentUser = currentUser, @@ -121,7 +121,7 @@ public async Task>> UploadSharedEntry([ var lastDotIndex = request.File.FileName.LastIndexOf(".", StringComparison.Ordinal); var extension = request.File.FileName.Substring(lastDotIndex + 1, request.File.FileName.Length - lastDotIndex - 1); - command = new UploadSharedEntry.Command() + command = new CreateSharedEntry.Command() { CurrentUser = currentUser, EntryId = entryId, diff --git a/src/Application/Entries/Commands/UploadEntry.cs b/src/Application/Entries/Commands/CreateEntry.cs similarity index 92% rename from src/Application/Entries/Commands/UploadEntry.cs rename to src/Application/Entries/Commands/CreateEntry.cs index 01694e20..9de0cac3 100644 --- a/src/Application/Entries/Commands/UploadEntry.cs +++ b/src/Application/Entries/Commands/CreateEntry.cs @@ -14,7 +14,7 @@ namespace Application.Entries.Commands; -public class UploadEntry { +public class CreateEntry { public class Validator : AbstractValidator { public Validator() @@ -22,10 +22,11 @@ public Validator() RuleLevelCascadeMode = CascadeMode.Stop; RuleFor(x => x.Name) + .NotEmpty().WithMessage("Entry's name is required.") .MaximumLength(256).WithMessage("Name cannot exceed 256 characters."); RuleFor(x => x.Path) - .NotEmpty().WithMessage("File's path is required.") + .NotEmpty().WithMessage("Entry's path is required.") .Matches("^(/(?!/)[a-z_.\\s\\-0-9]*)+(? private readonly IApplicationDbContext _context; private readonly IMapper _mapper; private readonly IDateTimeProvider _dateTimeProvider; - private readonly ILogger _logger; + private readonly ILogger _logger; - public Handler(IApplicationDbContext context, IMapper mapper, IDateTimeProvider dateTimeProvider, ILogger logger) + public Handler(IApplicationDbContext context, IMapper mapper, IDateTimeProvider dateTimeProvider, ILogger logger) { _context = context; _mapper = mapper; @@ -122,7 +123,7 @@ public async Task Handle(Command request, CancellationToken cancellati await _context.SaveChangesAsync(cancellationToken); using (Logging.PushProperties(nameof(Entry), entryEntity.Id, request.CurrentUser.Id)) { - _logger.LogUploadDigitalFile(request.CurrentUser.Id.ToString(), entryEntity.Id.ToString()); + _logger.LogCreateEntry(request.CurrentUser.Id.ToString(), entryEntity.Id.ToString()); } return _mapper.Map(result.Entity); } diff --git a/src/Application/Entries/Commands/UploadSharedEntry.cs b/src/Application/Entries/Commands/CreateSharedEntry.cs similarity index 95% rename from src/Application/Entries/Commands/UploadSharedEntry.cs rename to src/Application/Entries/Commands/CreateSharedEntry.cs index 9e06a5f0..acdb78b3 100644 --- a/src/Application/Entries/Commands/UploadSharedEntry.cs +++ b/src/Application/Entries/Commands/CreateSharedEntry.cs @@ -14,7 +14,7 @@ namespace Application.Entries.Commands; -public class UploadSharedEntry +public class CreateSharedEntry { public class Validator : AbstractValidator { @@ -42,8 +42,8 @@ public class CommandHandler : IRequestHandler private readonly IApplicationDbContext _context; private readonly IMapper _mapper; private readonly IDateTimeProvider _dateTimeProvider; - private readonly ILogger _logger; - public CommandHandler(IApplicationDbContext context, IMapper mapper, IDateTimeProvider dateTimeProvider, ILogger logger) + private readonly ILogger _logger; + public CommandHandler(IApplicationDbContext context, IMapper mapper, IDateTimeProvider dateTimeProvider, ILogger logger) { _context = context; _mapper = mapper; @@ -129,7 +129,7 @@ public async Task Handle(Command request, CancellationToken cancellati await _context.SaveChangesAsync(cancellationToken); using (Logging.PushProperties(nameof(Entry), entity.Id, request.CurrentUser.Id)) { - _logger.LogUploadSharedEntry(request.CurrentUser.Id.ToString(), entity.Id.ToString()); + _logger.LogCreateSharedEntry(request.CurrentUser.Id.ToString(), entity.Id.ToString()); } return _mapper.Map(result.Entity); } diff --git a/src/Application/Entries/Commands/UpdateEntry.cs b/src/Application/Entries/Commands/UpdateEntry.cs index 9341746f..b8318982 100644 --- a/src/Application/Entries/Commands/UpdateEntry.cs +++ b/src/Application/Entries/Commands/UpdateEntry.cs @@ -5,6 +5,7 @@ using Application.Common.Models.Operations; using AutoMapper; using Domain.Entities.Digital; +using FluentValidation; using MediatR; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; @@ -14,6 +15,18 @@ namespace Application.Entries.Commands; public class UpdateEntry { + public class Validator : AbstractValidator + { + public Validator() + { + RuleLevelCascadeMode = CascadeMode.Stop; + + RuleFor(x => x.Name) + .NotEmpty().WithMessage("Entry's name is required.") + .MaximumLength(256).WithMessage("Name cannot exceed 256 characters."); + } + } + public record Command : IRequest { public Guid CurrentUserId { get; init; } diff --git a/src/Application/Entries/EntryLogExtension.cs b/src/Application/Entries/EntryLogExtension.cs index 28e4d2c1..3605ad4d 100644 --- a/src/Application/Entries/EntryLogExtension.cs +++ b/src/Application/Entries/EntryLogExtension.cs @@ -19,10 +19,10 @@ public static partial class EntryLogExtension public static partial void LogUpdateEntry(this ILogger logger, string userId, string entryId); [LoggerMessage(Level = LogLevel.Information, Message = EntryLogMessages.UploadDigitalFile, EventId = EventId.Add)] - public static partial void LogUploadDigitalFile(this ILogger logger, string userId, string entryId); + public static partial void LogCreateEntry(this ILogger logger, string userId, string entryId); [LoggerMessage(Level = LogLevel.Information, Message = EntryLogMessages.UploadSharedEntry, EventId = EventId.Add)] - public static partial void LogUploadSharedEntry(this ILogger logger, string userId, string entryId); + public static partial void LogCreateSharedEntry(this ILogger logger, string userId, string entryId); [LoggerMessage(Level = LogLevel.Information, Message = EntryLogMessages.DeleteBinEntry, EventId = EventId.Remove)] public static partial void LogDeleteBinEntry(this ILogger logger, string userId, string entryId); diff --git a/src/Application/Entries/Queries/GetAllEntriesPaginated.cs b/src/Application/Entries/Queries/GetAllEntriesPaginated.cs index 1efa66fc..0c345df2 100644 --- a/src/Application/Entries/Queries/GetAllEntriesPaginated.cs +++ b/src/Application/Entries/Queries/GetAllEntriesPaginated.cs @@ -19,7 +19,7 @@ public Validator() RuleFor(x => x.EntryPath) .NotEmpty().WithMessage("File's path is required.") - .Matches("^(/(?!/)[a-z_.\\-0-9]*)+(?