diff --git a/src/Application/Documents/Commands/UploadFileToDocument.cs b/src/Application/Documents/Commands/UploadFileToDocument.cs index 989114a1..6690469d 100644 --- a/src/Application/Documents/Commands/UploadFileToDocument.cs +++ b/src/Application/Documents/Commands/UploadFileToDocument.cs @@ -1,3 +1,4 @@ +using Application.Common.Exceptions; using Application.Common.Extensions; using Application.Common.Interfaces; using Application.Common.Models.Dtos.Physical; @@ -5,6 +6,7 @@ using Domain.Entities; using Domain.Entities.Digital; using Domain.Entities.Physical; +using Domain.Statuses; using MediatR; using Microsoft.EntityFrameworkCore; @@ -62,6 +64,11 @@ public async Task Handle(Command request, CancellationToken cancell _context.Files.Remove(document.File); } + if (document.Status is DocumentStatus.Issued or DocumentStatus.Lost ) + { + throw new ConflictException("This document cannot be uploaded to."); + } + var file = new FileEntity { FileData = request.FileData.ToArray(), diff --git a/src/Application/Entries/Commands/CreateEntry.cs b/src/Application/Entries/Commands/CreateEntry.cs index 1a9ddb4c..3f485f18 100644 --- a/src/Application/Entries/Commands/CreateEntry.cs +++ b/src/Application/Entries/Commands/CreateEntry.cs @@ -17,13 +17,33 @@ namespace Application.Entries.Commands; public class CreateEntry { public class Validator : AbstractValidator { + private readonly string[] _allowedExtensions = + { + "pdf", + "doc", + "docx", + "xls", + "xlsx", + "ppt", + "pptx", + }; public Validator() { RuleLevelCascadeMode = CascadeMode.Stop; + RuleFor(x => x.FileExtension) + .Must(extension => + { + if (!_allowedExtensions.Contains(extension)) + { + throw new ConflictException("This file is not supported."); + } + return true; + }); + RuleFor(x => x.Name) .NotEmpty().WithMessage("Entry's name is required.") - .Matches("^[\\p{L}A-Za-z_.\\s\\-0-9]*$").WithMessage("Invalid name format.") + .Matches("^[\\p{L}A-Za-z()_.\\s\\-0-9]*$").WithMessage("Invalid name format.") .MaximumLength(256).WithMessage("Name cannot exceed 256 characters."); RuleFor(x => x.Path) @@ -104,9 +124,6 @@ public async Task Handle(Command request, CancellationToken cancellati throw new ConflictException("File size must be lower than 20MB"); } - var lastDotIndex = request.Name.LastIndexOf(".", StringComparison.Ordinal); - var fileExtension = request.Name.Substring(lastDotIndex + 1, request.Name.Length - lastDotIndex - 1); - var fileEntity = new FileEntity() { FileData = request.FileData.ToArray(), diff --git a/src/Application/Entries/Queries/GetAllEntriesPaginated.cs b/src/Application/Entries/Queries/GetAllEntriesPaginated.cs index ef0cf431..6eb64ff8 100644 --- a/src/Application/Entries/Queries/GetAllEntriesPaginated.cs +++ b/src/Application/Entries/Queries/GetAllEntriesPaginated.cs @@ -48,6 +48,7 @@ public QueryHandler(IApplicationDbContext context, IMapper mapper) public async Task> Handle(Query request, CancellationToken cancellationToken) { var entries = _context.Entries + .Include(x => x.File) .Where(x => x.Path.Equals(request.EntryPath) && x.OwnerId == request.CurrentUser.Id);