From aadbcb78bef7e5fb1166996a33de88306be05cc7 Mon Sep 17 00:00:00 2001 From: kaitoz11 <43519768+kaitoz11@users.noreply.github.com> Date: Wed, 14 Jun 2023 00:27:39 +0700 Subject: [PATCH] feat: get user log by id + get user logs paginated --- .../GetAllLogsPaginatedQueryParameters.cs | 6 ++ src/Api/Controllers/UsersController.cs | 46 +++++++++++++ .../Common/Models/Dtos/Logging/UserLogDto.cs | 25 +++++++ .../Users/Queries/GetAllUserLogsPaginated.cs | 66 +++++++++++++++++++ .../Users/Queries/GetUserLogById.cs | 43 ++++++++++++ 5 files changed, 186 insertions(+) create mode 100644 src/Api/Controllers/Payload/Requests/GetAllLogsPaginatedQueryParameters.cs create mode 100644 src/Application/Common/Models/Dtos/Logging/UserLogDto.cs create mode 100644 src/Application/Users/Queries/GetAllUserLogsPaginated.cs create mode 100644 src/Application/Users/Queries/GetUserLogById.cs diff --git a/src/Api/Controllers/Payload/Requests/GetAllLogsPaginatedQueryParameters.cs b/src/Api/Controllers/Payload/Requests/GetAllLogsPaginatedQueryParameters.cs new file mode 100644 index 00000000..1f2bf8b6 --- /dev/null +++ b/src/Api/Controllers/Payload/Requests/GetAllLogsPaginatedQueryParameters.cs @@ -0,0 +1,6 @@ +namespace Api.Controllers.Payload.Requests; + +public class GetAllLogsPaginatedQueryParameters : PaginatedQueryParameters +{ + public string? SearchTerm { get; set; } +} \ No newline at end of file diff --git a/src/Api/Controllers/UsersController.cs b/src/Api/Controllers/UsersController.cs index 095e9605..795769bc 100644 --- a/src/Api/Controllers/UsersController.cs +++ b/src/Api/Controllers/UsersController.cs @@ -1,6 +1,8 @@ +using Api.Controllers.Payload.Requests; using Api.Controllers.Payload.Requests.Users; using Application.Common.Interfaces; using Application.Common.Models; +using Application.Common.Models.Dtos.Logging; using Application.Identity; using Application.Users.Commands; using Application.Users.Queries; @@ -160,4 +162,48 @@ public async Task>> Update([FromRoute] Guid userId, var result = await Mediator.Send(command); return Ok(Result.Succeed(result)); } + + /// + /// Get all user related logs paginated + /// + /// Get all users related logs query parameters + /// A paginated list of UserLogDto + [RequiresRole(IdentityData.Roles.Admin)] + [HttpGet("logs")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status403Forbidden)] + public async Task>>> GetAllUserLogs( + [FromQuery] GetAllLogsPaginatedQueryParameters queryParameters) + { + var query = new GetAllUserLogsPaginated.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 user related log by Id + /// + /// Id of the logged user + /// UserLogDto of the logged user + [RequiresRole(IdentityData.Roles.Admin)] + [HttpGet("log/{logId:guid}")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status403Forbidden)] + public async Task>> GetUserLogById([FromRoute] Guid logId) + { + var query = new GetUserLogById.Query() + { + LogId = logId + }; + + var result = await Mediator.Send(query); + return Ok(Result.Succeed(result)); + } } diff --git a/src/Application/Common/Models/Dtos/Logging/UserLogDto.cs b/src/Application/Common/Models/Dtos/Logging/UserLogDto.cs new file mode 100644 index 00000000..e20ba4e3 --- /dev/null +++ b/src/Application/Common/Models/Dtos/Logging/UserLogDto.cs @@ -0,0 +1,25 @@ +using Application.Common.Mappings; +using Application.Users.Queries; +using AutoMapper; +using Domain.Entities.Logging; + +namespace Application.Common.Models.Dtos.Logging; + +public class UserLogDto : IMapFrom +{ + public Guid Id { get; set; } + public string Action { get; set; } = null!; + public UserDto? 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/Users/Queries/GetAllUserLogsPaginated.cs b/src/Application/Users/Queries/GetAllUserLogsPaginated.cs new file mode 100644 index 00000000..67528d67 --- /dev/null +++ b/src/Application/Users/Queries/GetAllUserLogsPaginated.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.Users.Queries; + +public class GetAllUserLogsPaginated +{ + 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.UserLogs + .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(UserLogDto.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/Users/Queries/GetUserLogById.cs b/src/Application/Users/Queries/GetUserLogById.cs new file mode 100644 index 00000000..7e561ae7 --- /dev/null +++ b/src/Application/Users/Queries/GetUserLogById.cs @@ -0,0 +1,43 @@ +using Application.Common.Interfaces; +using Application.Common.Models.Dtos.Logging; +using AutoMapper; +using MediatR; +using Microsoft.EntityFrameworkCore; + +namespace Application.Users.Queries; + +public class GetUserLogById +{ + 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.UserLogs + .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