package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBCountryService;
import vn.azteam.tpf.domain.TBCountry;
import vn.azteam.tpf.repository.TBCountryRepository;
import vn.azteam.tpf.repository.search.TBCountrySearchRepository;
import vn.azteam.tpf.service.dto.TBCountryDTO;
import vn.azteam.tpf.service.mapper.TBCountryMapper;
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 TBCountry.
 */
@Service
@Transactional
public class TBCountryServiceImpl implements TBCountryService {

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

    private final TBCountryRepository tBCountryRepository;

    private final TBCountryMapper tBCountryMapper;

    private final TBCountrySearchRepository tBCountrySearchRepository;

    public TBCountryServiceImpl(TBCountryRepository tBCountryRepository, TBCountryMapper tBCountryMapper, TBCountrySearchRepository tBCountrySearchRepository) {
        this.tBCountryRepository = tBCountryRepository;
        this.tBCountryMapper = tBCountryMapper;
        this.tBCountrySearchRepository = tBCountrySearchRepository;
    }

    /**
     * Save a tBCountry.
     *
     * @param tBCountryDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBCountryDTO save(TBCountryDTO tBCountryDTO) {
        log.debug("Request to save TBCountry : {}", tBCountryDTO);

        TBCountry tBCountry = tBCountryMapper.toEntity(tBCountryDTO);
        tBCountry = tBCountryRepository.save(tBCountry);
        TBCountryDTO result = tBCountryMapper.toDto(tBCountry);
        tBCountrySearchRepository.save(tBCountry);
        return result;
    }

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


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

    /**
     * Delete the tBCountry by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBCountry : {}", id);
        tBCountryRepository.deleteById(id);
        tBCountrySearchRepository.deleteById(id);
    }

    /**
     * Search for the tBCountry 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<TBCountryDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBCountries for query {}", query);
        return tBCountrySearchRepository.search(queryStringQuery(query), pageable)
            .map(tBCountryMapper::toDto);
    }
}
