diff --git a/src/Api/Controllers/FoldersController.cs b/src/Api/Controllers/FoldersController.cs index 977d196b..5670a143 100644 --- a/src/Api/Controllers/FoldersController.cs +++ b/src/Api/Controllers/FoldersController.cs @@ -1,6 +1,8 @@ +using Api.Controllers.Payload.Requests; using Api.Controllers.Payload.Requests.Folders; using Application.Common.Interfaces; using Application.Common.Models; +using Application.Common.Models.Dtos.Logging; using Application.Common.Models.Dtos.Physical; using Application.Folders.Commands; using Application.Folders.Queries; @@ -183,4 +185,49 @@ public async Task>> Update([FromRoute] Guid folde var result = await Mediator.Send(command); return Ok(Result.Succeed(result)); } + + /// + /// + /// + /// + /// + [RequiresRole(IdentityData.Roles.Admin)] + [HttpGet("logs")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status403Forbidden)] + public async Task>>> GetAllFolderLogs( + [FromQuery] GetAllLogsPaginatedQueryParameters queryParameters) + { + var query = new GetAllFolderLogsPaginated.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)); + } + + /// + /// + /// + /// + /// + [RequiresRole(IdentityData.Roles.Admin)] + [HttpGet("log/{logId:guid}")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status403Forbidden)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + public async Task>> GetFolderLogById([FromRoute] Guid logId) + { + var query = new GetFolderLogById.Query() + { + LogId = logId + }; + + var result = await Mediator.Send(query); + return Ok(Result.Succeed(result)); + } } \ No newline at end of file diff --git a/src/Application/Common/Models/Dtos/Logging/FolderLogDto.cs b/src/Application/Common/Models/Dtos/Logging/FolderLogDto.cs new file mode 100644 index 00000000..67fe5e22 --- /dev/null +++ b/src/Application/Common/Models/Dtos/Logging/FolderLogDto.cs @@ -0,0 +1,25 @@ +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 FolderLogDto : IMapFrom +{ + public Guid Id { get; set; } + public string Action { get; set; } = null!; + public FolderDto? Object { get; set; } + public DateTime Time { get; set; } + public UserDto User { get; set; } = null!; + + 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/Folders/Queries/GetAllFolderLogsPaginated.cs b/src/Application/Folders/Queries/GetAllFolderLogsPaginated.cs new file mode 100644 index 00000000..d7a3240e --- /dev/null +++ b/src/Application/Folders/Queries/GetAllFolderLogsPaginated.cs @@ -0,0 +1,66 @@ +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.Folders.Queries; + +public class GetAllFolderLogsPaginated +{ + 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.FolderLogs + .Include(x => x.Object) + .AsQueryable(); + + if (!(request.SearchTerm is null || request.SearchTerm.Trim().Equals(string.Empty))) + { + logs = logs.Where(x => + x.Action.Trim().ToLower().Contains(request.SearchTerm.Trim().ToLower())); + } + + var sortBy = request.SortBy; + if (sortBy is null || !sortBy.MatchesPropertyName()) + { + sortBy = nameof(FolderLogDto.Time); + } + + var sortOrder = request.SortOrder ?? "dsc"; + 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 + .Paginate(pageNumber.Value, sizeNumber.Value) + .OrderByCustom(sortBy, sortOrder) + .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/Folders/Queries/GetFolderLogById.cs b/src/Application/Folders/Queries/GetFolderLogById.cs new file mode 100644 index 00000000..340ba6a3 --- /dev/null +++ b/src/Application/Folders/Queries/GetFolderLogById.cs @@ -0,0 +1,43 @@ +using Application.Common.Interfaces; +using Application.Common.Models.Dtos.Logging; +using AutoMapper; +using MediatR; +using Microsoft.EntityFrameworkCore; + +namespace Application.Folders.Queries; + +public class GetFolderLogById +{ + 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.FolderLogs + .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