package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBRoleService;
import vn.azteam.tpf.domain.TBRole;
import vn.azteam.tpf.repository.TBRoleRepository;
import vn.azteam.tpf.repository.search.TBRoleSearchRepository;
import vn.azteam.tpf.service.dto.TBRoleDTO;
import vn.azteam.tpf.service.mapper.TBRoleMapper;
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 TBRole.
 */
@Service
@Transactional
public class TBRoleServiceImpl implements TBRoleService {

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

    private final TBRoleRepository tBRoleRepository;

    private final TBRoleMapper tBRoleMapper;

    private final TBRoleSearchRepository tBRoleSearchRepository;

    public TBRoleServiceImpl(TBRoleRepository tBRoleRepository, TBRoleMapper tBRoleMapper, TBRoleSearchRepository tBRoleSearchRepository) {
        this.tBRoleRepository = tBRoleRepository;
        this.tBRoleMapper = tBRoleMapper;
        this.tBRoleSearchRepository = tBRoleSearchRepository;
    }

    /**
     * Save a tBRole.
     *
     * @param tBRoleDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBRoleDTO save(TBRoleDTO tBRoleDTO) {
        log.debug("Request to save TBRole : {}", tBRoleDTO);

        TBRole tBRole = tBRoleMapper.toEntity(tBRoleDTO);
        tBRole = tBRoleRepository.save(tBRole);
        TBRoleDTO result = tBRoleMapper.toDto(tBRole);
        tBRoleSearchRepository.save(tBRole);
        return result;
    }

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


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

    /**
     * Delete the tBRole by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBRole : {}", id);
        tBRoleRepository.deleteById(id);
        tBRoleSearchRepository.deleteById(id);
    }

    /**
     * Search for the tBRole 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<TBRoleDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBRoles for query {}", query);
        return tBRoleSearchRepository.search(queryStringQuery(query), pageable)
            .map(tBRoleMapper::toDto);
    }
}
