package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBEquipmentService;
import vn.azteam.tpf.domain.TBEquipment;
import vn.azteam.tpf.repository.TBEquipmentRepository;
import vn.azteam.tpf.repository.search.TBEquipmentSearchRepository;
import vn.azteam.tpf.service.dto.TBEquipmentDTO;
import vn.azteam.tpf.service.mapper.TBEquipmentMapper;
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 TBEquipment.
 */
@Service
@Transactional
public class TBEquipmentServiceImpl implements TBEquipmentService {

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

    private final TBEquipmentRepository tBEquipmentRepository;

    private final TBEquipmentMapper tBEquipmentMapper;

    private final TBEquipmentSearchRepository tBEquipmentSearchRepository;

    public TBEquipmentServiceImpl(TBEquipmentRepository tBEquipmentRepository, TBEquipmentMapper tBEquipmentMapper, TBEquipmentSearchRepository tBEquipmentSearchRepository) {
        this.tBEquipmentRepository = tBEquipmentRepository;
        this.tBEquipmentMapper = tBEquipmentMapper;
        this.tBEquipmentSearchRepository = tBEquipmentSearchRepository;
    }

    /**
     * Save a tBEquipment.
     *
     * @param tBEquipmentDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBEquipmentDTO save(TBEquipmentDTO tBEquipmentDTO) {
        log.debug("Request to save TBEquipment : {}", tBEquipmentDTO);

        TBEquipment tBEquipment = tBEquipmentMapper.toEntity(tBEquipmentDTO);
        tBEquipment = tBEquipmentRepository.save(tBEquipment);
        TBEquipmentDTO result = tBEquipmentMapper.toDto(tBEquipment);
        tBEquipmentSearchRepository.save(tBEquipment);
        return result;
    }

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


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

    /**
     * Delete the tBEquipment by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBEquipment : {}", id);
        tBEquipmentRepository.deleteById(id);
        tBEquipmentSearchRepository.deleteById(id);
    }

    /**
     * Search for the tBEquipment 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<TBEquipmentDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBEquipments for query {}", query);
        return tBEquipmentSearchRepository.search(queryStringQuery(query), pageable)
            .map(tBEquipmentMapper::toDto);
    }
}
