package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBDashboardTypeService;
import vn.azteam.tpf.domain.TBDashboardType;
import vn.azteam.tpf.repository.TBDashboardTypeRepository;
import vn.azteam.tpf.repository.search.TBDashboardTypeSearchRepository;
import vn.azteam.tpf.service.dto.TBDashboardTypeDTO;
import vn.azteam.tpf.service.mapper.TBDashboardTypeMapper;
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 TBDashboardType.
 */
@Service
@Transactional
public class TBDashboardTypeServiceImpl implements TBDashboardTypeService {

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

    private final TBDashboardTypeRepository tBDashboardTypeRepository;

    private final TBDashboardTypeMapper tBDashboardTypeMapper;

    private final TBDashboardTypeSearchRepository tBDashboardTypeSearchRepository;

    public TBDashboardTypeServiceImpl(TBDashboardTypeRepository tBDashboardTypeRepository, TBDashboardTypeMapper tBDashboardTypeMapper, TBDashboardTypeSearchRepository tBDashboardTypeSearchRepository) {
        this.tBDashboardTypeRepository = tBDashboardTypeRepository;
        this.tBDashboardTypeMapper = tBDashboardTypeMapper;
        this.tBDashboardTypeSearchRepository = tBDashboardTypeSearchRepository;
    }

    /**
     * Save a tBDashboardType.
     *
     * @param tBDashboardTypeDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBDashboardTypeDTO save(TBDashboardTypeDTO tBDashboardTypeDTO) {
        log.debug("Request to save TBDashboardType : {}", tBDashboardTypeDTO);

        TBDashboardType tBDashboardType = tBDashboardTypeMapper.toEntity(tBDashboardTypeDTO);
        tBDashboardType = tBDashboardTypeRepository.save(tBDashboardType);
        TBDashboardTypeDTO result = tBDashboardTypeMapper.toDto(tBDashboardType);
        tBDashboardTypeSearchRepository.save(tBDashboardType);
        return result;
    }

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


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

    /**
     * Delete the tBDashboardType by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBDashboardType : {}", id);
        tBDashboardTypeRepository.deleteById(id);
        tBDashboardTypeSearchRepository.deleteById(id);
    }

    /**
     * Search for the tBDashboardType 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<TBDashboardTypeDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBDashboardTypes for query {}", query);
        return tBDashboardTypeSearchRepository.search(queryStringQuery(query), pageable)
            .map(tBDashboardTypeMapper::toDto);
    }
}
