From 231822fb8dab1c41e16b5776e8d7f870cb9f8e8a Mon Sep 17 00:00:00 2001 From: Vzart Date: Tue, 13 Jun 2023 23:44:21 +0700 Subject: [PATCH 1/2] feat: get room log by ID and logs paginated --- .../GetAllLogsPaginatedQueryParameters.cs | 9 +++ src/Api/Controllers/RoomsController.cs | 30 +++++++++ .../Common/Models/Dtos/Logging/RoomLogDto.cs | 28 ++++++++ .../Rooms/Queries/GetAllRoomLogsPaginated.cs | 67 +++++++++++++++++++ .../Rooms/Queries/GetLogOfRoomById.cs | 43 ++++++++++++ 5 files changed, 177 insertions(+) create mode 100644 src/Api/Controllers/Payload/Requests/GetAllLogsPaginatedQueryParameters.cs create mode 100644 src/Application/Common/Models/Dtos/Logging/RoomLogDto.cs create mode 100644 src/Application/Rooms/Queries/GetAllRoomLogsPaginated.cs create mode 100644 src/Application/Rooms/Queries/GetLogOfRoomById.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..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/Api/Controllers/RoomsController.cs b/src/Api/Controllers/RoomsController.cs index 53f111af..d92b58be 100644 --- a/src/Api/Controllers/RoomsController.cs +++ b/src/Api/Controllers/RoomsController.cs @@ -1,7 +1,9 @@ +using Api.Controllers.Payload.Requests; using Api.Controllers.Payload.Requests.Lockers; using Api.Controllers.Payload.Requests.Rooms; using Application.Common.Interfaces; using Application.Common.Models; +using Application.Common.Models.Dtos.Logging; using Application.Common.Models.Dtos.Physical; using Application.Identity; using Application.Rooms.Commands; @@ -63,6 +65,34 @@ public async Task>>> GetAllPaginated( return Ok(Result>.Succeed(result)); } + [HttpGet("log/{logId:guid}")] + public async Task>> GetLogById([FromRoute] Guid logId) + { + var query = new GetLogOfRoomById.Query() + { + LogId = logId + }; + + var result = await Mediator.Send(query); + return Ok(Result.Succeed(result)); + } + + [HttpGet("logs")] public async Task>>> GetAllLogsPaginated( + [FromQuery] GetAllLogsPaginatedQueryParameters queryParameters) + { + var query = new GetAllRoomLogsPaginated.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 empty containers in a room /// diff --git a/src/Application/Common/Models/Dtos/Logging/RoomLogDto.cs b/src/Application/Common/Models/Dtos/Logging/RoomLogDto.cs new file mode 100644 index 00000000..079c87ef --- /dev/null +++ b/src/Application/Common/Models/Dtos/Logging/RoomLogDto.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 RoomLogDto : IMapFrom +{ + public Guid Id { get; set; } + public Guid UserId { get; set; } + public string Action { get; set; } + public RoomDto? 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/Rooms/Queries/GetAllRoomLogsPaginated.cs b/src/Application/Rooms/Queries/GetAllRoomLogsPaginated.cs new file mode 100644 index 00000000..1099f5ba --- /dev/null +++ b/src/Application/Rooms/Queries/GetAllRoomLogsPaginated.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.Rooms.Queries; + +public class GetAllRoomLogsPaginated +{ + 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.RoomLogs + .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(RoomLogDto.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/Rooms/Queries/GetLogOfRoomById.cs b/src/Application/Rooms/Queries/GetLogOfRoomById.cs new file mode 100644 index 00000000..cee96966 --- /dev/null +++ b/src/Application/Rooms/Queries/GetLogOfRoomById.cs @@ -0,0 +1,43 @@ +using Application.Common.Interfaces; +using Application.Common.Models.Dtos.Logging; +using AutoMapper; +using MediatR; +using Microsoft.EntityFrameworkCore; + +namespace Application.Rooms.Queries; + +public class GetLogOfRoomById +{ + 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.RoomLogs + .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 From 3f9afeae033bcd3b7df28316dd2fff0b3e30f639 Mon Sep 17 00:00:00 2001 From: Vzart Date: Wed, 14 Jun 2023 00:20:51 +0700 Subject: [PATCH 2/2] add: documentation for endpoints --- src/Api/Controllers/RoomsController.cs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Api/Controllers/RoomsController.cs b/src/Api/Controllers/RoomsController.cs index d92b58be..fc001505 100644 --- a/src/Api/Controllers/RoomsController.cs +++ b/src/Api/Controllers/RoomsController.cs @@ -65,7 +65,15 @@ public async Task>>> GetAllPaginated( return Ok(Result>.Succeed(result)); } + /// + /// Get a room log by id + /// + /// + /// return a RoomLogDto + [RequiresRole(IdentityData.Roles.Admin)] [HttpGet("log/{logId:guid}")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status404NotFound)] public async Task>> GetLogById([FromRoute] Guid logId) { var query = new GetLogOfRoomById.Query() @@ -77,7 +85,15 @@ public async Task>> GetLogById([FromRoute] Guid return Ok(Result.Succeed(result)); } - [HttpGet("logs")] public async Task>>> GetAllLogsPaginated( + /// + /// Get all room logs paginated + /// + /// + /// A paginated list of RoomLogDto + [RequiresRole(IdentityData.Roles.Admin)] + [HttpGet("logs")] + [ProducesResponseType(StatusCodes.Status200OK)] + public async Task>>> GetAllLogsPaginated( [FromQuery] GetAllLogsPaginatedQueryParameters queryParameters) { var query = new GetAllRoomLogsPaginated.Query()