package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBWaterTypeService;
import vn.azteam.tpf.domain.TBWaterType;
import vn.azteam.tpf.repository.TBWaterTypeRepository;
import vn.azteam.tpf.repository.search.TBWaterTypeSearchRepository;
import vn.azteam.tpf.service.dto.TBWaterTypeDTO;
import vn.azteam.tpf.service.mapper.TBWaterTypeMapper;
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 TBWaterType.
 */
@Service
@Transactional
public class TBWaterTypeServiceImpl implements TBWaterTypeService {

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

    private final TBWaterTypeRepository tBWaterTypeRepository;

    private final TBWaterTypeMapper tBWaterTypeMapper;

    private final TBWaterTypeSearchRepository tBWaterTypeSearchRepository;

    public TBWaterTypeServiceImpl(TBWaterTypeRepository tBWaterTypeRepository, TBWaterTypeMapper tBWaterTypeMapper, TBWaterTypeSearchRepository tBWaterTypeSearchRepository) {
        this.tBWaterTypeRepository = tBWaterTypeRepository;
        this.tBWaterTypeMapper = tBWaterTypeMapper;
        this.tBWaterTypeSearchRepository = tBWaterTypeSearchRepository;
    }

    /**
     * Save a tBWaterType.
     *
     * @param tBWaterTypeDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBWaterTypeDTO save(TBWaterTypeDTO tBWaterTypeDTO) {
        log.debug("Request to save TBWaterType : {}", tBWaterTypeDTO);

        TBWaterType tBWaterType = tBWaterTypeMapper.toEntity(tBWaterTypeDTO);
        tBWaterType = tBWaterTypeRepository.save(tBWaterType);
        TBWaterTypeDTO result = tBWaterTypeMapper.toDto(tBWaterType);
        tBWaterTypeSearchRepository.save(tBWaterType);
        return result;
    }

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


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

    /**
     * Delete the tBWaterType by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBWaterType : {}", id);
        tBWaterTypeRepository.deleteById(id);
        tBWaterTypeSearchRepository.deleteById(id);
    }

    /**
     * Search for the tBWaterType 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<TBWaterTypeDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBWaterTypes for query {}", query);
        return tBWaterTypeSearchRepository.search(queryStringQuery(query), pageable)
            .map(tBWaterTypeMapper::toDto);
    }
}
