package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBEntityService;
import vn.azteam.tpf.domain.TBEntity;
import vn.azteam.tpf.repository.TBEntityRepository;
import vn.azteam.tpf.repository.search.TBEntitySearchRepository;
import vn.azteam.tpf.service.dto.TBEntityDTO;
import vn.azteam.tpf.service.mapper.TBEntityMapper;
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 TBEntity.
 */
@Service
@Transactional
public class TBEntityServiceImpl implements TBEntityService {

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

    private final TBEntityRepository tBEntityRepository;

    private final TBEntityMapper tBEntityMapper;

    private final TBEntitySearchRepository tBEntitySearchRepository;

    public TBEntityServiceImpl(TBEntityRepository tBEntityRepository, TBEntityMapper tBEntityMapper, TBEntitySearchRepository tBEntitySearchRepository) {
        this.tBEntityRepository = tBEntityRepository;
        this.tBEntityMapper = tBEntityMapper;
        this.tBEntitySearchRepository = tBEntitySearchRepository;
    }

    /**
     * Save a tBEntity.
     *
     * @param tBEntityDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBEntityDTO save(TBEntityDTO tBEntityDTO) {
        log.debug("Request to save TBEntity : {}", tBEntityDTO);

        TBEntity tBEntity = tBEntityMapper.toEntity(tBEntityDTO);
        tBEntity = tBEntityRepository.save(tBEntity);
        TBEntityDTO result = tBEntityMapper.toDto(tBEntity);
        tBEntitySearchRepository.save(tBEntity);
        return result;
    }

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


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

    /**
     * Delete the tBEntity by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBEntity : {}", id);
        tBEntityRepository.deleteById(id);
        tBEntitySearchRepository.deleteById(id);
    }

    /**
     * Search for the tBEntity 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<TBEntityDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBEntities for query {}", query);
        return tBEntitySearchRepository.search(queryStringQuery(query), pageable)
            .map(tBEntityMapper::toDto);
    }
}
