package com.github.microservice.components.data.base.page;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.util.StringUtils;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class PageableModel {

    @JsonProperty("page")
    private int page;

    @JsonProperty("size")
    private int size;

    @JsonProperty("sort")
    private String sort;

    @JsonProperty("direction")
    private Sort.Direction direction = Sort.Direction.DESC;


    public Pageable toPageable() {
        return StringUtils.hasText(sort) ? PageRequest.of(page, size, Sort.by(direction, sort)) : PageRequest.of(page, size);
    }
}
