package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBCommandService;
import vn.azteam.tpf.domain.TBCommand;
import vn.azteam.tpf.repository.TBCommandRepository;
import vn.azteam.tpf.repository.search.TBCommandSearchRepository;
import vn.azteam.tpf.service.dto.TBCommandDTO;
import vn.azteam.tpf.service.mapper.TBCommandMapper;
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 TBCommand.
 */
@Service
@Transactional
public class TBCommandServiceImpl implements TBCommandService {

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

    private final TBCommandRepository tBCommandRepository;

    private final TBCommandMapper tBCommandMapper;

    private final TBCommandSearchRepository tBCommandSearchRepository;

    public TBCommandServiceImpl(TBCommandRepository tBCommandRepository, TBCommandMapper tBCommandMapper, TBCommandSearchRepository tBCommandSearchRepository) {
        this.tBCommandRepository = tBCommandRepository;
        this.tBCommandMapper = tBCommandMapper;
        this.tBCommandSearchRepository = tBCommandSearchRepository;
    }

    /**
     * Save a tBCommand.
     *
     * @param tBCommandDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBCommandDTO save(TBCommandDTO tBCommandDTO) {
        log.debug("Request to save TBCommand : {}", tBCommandDTO);

        TBCommand tBCommand = tBCommandMapper.toEntity(tBCommandDTO);
        tBCommand = tBCommandRepository.save(tBCommand);
        TBCommandDTO result = tBCommandMapper.toDto(tBCommand);
        tBCommandSearchRepository.save(tBCommand);
        return result;
    }

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


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

    /**
     * Delete the tBCommand by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBCommand : {}", id);
        tBCommandRepository.deleteById(id);
        tBCommandSearchRepository.deleteById(id);
    }

    /**
     * Search for the tBCommand 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<TBCommandDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBCommands for query {}", query);
        return tBCommandSearchRepository.search(queryStringQuery(query), pageable)
            .map(tBCommandMapper::toDto);
    }
}
