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
public static PagedResponse<T> CreatePaginatedResponse<T>(IUriService uriService, PaginationFilter pagination, List<T> response) | |
{ | |
var nextPage = pagination.PageNumber >= 1 | |
? uriService.GetAllPostsUri(new PaginationQuery(pagination.PageNumber + 1, pagination.PageSize)).ToString() | |
: null; | |
var previousPage = pagination.PageNumber - 1 >= 1 | |
? uriService.GetAllPostsUri(new PaginationQuery(pagination.PageNumber - 1, pagination.PageSize)).ToString() | |
: null; |
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
public async Task<IActionResult> GetAll([FromQuery]PaginationQuery paginationQuery) | |
{ | |
var pagination = _mapper.Map<PaginationFilter>(paginationQuery); | |
var posts = await _postService.GetPostsAsync(pagination); | |
var postsResponse = _mapper.Map<List<PostResponse>>(posts); | |
if (pagination == null || pagination.PageNumber < 1 || pagination.PageSize < 1) | |
{ | |
return Ok(new PagedResponse<PostResponse>(postsResponse)); | |
} |
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
public class UriService : IUriService | |
{ | |
private readonly string _baseUri; | |
public UriService(string baseUri) | |
{ | |
_baseUri = baseUri; | |
} | |
public Uri GetPostUri(string postId) |
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
public async Task<List<Post>> GetPostsAsync(PaginationFilter paginationFilter = null) | |
{ | |
if (paginationFilter == null) | |
{ | |
return await _dataContext.Posts.Include(x => x.Tags).ToListAsync(); | |
} | |
var skip = (paginationFilter.PageNumber - 1) * paginationFilter.PageSize; | |
return await _dataContext.Posts.Include(x => x.Tags) | |
.Skip(skip).Take(paginationFilter.PageSize).ToListAsync(); |
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
public class RequestToDomainProfile : Profile | |
{ | |
public RequestToDomainProfile() | |
{ | |
CreateMap<PaginationQuery, PaginationFilter>(); | |
} | |
} |
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
public class PaginationFilter | |
{ | |
public int PageNumber { get; set; } | |
public int PageSize { get; set; } | |
} |
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
public class PaginationQuery | |
{ | |
public PaginationQuery() | |
{ | |
PageNumber = 1; | |
PageSize = 100; | |
} | |
public PaginationQuery(int pageNumber, int pageSize) | |
{ |
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
public class PagedResponse<T> | |
{ | |
public PagedResponse(){} | |
public PagedResponse(IEnumerable<T> data) | |
{ | |
Data = data; | |
} | |
public IEnumerable<T> Data { get; set; } |
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
public class Response<T> | |
{ | |
public Response() {} | |
public Response(T response) | |
{ | |
Data = response; | |
} | |
public T Data { get; set; } |
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
private static string GenerateCacheKeyFromRequest(HttpRequest request) | |
{ | |
var keyBuilder = new StringBuilder(); | |
keyBuilder.Append($"{request.Path}"); | |
foreach (var (key, value) in request.Query.OrderBy(x=>x.Key)) | |
{ | |
keyBuilder.Append($"|{key}-{value}"); | |
} |