package @app.group.id@.@app.name@.resources.support;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

@Component
public class HypermediaSupport {
    public ResponseEntity<Page> wrap(Page page, Pageable pageable) {
        HttpHeaders headers = new HttpHeaders();

        long total = page.getTotalElements();
        // Page and range are zero-based:
        long from = page.getNumber() * page.getSize();
        long to = from + pageable.getPageSize() - 1;

        headers.add("Accept-Ranges", "item");
        headers.add("Content-Range", "item " + from + "-" + to + "/" + total);

        return new ResponseEntity<Page>(page, headers, HttpStatus.PARTIAL_CONTENT);
    }
}
