package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBEntityTypeService;
import vn.azteam.tpf.domain.TBEntityType;
import vn.azteam.tpf.repository.TBEntityTypeRepository;
import vn.azteam.tpf.repository.search.TBEntityTypeSearchRepository;
import vn.azteam.tpf.service.dto.TBEntityTypeDTO;
import vn.azteam.tpf.service.mapper.TBEntityTypeMapper;
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 TBEntityType.
 */
@Service
@Transactional
public class TBEntityTypeServiceImpl implements TBEntityTypeService {

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

    private final TBEntityTypeRepository tBEntityTypeRepository;

    private final TBEntityTypeMapper tBEntityTypeMapper;

    private final TBEntityTypeSearchRepository tBEntityTypeSearchRepository;

    public TBEntityTypeServiceImpl(TBEntityTypeRepository tBEntityTypeRepository, TBEntityTypeMapper tBEntityTypeMapper, TBEntityTypeSearchRepository tBEntityTypeSearchRepository) {
        this.tBEntityTypeRepository = tBEntityTypeRepository;
        this.tBEntityTypeMapper = tBEntityTypeMapper;
        this.tBEntityTypeSearchRepository = tBEntityTypeSearchRepository;
    }

    /**
     * Save a tBEntityType.
     *
     * @param tBEntityTypeDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBEntityTypeDTO save(TBEntityTypeDTO tBEntityTypeDTO) {
        log.debug("Request to save TBEntityType : {}", tBEntityTypeDTO);

        TBEntityType tBEntityType = tBEntityTypeMapper.toEntity(tBEntityTypeDTO);
        tBEntityType = tBEntityTypeRepository.save(tBEntityType);
        TBEntityTypeDTO result = tBEntityTypeMapper.toDto(tBEntityType);
        tBEntityTypeSearchRepository.save(tBEntityType);
        return result;
    }

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


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

    /**
     * Delete the tBEntityType by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBEntityType : {}", id);
        tBEntityTypeRepository.deleteById(id);
        tBEntityTypeSearchRepository.deleteById(id);
    }

    /**
     * Search for the tBEntityType 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<TBEntityTypeDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBEntityTypes for query {}", query);
        return tBEntityTypeSearchRepository.search(queryStringQuery(query), pageable)
            .map(tBEntityTypeMapper::toDto);
    }
}
