package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBEnvironmentUpdateService;
import vn.azteam.tpf.domain.TBEnvironmentUpdate;
import vn.azteam.tpf.repository.TBEnvironmentUpdateRepository;
import vn.azteam.tpf.repository.search.TBEnvironmentUpdateSearchRepository;
import vn.azteam.tpf.service.dto.TBEnvironmentUpdateDTO;
import vn.azteam.tpf.service.mapper.TBEnvironmentUpdateMapper;
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 TBEnvironmentUpdate.
 */
@Service
@Transactional
public class TBEnvironmentUpdateServiceImpl implements TBEnvironmentUpdateService {

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

    private final TBEnvironmentUpdateRepository tBEnvironmentUpdateRepository;

    private final TBEnvironmentUpdateMapper tBEnvironmentUpdateMapper;

    private final TBEnvironmentUpdateSearchRepository tBEnvironmentUpdateSearchRepository;

    public TBEnvironmentUpdateServiceImpl(TBEnvironmentUpdateRepository tBEnvironmentUpdateRepository, TBEnvironmentUpdateMapper tBEnvironmentUpdateMapper, TBEnvironmentUpdateSearchRepository tBEnvironmentUpdateSearchRepository) {
        this.tBEnvironmentUpdateRepository = tBEnvironmentUpdateRepository;
        this.tBEnvironmentUpdateMapper = tBEnvironmentUpdateMapper;
        this.tBEnvironmentUpdateSearchRepository = tBEnvironmentUpdateSearchRepository;
    }

    /**
     * Save a tBEnvironmentUpdate.
     *
     * @param tBEnvironmentUpdateDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBEnvironmentUpdateDTO save(TBEnvironmentUpdateDTO tBEnvironmentUpdateDTO) {
        log.debug("Request to save TBEnvironmentUpdate : {}", tBEnvironmentUpdateDTO);

        TBEnvironmentUpdate tBEnvironmentUpdate = tBEnvironmentUpdateMapper.toEntity(tBEnvironmentUpdateDTO);
        tBEnvironmentUpdate = tBEnvironmentUpdateRepository.save(tBEnvironmentUpdate);
        TBEnvironmentUpdateDTO result = tBEnvironmentUpdateMapper.toDto(tBEnvironmentUpdate);
        tBEnvironmentUpdateSearchRepository.save(tBEnvironmentUpdate);
        return result;
    }

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


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

    /**
     * Delete the tBEnvironmentUpdate by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBEnvironmentUpdate : {}", id);
        tBEnvironmentUpdateRepository.deleteById(id);
        tBEnvironmentUpdateSearchRepository.deleteById(id);
    }

    /**
     * Search for the tBEnvironmentUpdate 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<TBEnvironmentUpdateDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBEnvironmentUpdates for query {}", query);
        return tBEnvironmentUpdateSearchRepository.search(queryStringQuery(query), pageable)
            .map(tBEnvironmentUpdateMapper::toDto);
    }
}
