package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBGuidelineService;
import vn.azteam.tpf.domain.TBGuideline;
import vn.azteam.tpf.repository.TBGuidelineRepository;
import vn.azteam.tpf.repository.search.TBGuidelineSearchRepository;
import vn.azteam.tpf.service.dto.TBGuidelineDTO;
import vn.azteam.tpf.service.mapper.TBGuidelineMapper;
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 TBGuideline.
 */
@Service
@Transactional
public class TBGuidelineServiceImpl implements TBGuidelineService {

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

    private final TBGuidelineRepository tBGuidelineRepository;

    private final TBGuidelineMapper tBGuidelineMapper;

    private final TBGuidelineSearchRepository tBGuidelineSearchRepository;

    public TBGuidelineServiceImpl(TBGuidelineRepository tBGuidelineRepository, TBGuidelineMapper tBGuidelineMapper, TBGuidelineSearchRepository tBGuidelineSearchRepository) {
        this.tBGuidelineRepository = tBGuidelineRepository;
        this.tBGuidelineMapper = tBGuidelineMapper;
        this.tBGuidelineSearchRepository = tBGuidelineSearchRepository;
    }

    /**
     * Save a tBGuideline.
     *
     * @param tBGuidelineDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBGuidelineDTO save(TBGuidelineDTO tBGuidelineDTO) {
        log.debug("Request to save TBGuideline : {}", tBGuidelineDTO);

        TBGuideline tBGuideline = tBGuidelineMapper.toEntity(tBGuidelineDTO);
        tBGuideline = tBGuidelineRepository.save(tBGuideline);
        TBGuidelineDTO result = tBGuidelineMapper.toDto(tBGuideline);
        tBGuidelineSearchRepository.save(tBGuideline);
        return result;
    }

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


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

    /**
     * Delete the tBGuideline by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBGuideline : {}", id);
        tBGuidelineRepository.deleteById(id);
        tBGuidelineSearchRepository.deleteById(id);
    }

    /**
     * Search for the tBGuideline 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<TBGuidelineDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBGuidelines for query {}", query);
        return tBGuidelineSearchRepository.search(queryStringQuery(query), pageable)
            .map(tBGuidelineMapper::toDto);
    }
}
