| @@ -31,7 +31,7 @@ namespace BlackRock.Reporting.API.Controllers | |||
| } | |||
| // GET: api/documents | |||
| [HttpGet] | |||
| public async Task<IActionResult> GetDocuments(PaggingAndFiltering query) | |||
| public async Task<IActionResult> GetDocuments(SortFilter query) | |||
| { | |||
| throw new DomainException("Relay"); | |||
| var result = await mediator.GetDocuments(query); | |||
| @@ -6,6 +6,6 @@ namespace BlackRock.Reporting.API.Core | |||
| public interface IUsersRepository : IRepository<User> | |||
| { | |||
| void UpdateEmail(User user,string email); | |||
| Task<PaggingCollection<User>> GetAllByFilter(UserPaggingAndFiltering queryObj); | |||
| Task<PagedCollection<User>> GetAllByFilter(UsersFilter queryObj); | |||
| } | |||
| } | |||
| @@ -1,10 +0,0 @@ | |||
| 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; } | |||
| } | |||
| } | |||
| @@ -1,7 +0,0 @@ | |||
| namespace BlackRock.Reporting.API.Core.Models | |||
| { | |||
| public interface IUserPaggingAndFiltering : IPaggingAndFiltering | |||
| { | |||
| string EmailDomain {get;set;} | |||
| } | |||
| } | |||
| @@ -0,0 +1,12 @@ | |||
| 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; } | |||
| } | |||
| } | |||
| @@ -1,18 +0,0 @@ | |||
| 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;} | |||
| } | |||
| } | |||
| @@ -1,12 +0,0 @@ | |||
| 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;} | |||
| } | |||
| } | |||
| @@ -0,0 +1,8 @@ | |||
| namespace BlackRock.Reporting.API.Core.Models | |||
| { | |||
| public class PaginationFilter | |||
| { | |||
| public int? Page { get; set; } | |||
| public int? PageSize { get; set; } | |||
| } | |||
| } | |||
| @@ -0,0 +1,8 @@ | |||
| namespace BlackRock.Reporting.API.Core.Models | |||
| { | |||
| public class SortFilter : PaginationFilter | |||
| { | |||
| public string? SortBy { get; set; } | |||
| public bool? IsSortAscending { get; set; } | |||
| } | |||
| } | |||
| @@ -0,0 +1,7 @@ | |||
| namespace BlackRock.Reporting.API.Core.Models | |||
| { | |||
| public class UsersFilter : SortFilter | |||
| { | |||
| public string? EmailDomain { get; set; } | |||
| } | |||
| } | |||
| @@ -5,28 +5,29 @@ namespace BlackRock.Reporting.API.Extensions | |||
| { | |||
| 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) || | |||
| !columnsMap.ContainsKey(queryObj.SortBy)) | |||
| return query; | |||
| if (queryObj.IsSortAscending) | |||
| if (queryObj.IsSortAscending.GetValueOrDefault()) | |||
| return query.OrderBy(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; | |||
| if (queryObj.PageSize <= 0) | |||
| if (queryObj.PageSize is null) | |||
| 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()); | |||
| } | |||
| } | |||
| } | |||
| @@ -6,7 +6,7 @@ namespace BlackRock.Reporting.API.Mediator.DocumentMediator | |||
| public interface IDocumentMediator | |||
| { | |||
| 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>> UpdateDocument(UpdateDocumentForm form); | |||
| Task<Result<int>> DeleteDocument(int id); | |||
| @@ -7,11 +7,11 @@ using MediatR; | |||
| 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 IMapper mapper; | |||
| @@ -22,7 +22,7 @@ namespace BlackRock.Reporting.API.Mediator.UserMediator.Queries | |||
| this.mapper = mapper; | |||
| 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) | |||
| throw new ArgumentNullException($"Parameter {nameof(command)} must not be null"); | |||
| @@ -30,13 +30,13 @@ namespace BlackRock.Reporting.API.Mediator.UserMediator.Queries | |||
| try | |||
| { | |||
| 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"); | |||
| return new Result<PaggingCollection<UserDto>> { Data = usersDto }; | |||
| return new Result<PagedCollection<UserDto>> { Data = usersDto }; | |||
| } | |||
| 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." }; | |||
| } | |||
| } | |||
| } | |||
| @@ -13,9 +13,9 @@ namespace BlackRock.Reporting.API.Persistence.Repositories | |||
| { | |||
| 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 query = queryResult.AsQueryable(); | |||
| @@ -137,3 +137,6 @@ | |||
| 2.0 | |||
| 2.0 | |||
| 2.0 | |||
| 2.0 | |||
| 2.0 | |||
| 2.0 | |||