package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBCityService;
import vn.azteam.tpf.domain.TBCity;
import vn.azteam.tpf.repository.TBCityRepository;
import vn.azteam.tpf.repository.search.TBCitySearchRepository;
import vn.azteam.tpf.service.dto.TBCityDTO;
import vn.azteam.tpf.service.mapper.TBCityMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * Service Implementation for managing TBCity.
 */
@Service
@Transactional
public class TBCityServiceImpl implements TBCityService {

    private final Logger log = LoggerFactory.getLogger(TBCityServiceImpl.class);

    private final TBCityRepository tBCityRepository;

    private final TBCityMapper tBCityMapper;

    private final TBCitySearchRepository tBCitySearchRepository;

    public TBCityServiceImpl(TBCityRepository tBCityRepository, TBCityMapper tBCityMapper, TBCitySearchRepository tBCitySearchRepository) {
        this.tBCityRepository = tBCityRepository;
        this.tBCityMapper = tBCityMapper;
        this.tBCitySearchRepository = tBCitySearchRepository;
    }

    /**
     * Save a tBCity.
     *
     * @param tBCityDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBCityDTO save(TBCityDTO tBCityDTO) {
        log.debug("Request to save TBCity : {}", tBCityDTO);

        TBCity tBCity = tBCityMapper.toEntity(tBCityDTO);
        tBCity = tBCityRepository.save(tBCity);
        TBCityDTO result = tBCityMapper.toDto(tBCity);
        tBCitySearchRepository.save(tBCity);
        return result;
    }

    /**
     * Get all the tBCities.
     *
     * @param pageable the pagination information
     * @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<TBCityDTO> findAll(Pageable pageable) {
        log.debug("Request to get all TBCities");
        return tBCityRepository.findAll(pageable)
            .map(tBCityMapper::toDto);
    }


    /**
     * Get one tBCity by id.
     *
     * @param id the id of the entity
     * @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public Optional<TBCityDTO> findOne(Long id) {
        log.debug("Request to get TBCity : {}", id);
        return tBCityRepository.findById(id)
            .map(tBCityMapper::toDto);
    }

    /**
     * Delete the tBCity by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBCity : {}", id);
        tBCityRepository.deleteById(id);
        tBCitySearchRepository.deleteById(id);
    }

    /**
     * Search for the tBCity corresponding to the query.
     *
     * @param query the query of the search
     * @param pageable the pagination information
     * @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<TBCityDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBCities for query {}", query);
        return tBCitySearchRepository.search(queryStringQuery(query), pageable)
            .map(tBCityMapper::toDto);
    }
}
