package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBPermissionService;
import vn.azteam.tpf.domain.TBPermission;
import vn.azteam.tpf.repository.TBPermissionRepository;
import vn.azteam.tpf.repository.search.TBPermissionSearchRepository;
import vn.azteam.tpf.service.dto.TBPermissionDTO;
import vn.azteam.tpf.service.mapper.TBPermissionMapper;
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 TBPermission.
 */
@Service
@Transactional
public class TBPermissionServiceImpl implements TBPermissionService {

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

    private final TBPermissionRepository tBPermissionRepository;

    private final TBPermissionMapper tBPermissionMapper;

    private final TBPermissionSearchRepository tBPermissionSearchRepository;

    public TBPermissionServiceImpl(TBPermissionRepository tBPermissionRepository, TBPermissionMapper tBPermissionMapper, TBPermissionSearchRepository tBPermissionSearchRepository) {
        this.tBPermissionRepository = tBPermissionRepository;
        this.tBPermissionMapper = tBPermissionMapper;
        this.tBPermissionSearchRepository = tBPermissionSearchRepository;
    }

    /**
     * Save a tBPermission.
     *
     * @param tBPermissionDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBPermissionDTO save(TBPermissionDTO tBPermissionDTO) {
        log.debug("Request to save TBPermission : {}", tBPermissionDTO);

        TBPermission tBPermission = tBPermissionMapper.toEntity(tBPermissionDTO);
        tBPermission = tBPermissionRepository.save(tBPermission);
        TBPermissionDTO result = tBPermissionMapper.toDto(tBPermission);
        tBPermissionSearchRepository.save(tBPermission);
        return result;
    }

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


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

    /**
     * Delete the tBPermission by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBPermission : {}", id);
        tBPermissionRepository.deleteById(id);
        tBPermissionSearchRepository.deleteById(id);
    }

    /**
     * Search for the tBPermission 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<TBPermissionDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBPermissions for query {}", query);
        return tBPermissionSearchRepository.search(queryStringQuery(query), pageable)
            .map(tBPermissionMapper::toDto);
    }
}
