Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
akbaramd committed Dec 29, 2024
1 parent 0211555 commit 9291575
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 2 deletions.
24 changes: 23 additions & 1 deletion Nezam.EES.Slice.Secretariat/Application/Dto/DocumentDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class DocumentDto
public DocumentStatus Status { get; set; }
public ParticipantDto Owner { get; set; }
public ParticipantDto Receiver { get; set; }
public List<DocumentAttachmentDto> Attachments { get; set; } = new();

// Method to map from Document entity
public static DocumentDto FromEntity(DocumentAggregateRoot document)
Expand All @@ -26,7 +27,28 @@ public static DocumentDto FromEntity(DocumentAggregateRoot document)
Type = document.Type,
Status = document.Status,
Owner = ParticipantDto.FromEntity(document.OwnerParticipant),
Receiver = ParticipantDto.FromEntity(document.ReceiverParticipant)
Receiver = ParticipantDto.FromEntity(document.ReceiverParticipant),
Attachments = document.Attachments.Select(DocumentAttachmentDto.FromEntity).ToList()
};
}
}

public class DocumentAttachmentDto
{
public string FileName { get; set; }
public string FileType { get; set; }
public long FileSize { get; set; }
public string FilePath { get; set; }

// Method to map from DocumentAttachmentEntity
public static DocumentAttachmentDto FromEntity(DocumentAttachmentEntity attachment)
{
return new DocumentAttachmentDto
{
FileName = attachment.FileName,
FileType = attachment.FileType,
FileSize = attachment.FileSize,
FilePath = attachment.FilePath
};
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using FastEndpoints;
using Microsoft.AspNetCore.Http;
using Nezam.EES.Slice.Secretariat.Domains.Documents;
using Nezam.EES.Slice.Secretariat.Domains.Documents.ValueObjects;
using Payeh.SharedKernel.Exceptions;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using FluentValidation;
using Nezam.EES.Slice.Secretariat.Domains.Documents.Repositories;
using Payeh.SharedKernel.UnitOfWork;

[AllowFileUploads]
public class AttachToDocumentEndpoint : EndpointWithoutRequest
{
private readonly IDocumentRepository _documentRepository;
private readonly IWebHostEnvironment _webHostEnvironment;
private readonly IUnitOfWorkManager _unitOfWorkManager;

public AttachToDocumentEndpoint(IDocumentRepository documentRepository, IWebHostEnvironment webHostEnvironment, IUnitOfWorkManager unitOfWorkManager)
{
_documentRepository = documentRepository;
_webHostEnvironment = webHostEnvironment;
_unitOfWorkManager = unitOfWorkManager;
}

public override void Configure()
{
Post("/documents/{DocumentId}/attach-file");
AllowFileUploads();
}

public override async Task HandleAsync(CancellationToken ct)
{
using var uow = _unitOfWorkManager.Begin();

// Validate Document ID from route
var documentIdStr = Route<string>("DocumentId");
var documentId = DocumentId.NewId(Guid.Parse(documentIdStr ?? throw new InvalidOperationException()));

// Fetch the document
var document = await _documentRepository.FindOneAsync(x => x.DocumentId == documentId);
if (document == null)
{
await SendNotFoundAsync(ct);
return;
}

// Validate files
if (Files.Count == 0 || Files[0].Length == 0)
throw new ValidationException("No file provided or file is empty.");

var file = Files[0];

// Save file to wwwroot/uploads
var uploadsFolder = Path.Combine(_webHostEnvironment.WebRootPath, "uploads");
if (!Directory.Exists(uploadsFolder))
{
Directory.CreateDirectory(uploadsFolder);
}

var fileName = Path.GetRandomFileName();
var filePath = Path.Combine(uploadsFolder, fileName);

await using (var stream = new FileStream(filePath, FileMode.Create))
{
await file.CopyToAsync(stream, ct);
}

// Attach file to document
document.AddAttachment(
file.FileName,
file.ContentType,
file.Length,
filePath
);

// Save changes
await _documentRepository.UpdateAsync(document, true);
await uow.CommitAsync(ct);
await SendOkAsync(new { Message = "File uploaded and attached successfully.", FilePath = filePath }, ct);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public override async Task HandleAsync(GetMyDocumentsRequest req, CancellationTo
// Query documents with pagination
var query = _dbContext.Documents
.AsNoTracking()
.Include(d => d.Attachments)
.Include(d => d.OwnerParticipant)
.Include(d => d.ReceiverParticipant)
.Include(d => d.Referrals)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public void RevertToDraft(UserId UserId)
AddDomainEvent(new DocumentRevertedToDraftEvent(this.DocumentId));
}

public void AddAttachment(string fileName, string fileType, long fileSize, string filePath, UserId UserId)
public void AddAttachment(string fileName, string fileType, long fileSize, string filePath)
{
EnsureNotArchived();
if (string.IsNullOrWhiteSpace(fileName)) throw new PayehException("File name cannot be empty.");
Expand Down

0 comments on commit 9291575

Please sign in to comment.