package com.aksenov.monitor.services;

import com.aksenov.monitor.data.Customer;
import com.aksenov.monitor.repositories.CustomerRepository;

import java.util.List;
import java.util.Optional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

@Service
public class CustomerService {

    private final CustomerRepository repository;

    public CustomerService(CustomerRepository repository) {
        this.repository = repository;
    }

    public Optional<Customer> get(Long id) {
        return repository.findById(id);
    }

    public Customer save(Customer entity) {
        return repository.save(entity);
    }

    public void delete(Long id) {
        repository.deleteById(id);
    }

    public Page<Customer> list(Pageable pageable) {
        return repository.findAll(pageable);
    }

    public Page<Customer> list(Pageable pageable, Specification<Customer> filter) {
        return repository.findAll(filter, pageable);
    }

    public int count() {
        return (int) repository.count();
    }

    public List<Customer> list() {
        return repository.findAll();
    }
}
