L
L
lucky42021-06-23 12:11:46
C++ / C#
lucky4, 2021-06-23 12:11:46

How can you shorten the code?

public async Task<IList<WishlistItemDto>> GetAllAsync(PageInfo pageInfo)
        {
            if (pageInfo.Page <= 0 || pageInfo.ItemsPerPage <= 0)
            {
                pageInfo.Page = 1;
                pageInfo.ItemsPerPage = 1;
            }

            int pageSize = 10;
            int pageNumber = (pageInfo.Page - 1) * pageInfo.ItemsPerPage;

            var items = await _wishListItemRepository.GetAllAsync(pageNumber, pageSize);

            var reversedList = (List<WishlistItemDto>)_mapper.Map<IList<WishlistItemDto>>(items);
            reversedList.Reverse();

            return reversedList;
        }


I don't like this reversedList variable. How can it be immediately driven into return?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Bannikov, 2021-06-23
@lucky4

It will not work to shorten the code, because it is already very compact
Page number and page size in PageInfo, it makes sense to control it from the outside, for example, in the constructor or the PageInfo
Offset and Limit properties, it makes sense to take it out - to extensions or to PageInfo methods

public async Task<IList<WishlistItemDto>> GetAllAsync(PageInfo pageInfo)
        {
            var items = await _wishListItemRepository.GetAllAsync(pageInfo.Offset(), pageInfo.Limit());
            return _mapper.Map<IList<WishlistItemDto>>(items).Reverse().ToList();
        }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question