Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1
feat: get room log by ID and get room logs paginated#241
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff 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 RoomLogDto : IMapFrom<RoomLog> | ||
| { | ||
| 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<RoomLog, RoomLogDto>() | ||
| .ForMember(dest => dest.Time, | ||
| opt => opt.MapFrom(src => src.Time.ToDateTimeUnspecified())) | ||
| .ForMember(dest => dest.Object, | ||
| opt => opt.MapFrom(src => src.Object)); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff 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.Rooms.Queries; | ||
| public class GetAllRoomLogsPaginated | ||
| { | ||
| public record Query : IRequest<PaginatedList<RoomLogDto>> | ||
| { | ||
| 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<RoomLogDto>> | ||
| { | ||
| private readonly IApplicationDbContext _context; | ||
| private readonly IMapper _mapper; | ||
| public QueryHandler(IApplicationDbContext context, IMapper mapper) | ||
| { | ||
| _context = context; | ||
| _mapper = mapper; | ||
| } | ||
| public async Task<PaginatedList<RoomLogDto>> 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<RoomLogDto>()) | ||
| { | ||
| 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<RoomLogDto>>(list); | ||
| return new PaginatedList<RoomLogDto>(result, count, pageNumber.Value, sizeNumber.Value); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff 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.Rooms.Queries; | ||
| public class GetLogOfRoomById | ||
| { | ||
| public record Query : IRequest<RoomLogDto> | ||
| { | ||
| public Guid LogId { get; init; } | ||
| } | ||
| public class QueryHandler : IRequestHandler<Query, RoomLogDto> | ||
| { | ||
| private readonly IApplicationDbContext _context; | ||
| private readonly IMapper _mapper; | ||
| public QueryHandler(IApplicationDbContext context, IMapper mapper) | ||
| { | ||
| _context = context; | ||
| _mapper = mapper; | ||
| } | ||
| public async Task<RoomLogDto> 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<RoomLogDto>(log); | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.