| } | } | ||||
| // GET: api/documents | // GET: api/documents | ||||
| [HttpGet] | [HttpGet] | ||||
| public async Task<IActionResult> GetDocuments(PaggingAndFiltering query) | |||||
| public async Task<IActionResult> GetDocuments(SortFilter query) | |||||
| { | { | ||||
| throw new DomainException("Relay"); | throw new DomainException("Relay"); | ||||
| var result = await mediator.GetDocuments(query); | var result = await mediator.GetDocuments(query); |
| public interface IUsersRepository : IRepository<User> | public interface IUsersRepository : IRepository<User> | ||||
| { | { | ||||
| void UpdateEmail(User user,string email); | void UpdateEmail(User user,string email); | ||||
| Task<PaggingCollection<User>> GetAllByFilter(UserPaggingAndFiltering queryObj); | |||||
| Task<PagedCollection<User>> GetAllByFilter(UsersFilter queryObj); | |||||
| } | } | ||||
| } | } |
| namespace BlackRock.Reporting.API.Core.Models | |||||
| { | |||||
| public interface IPaggingAndFiltering | |||||
| { | |||||
| string SortBy { get; set; } | |||||
| bool IsSortAscending { get; set; } | |||||
| int Page { get; set; } | |||||
| int PageSize { get; set; } | |||||
| } | |||||
| } |
| namespace BlackRock.Reporting.API.Core.Models | |||||
| { | |||||
| public interface IUserPaggingAndFiltering : IPaggingAndFiltering | |||||
| { | |||||
| string EmailDomain {get;set;} | |||||
| } | |||||
| } |
| using System.Collections.ObjectModel; | |||||
| namespace BlackRock.Reporting.API.Core.Models | |||||
| { | |||||
| public class PagedCollection<T> : Collection<T> | |||||
| where T : class | |||||
| { | |||||
| public int Page { set; get; } | |||||
| public int PageSize { set; get; } | |||||
| public int TotalCount { set; get; } | |||||
| } | |||||
| } |
| namespace BlackRock.Reporting.API.Core.Models | |||||
| { | |||||
| public class UserPaggingAndFiltering : IUserPaggingAndFiltering | |||||
| { | |||||
| public string? EmailDomain { get ; set ; } | |||||
| public string? SortBy { get ; set ; } | |||||
| public bool IsSortAscending { get ; set ; } | |||||
| public int Page { get ; set ; } | |||||
| public int PageSize { get ; set ; } | |||||
| } | |||||
| public class PaggingAndFiltering : IPaggingAndFiltering | |||||
| { | |||||
| public string? SortBy {get;set;} | |||||
| public bool IsSortAscending {get;set;} | |||||
| public int Page {get;set;} | |||||
| public int PageSize {get;set;} | |||||
| } | |||||
| } |
| using System.Collections.ObjectModel; | |||||
| namespace BlackRock.Reporting.API.Core.Models | |||||
| { | |||||
| public class PaggingCollection<T> : Collection<T>, IPaggingAndFiltering where T : class | |||||
| { | |||||
| public string? SortBy {set;get;} | |||||
| public bool IsSortAscending {set;get;} | |||||
| public int Page {set;get;} | |||||
| public int PageSize {set;get;} | |||||
| } | |||||
| } |
| namespace BlackRock.Reporting.API.Core.Models | |||||
| { | |||||
| public class PaginationFilter | |||||
| { | |||||
| public int? Page { get; set; } | |||||
| public int? PageSize { get; set; } | |||||
| } | |||||
| } |
| namespace BlackRock.Reporting.API.Core.Models | |||||
| { | |||||
| public class SortFilter : PaginationFilter | |||||
| { | |||||
| public string? SortBy { get; set; } | |||||
| public bool? IsSortAscending { get; set; } | |||||
| } | |||||
| } |
| namespace BlackRock.Reporting.API.Core.Models | |||||
| { | |||||
| public class UsersFilter : SortFilter | |||||
| { | |||||
| public string? EmailDomain { get; set; } | |||||
| } | |||||
| } |
| { | { | ||||
| public static class IQueryableExtensions | public static class IQueryableExtensions | ||||
| { | { | ||||
| public static IQueryable<T> ApplyOrdering<T>(this IQueryable<T> query, | |||||
| IPaggingAndFiltering queryObj, Dictionary<string, Expression<Func<T, object>>> columnsMap) | |||||
| public static IQueryable<T> ApplyOrdering<T>( | |||||
| this IQueryable<T> query, | |||||
| SortFilter queryObj, | |||||
| Dictionary<string, Expression<Func<T, object>>> columnsMap) | |||||
| { | { | ||||
| if (string.IsNullOrWhiteSpace(queryObj.SortBy) || | if (string.IsNullOrWhiteSpace(queryObj.SortBy) || | ||||
| !columnsMap.ContainsKey(queryObj.SortBy)) | !columnsMap.ContainsKey(queryObj.SortBy)) | ||||
| return query; | return query; | ||||
| if (queryObj.IsSortAscending) | |||||
| if (queryObj.IsSortAscending.GetValueOrDefault()) | |||||
| return query.OrderBy(columnsMap[queryObj.SortBy]); | return query.OrderBy(columnsMap[queryObj.SortBy]); | ||||
| return query.OrderByDescending(columnsMap[queryObj.SortBy]); | return query.OrderByDescending(columnsMap[queryObj.SortBy]); | ||||
| } | } | ||||
| public static IQueryable<T> ApplyPagging<T>(this IQueryable<T> query, | |||||
| IPaggingAndFiltering queryObj) | |||||
| public static IQueryable<T> ApplyPagging<T>(this IQueryable<T> query,PaginationFilter queryObj) | |||||
| { | { | ||||
| if (queryObj.Page <= 0) | |||||
| if (queryObj.Page is null) | |||||
| queryObj.Page = 1; | queryObj.Page = 1; | ||||
| if (queryObj.PageSize <= 0) | |||||
| if (queryObj.PageSize is null) | |||||
| queryObj.PageSize = 10; | queryObj.PageSize = 10; | ||||
| return query.Skip((queryObj.Page - 1) * queryObj.PageSize) | |||||
| .Take(queryObj.PageSize); | |||||
| return query.Skip((queryObj.Page.GetValueOrDefault() - 1) * queryObj.PageSize.GetValueOrDefault()) | |||||
| .Take(queryObj.PageSize.GetValueOrDefault()); | |||||
| } | } | ||||
| } | } | ||||
| } | } |
| public interface IDocumentMediator | public interface IDocumentMediator | ||||
| { | { | ||||
| Task<Result<Document>> GetDocument(int id); | Task<Result<Document>> GetDocument(int id); | ||||
| Task<Result<PaggingCollection<Document>>> GetDocuments(PaggingAndFiltering query); | |||||
| Task<Result<PagedCollection<Document>>> GetDocuments(SortFilter query); | |||||
| Task<Result<int>> CreateDocument(CreateDocumentForm form); | Task<Result<int>> CreateDocument(CreateDocumentForm form); | ||||
| Task<Result<int>> UpdateDocument(UpdateDocumentForm form); | Task<Result<int>> UpdateDocument(UpdateDocumentForm form); | ||||
| Task<Result<int>> DeleteDocument(int id); | Task<Result<int>> DeleteDocument(int id); |
| namespace BlackRock.Reporting.API.Mediator.UserMediator.Queries | namespace BlackRock.Reporting.API.Mediator.UserMediator.Queries | ||||
| { | { | ||||
| public class GetAllUsersQuery : UserPaggingAndFiltering, IRequest<Result<PaggingCollection<UserDto>>> | |||||
| public class GetAllUsersQuery : UsersFilter, IRequest<Result<PagedCollection<UserDto>>> | |||||
| { | { | ||||
| } | } | ||||
| public class GetAllUsersQueryHandlers : IRequestHandler<GetAllUsersQuery, Result<PaggingCollection<UserDto>>> | |||||
| public class GetAllUsersQueryHandlers : IRequestHandler<GetAllUsersQuery, Result<PagedCollection<UserDto>>> | |||||
| { | { | ||||
| private readonly ILogger<GetAllUsersQueryHandlers> logger; | private readonly ILogger<GetAllUsersQueryHandlers> logger; | ||||
| private readonly IMapper mapper; | private readonly IMapper mapper; | ||||
| this.mapper = mapper; | this.mapper = mapper; | ||||
| this.logger = logger; | this.logger = logger; | ||||
| } | } | ||||
| public async Task<Result<PaggingCollection<UserDto>>> Handle(GetAllUsersQuery command, CancellationToken cancellationToken) | |||||
| public async Task<Result<PagedCollection<UserDto>>> Handle(GetAllUsersQuery command, CancellationToken cancellationToken) | |||||
| { | { | ||||
| if (command == null) | if (command == null) | ||||
| throw new ArgumentNullException($"Parameter {nameof(command)} must not be null"); | throw new ArgumentNullException($"Parameter {nameof(command)} must not be null"); | ||||
| try | try | ||||
| { | { | ||||
| var users = await unitOfWork.UsersRepository.GetAllByFilter(command); | var users = await unitOfWork.UsersRepository.GetAllByFilter(command); | ||||
| var usersDto = mapper.Map<PaggingCollection<User>, PaggingCollection<UserDto>>(users); | |||||
| var usersDto = mapper.Map<PagedCollection<User>, PagedCollection<UserDto>>(users); | |||||
| logger.LogInformation($"The Users has been founded successfully"); | logger.LogInformation($"The Users has been founded successfully"); | ||||
| return new Result<PaggingCollection<UserDto>> { Data = usersDto }; | |||||
| return new Result<PagedCollection<UserDto>> { Data = usersDto }; | |||||
| } | } | ||||
| catch (Exception ex) | catch (Exception ex) | ||||
| { | { | ||||
| return new Result<PaggingCollection<UserDto>> { IsSuccess = false, Error = "Faild to fetch data from DB." }; | |||||
| return new Result<PagedCollection<UserDto>> { IsSuccess = false, Error = "Faild to fetch data from DB." }; | |||||
| } | } | ||||
| } | } | ||||
| } | } |
| { | { | ||||
| this.context = context; | this.context = context; | ||||
| } | } | ||||
| public async Task<PaggingCollection<User>> GetAllByFilter(UserPaggingAndFiltering queryObj) | |||||
| public async Task<PagedCollection<User>> GetAllByFilter(UsersFilter queryObj) | |||||
| { | { | ||||
| var result = new PaggingCollection<User>(); | |||||
| var result = new PagedCollection<User>(); | |||||
| var queryResult = await context.Users.ToListAsync(); | var queryResult = await context.Users.ToListAsync(); | ||||
| var query = queryResult.AsQueryable(); | var query = queryResult.AsQueryable(); |
| 2.0 | 2.0 | ||||
| 2.0 | 2.0 | ||||
| 2.0 | 2.0 | ||||
| 2.0 | |||||
| 2.0 | |||||
| 2.0 |