diff --git a/src/Api/Controllers/DocumentsController.cs b/src/Api/Controllers/DocumentsController.cs index 2a6b6036..57aea634 100644 --- a/src/Api/Controllers/DocumentsController.cs +++ b/src/Api/Controllers/DocumentsController.cs @@ -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; @@ -72,6 +74,26 @@ public async Task>>> GetAll var result = await Mediator.Send(query); return Ok(Result>.Succeed(result)); } + + /// + /// Get a document log by Id + /// + /// + /// Return a DocumentLogDto + [RequiresRole(IdentityData.Roles.Admin)] + [HttpGet("log/{logId:guid}")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + public async Task>> GetLogById([FromRoute] Guid logId) + { + var query = new GetLogOfDocumentById.Query() + { + LogId = logId + }; + + var result = await Mediator.Send(query); + return Ok(Result.Succeed(result)); + } /// /// Get all documents paginated @@ -101,7 +123,30 @@ public async Task>>> GetAllForAdm var result = await Mediator.Send(query); return Ok(Result>.Succeed(result)); } - + + /// + /// Get all log of document + /// + /// + /// Paginated list of DocumentLogDto + [RequiresRole(IdentityData.Roles.Admin)] + [HttpGet("logs")] + [ProducesResponseType(StatusCodes.Status200OK)] + public async Task>>> 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>.Succeed(result)); + } + /// /// Get all documents for staff paginated /// diff --git a/src/Api/Controllers/Payload/Requests/GetAllLogsPaginatedQueryParameters.cs b/src/Api/Controllers/Payload/Requests/GetAllLogsPaginatedQueryParameters.cs new file mode 100644 index 00000000..eca75ee0 --- /dev/null +++ b/src/Api/Controllers/Payload/Requests/GetAllLogsPaginatedQueryParameters.cs @@ -0,0 +1,9 @@ +namespace Api.Controllers.Payload.Requests; + +/// +/// get all logs paginated +/// +public class GetAllLogsPaginatedQueryParameters : PaginatedQueryParameters +{ + public string? SearchTerm { get; set; } +} \ No newline at end of file diff --git a/src/Application/Common/Models/Dtos/Logging/DocumentLogDto.cs b/src/Application/Common/Models/Dtos/Logging/DocumentLogDto.cs new file mode 100644 index 00000000..c998ccf4 --- /dev/null +++ b/src/Application/Common/Models/Dtos/Logging/DocumentLogDto.cs @@ -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 +{ + 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() + .ForMember(dest => dest.Time, + opt => opt.MapFrom(src => src.Time.ToDateTimeUnspecified())) + .ForMember(dest => dest.Object, + opt => opt.MapFrom(src => src.Object)); + + } +} \ No newline at end of file diff --git a/src/Application/Documents/Queries/GetAllDocumentLogsPaginated.cs b/src/Application/Documents/Queries/GetAllDocumentLogsPaginated.cs new file mode 100644 index 00000000..a0bf18ae --- /dev/null +++ b/src/Application/Documents/Queries/GetAllDocumentLogsPaginated.cs @@ -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> + { + 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> + { + private readonly IApplicationDbContext _context; + private readonly IMapper _mapper; + + public QueryHandler(IApplicationDbContext context, IMapper mapper) + { + _context = context; + _mapper = mapper; + } + + public async Task> 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()) + { + 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); + + return new PaginatedList(result, count, pageNumber.Value, sizeNumber.Value); + } + } +} \ No newline at end of file diff --git a/src/Application/Documents/Queries/GetLogOfDocumentById.cs b/src/Application/Documents/Queries/GetLogOfDocumentById.cs new file mode 100644 index 00000000..2a6fd6a2 --- /dev/null +++ b/src/Application/Documents/Queries/GetLogOfDocumentById.cs @@ -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 + { + public Guid LogId { get; init; } + } + + public class QueryHandler : IRequestHandler + { + private readonly IApplicationDbContext _context; + private readonly IMapper _mapper; + + public QueryHandler(IApplicationDbContext context, IMapper mapper) + { + _context = context; + _mapper = mapper; + } + + public async Task 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(log); + } + } +} \ No newline at end of file