package com.aksenov.monitor.services;

import com.aksenov.monitor.data.City;
import com.aksenov.monitor.repositories.CityRepository;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
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 CityService {

    private final CityRepository repository;

    @Autowired
    public CityService(CityRepository repository) {
        this.repository = repository;
    }

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

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

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

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

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


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


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