package com.izouma.nineth.service;

import com.izouma.nineth.domain.Banner;
import com.izouma.nineth.dto.PageQuery;
import com.izouma.nineth.dto.PageWrapper;
import com.izouma.nineth.repo.BannerRepo;
import com.izouma.nineth.utils.JpaUtils;
import lombok.AllArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

@Service
@AllArgsConstructor
public class BannerService {

    private BannerRepo bannerRepo;

    @Cacheable(value = "bannerList", key = "#pageQuery.hashCode()")
    public PageWrapper<Banner> all(PageQuery pageQuery) {
        Page<Banner> page = bannerRepo.findAll(JpaUtils.toSpecification(pageQuery, Banner.class), JpaUtils.toPageRequest(pageQuery));
        return new PageWrapper<>(page.getContent(), page.getPageable().getPageNumber(),
                page.getPageable().getPageSize(), page.getTotalElements());
    }
}
