package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBDistrictService;
import vn.azteam.tpf.domain.TBDistrict;
import vn.azteam.tpf.repository.TBDistrictRepository;
import vn.azteam.tpf.repository.search.TBDistrictSearchRepository;
import vn.azteam.tpf.service.dto.TBDistrictDTO;
import vn.azteam.tpf.service.mapper.TBDistrictMapper;
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 TBDistrict.
 */
@Service
@Transactional
public class TBDistrictServiceImpl implements TBDistrictService {

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

    private final TBDistrictRepository tBDistrictRepository;

    private final TBDistrictMapper tBDistrictMapper;

    private final TBDistrictSearchRepository tBDistrictSearchRepository;

    public TBDistrictServiceImpl(TBDistrictRepository tBDistrictRepository, TBDistrictMapper tBDistrictMapper, TBDistrictSearchRepository tBDistrictSearchRepository) {
        this.tBDistrictRepository = tBDistrictRepository;
        this.tBDistrictMapper = tBDistrictMapper;
        this.tBDistrictSearchRepository = tBDistrictSearchRepository;
    }

    /**
     * Save a tBDistrict.
     *
     * @param tBDistrictDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBDistrictDTO save(TBDistrictDTO tBDistrictDTO) {
        log.debug("Request to save TBDistrict : {}", tBDistrictDTO);

        TBDistrict tBDistrict = tBDistrictMapper.toEntity(tBDistrictDTO);
        tBDistrict = tBDistrictRepository.save(tBDistrict);
        TBDistrictDTO result = tBDistrictMapper.toDto(tBDistrict);
        tBDistrictSearchRepository.save(tBDistrict);
        return result;
    }

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


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

    /**
     * Delete the tBDistrict by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBDistrict : {}", id);
        tBDistrictRepository.deleteById(id);
        tBDistrictSearchRepository.deleteById(id);
    }

    /**
     * Search for the tBDistrict 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<TBDistrictDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBDistricts for query {}", query);
        return tBDistrictSearchRepository.search(queryStringQuery(query), pageable)
            .map(tBDistrictMapper::toDto);
    }
}
