Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion src/Api/Controllers/DocumentsController.cs
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
using Api.Controllers.Payload.Requests;
using Api.Controllers.Payload.Requests.Documents;
using Application.Common.Interfaces;
using Application.Common.Models;
using Application.Common.Models.Dtos;
using Application.Common.Models.Dtos.ImportDocument;
using Application.Common.Models.Dtos.Logging;
using Application.Common.Models.Dtos.Physical;
using Application.Documents.Commands;
using Application.Documents.Queries;
Expand DownExpand Up@@ -72,6 +74,26 @@ public async Task<ActionResult<Result<PaginatedList<IssuedDocumentDto>>>> GetAll
var result = await Mediator.Send(query);
return Ok(Result<PaginatedList<IssuedDocumentDto>>.Succeed(result));
}

/// <summary>
/// Get a document log by Id
/// </summary>
/// <param name="logId"></param>
/// <returns>Return a DocumentLogDto</returns>
[RequiresRole(IdentityData.Roles.Admin)]
[HttpGet("log/{logId:guid}")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<ActionResult<Result<DocumentLogDto>>> GetLogById([FromRoute] Guid logId)
{
var query = new GetLogOfDocumentById.Query()
{
LogId = logId
};

var result = await Mediator.Send(query);
return Ok(Result<DocumentLogDto>.Succeed(result));
}

/// <summary>
/// Get all documents paginated
Expand DownExpand Up@@ -101,7 +123,30 @@ public async Task<ActionResult<Result<PaginatedList<DocumentDto>>>> GetAllForAdm
var result = await Mediator.Send(query);
return Ok(Result<PaginatedList<DocumentDto>>.Succeed(result));
}


/// <summary>
/// Get all log of document
/// </summary>
/// <param name="queryParameters"></param>
/// <returns>Paginated list of DocumentLogDto</returns>
[RequiresRole(IdentityData.Roles.Admin)]
[HttpGet("logs")]
[ProducesResponseType(StatusCodes.Status200OK)]
public async Task<ActionResult<Result<PaginatedList<DocumentLogDto>>>> GetAllLogsPaginated(
[FromQuery] GetAllLogsPaginatedQueryParameters queryParameters)
{
var query = new GetAllDocumentLogsPaginated.Query()
{
SearchTerm = queryParameters.SearchTerm,
Page = queryParameters.Page,
Size = queryParameters.Size,
SortBy = queryParameters.SortBy,
SortOrder = queryParameters.SortOrder,
};
var result = await Mediator.Send(query);
return Ok(Result<PaginatedList<DocumentLogDto>>.Succeed(result));
}

/// <summary>
/// Get all documents for staff paginated
/// </summary>
Expand Down
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
namespace Api.Controllers.Payload.Requests;

/// <summary>
/// get all logs paginated
/// </summary>
public class GetAllLogsPaginatedQueryParameters : PaginatedQueryParameters
{
public string? SearchTerm { get; set; }
}
28 changes: 28 additions & 0 deletions src/Application/Common/Models/Dtos/Logging/DocumentLogDto.cs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
using Application.Common.Mappings;
using Application.Common.Models.Dtos.Physical;
using Application.Users.Queries;
using AutoMapper;
using Domain.Entities.Logging;

namespace Application.Common.Models.Dtos.Logging;

public class DocumentLogDto : IMapFrom<DocumentLog>
{
public Guid Id { get; set; }
public Guid UserId { get; set; }
public string Action { get; set; }
public DocumentDto? Object { get; set; }
public DateTime Time { get; set; }
public UserDto User { get; set; }

public void Mapping(Profile profile)
{

profile.CreateMap<DocumentLog, DocumentLogDto>()
.ForMember(dest => dest.Time,
opt => opt.MapFrom(src => src.Time.ToDateTimeUnspecified()))
.ForMember(dest => dest.Object,
opt => opt.MapFrom(src => src.Object));

}
}
67 changes: 67 additions & 0 deletions src/Application/Documents/Queries/GetAllDocumentLogsPaginated.cs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
using Application.Common.Extensions;
using Application.Common.Interfaces;
using Application.Common.Models;
using Application.Common.Models.Dtos.Logging;
using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace Application.Documents.Queries;

public class GetAllDocumentLogsPaginated
{
public record Query : IRequest<PaginatedList<DocumentLogDto>>
{
public string? SearchTerm { get; init; }
public int? Page { get; init; }
public int? Size { get; init; }
public string? SortBy { get; init; }
public string? SortOrder { get; init; }
}

public class QueryHandler : IRequestHandler<Query, PaginatedList<DocumentLogDto>>
{
private readonly IApplicationDbContext _context;
private readonly IMapper _mapper;

public QueryHandler(IApplicationDbContext context, IMapper mapper)
{
_context = context;
_mapper = mapper;
}

public async Task<PaginatedList<DocumentLogDto>> Handle(Query request, CancellationToken cancellationToken)
{
var logs = _context.DocumentLogs
.Include(x => x.Object)
.Include(x => x.User)
.ThenInclude(x => x.Department)
.AsQueryable();

if (!(request.SearchTerm is null || request.SearchTerm.Trim().Equals(string.Empty)))
{
logs = logs.Where(x =>
x.Action.ToLower().Contains(request.SearchTerm.ToLower()));
}

var sortBy = request.SortBy;
if (sortBy is null || !sortBy.MatchesPropertyName<DocumentLogDto>())
{
sortBy = nameof(DocumentLogDto.Time);
}
var sortOrder = request.SortOrder ?? "desc";
var pageNumber = request.Page is null or <= 0 ? 1 : request.Page;
var sizeNumber = request.Size is null or <= 0 ? 5 : request.Size;

var count = await logs.CountAsync(cancellationToken);
var list = await logs
.OrderByCustom(sortBy, sortOrder)
.Paginate(pageNumber.Value, sizeNumber.Value)
.ToListAsync(cancellationToken);

var result = _mapper.Map<List<DocumentLogDto>>(list);

return new PaginatedList<DocumentLogDto>(result, count, pageNumber.Value, sizeNumber.Value);
}
}
}
43 changes: 43 additions & 0 deletions src/Application/Documents/Queries/GetLogOfDocumentById.cs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
using Application.Common.Interfaces;
using Application.Common.Models.Dtos.Logging;
using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace Application.Documents.Queries;

public class GetLogOfDocumentById
{
public record Query : IRequest<DocumentLogDto>
{
public Guid LogId { get; init; }
}

public class QueryHandler : IRequestHandler<Query, DocumentLogDto>
{
private readonly IApplicationDbContext _context;
private readonly IMapper _mapper;

public QueryHandler(IApplicationDbContext context, IMapper mapper)
{
_context = context;
_mapper = mapper;
}

public async Task<DocumentLogDto> Handle(Query request, CancellationToken cancellationToken)
{
var log = await _context.DocumentLogs
.Include(x => x.Object)
.Include(x => x.User)
.ThenInclude(x => x.Department)
.FirstOrDefaultAsync(x => x.Id.Equals(request.LogId), cancellationToken);

if (log is null)
{
throw new KeyNotFoundException("Log does not exist.");
}

return _mapper.Map<DocumentLogDto>(log);
}
}
}