-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
456 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
...t/Application/UseCases/Documents/GetDocumentAttachments/GetDocumentAttachmentsEndpoint.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
using System.Security.Claims; | ||
using FastEndpoints; | ||
using Microsoft.EntityFrameworkCore; | ||
using Nezam.EEs.Shared.Domain.Identity.User; | ||
using Nezam.EES.Slice.Secretariat.Application.Dto; | ||
using Nezam.EES.Slice.Secretariat.Domains.Documents.ValueObjects; | ||
using Nezam.EES.Slice.Secretariat.Infrastructure.EntityFrameworkCore; | ||
|
||
namespace Nezam.EES.Slice.Secretariat.Application.UseCases.Documents.GetDocumentAttachments | ||
{ | ||
public class GetDocumentAttachmentsRequest | ||
{ | ||
public Guid DocumentId { get; set; } | ||
} | ||
|
||
public class GetDocumentAttachmentsResponse | ||
{ | ||
public List<DocumentAttachmentDto> Attachments { get; set; } | ||
} | ||
|
||
|
||
|
||
public class GetDocumentAttachmentsEndpoint : Endpoint<GetDocumentAttachmentsRequest, GetDocumentAttachmentsResponse> | ||
{ | ||
private readonly ISecretariatDbContext _dbContext; | ||
|
||
public GetDocumentAttachmentsEndpoint(ISecretariatDbContext dbContext) | ||
{ | ||
_dbContext = dbContext; | ||
} | ||
|
||
public override void Configure() | ||
{ | ||
Get("/api/documents/{documentId}/attachments"); | ||
// Note: The {documentId} part should map to the DocumentId parameter. | ||
} | ||
|
||
public override async Task HandleAsync(GetDocumentAttachmentsRequest req, CancellationToken ct) | ||
{ | ||
// Extract current user ID from claims | ||
var userId = GetCurrentUserId(); | ||
|
||
if (userId == null) | ||
{ | ||
ThrowError("User ID not found in claims."); | ||
} | ||
|
||
var documentId = DocumentId.NewId(req.DocumentId); | ||
|
||
// Query the document to ensure the user has access | ||
var document = await _dbContext.Documents | ||
.AsNoTracking() | ||
.Include(d => d.Attachments) | ||
.Where(d => d.DocumentId == documentId) | ||
.FirstOrDefaultAsync(ct); | ||
|
||
if (document == null) | ||
{ | ||
ThrowError("Document not found."); | ||
} | ||
|
||
// Ensure user is authorized to access this document (customize this check) | ||
var participantId = await _dbContext.Participants | ||
.Where(p => p.UserId == userId) | ||
.Select(p => p.ParticipantId) | ||
.FirstOrDefaultAsync(ct); | ||
|
||
if (participantId == null || | ||
(document.OwnerParticipantId != participantId && document.ReceiverParticipantId != participantId)) | ||
{ | ||
ThrowError("Unauthorized access to the document."); | ||
} | ||
|
||
// Map to the response DTO | ||
var attachments = document.Attachments.Select(DocumentAttachmentDto.FromEntity).ToList(); | ||
|
||
// Send the response with document attachments | ||
await SendOkAsync(new GetDocumentAttachmentsResponse { Attachments = attachments }, ct); | ||
} | ||
|
||
private UserId? GetCurrentUserId() | ||
{ | ||
// Retrieve user ID from claims | ||
var userIdClaim = | ||
User.Claims.FirstOrDefault(c => | ||
c.Type == ClaimTypes.NameIdentifier); // Adjust claim type as per your system | ||
return userIdClaim != null ? UserId.NewId(Guid.Parse(userIdClaim.Value)) : null; | ||
} | ||
} | ||
} |
Oops, something went wrong.