package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBNurseryDetailsService;
import vn.azteam.tpf.domain.TBNurseryDetails;
import vn.azteam.tpf.repository.TBNurseryDetailsRepository;
import vn.azteam.tpf.repository.search.TBNurseryDetailsSearchRepository;
import vn.azteam.tpf.service.dto.TBNurseryDetailsDTO;
import vn.azteam.tpf.service.mapper.TBNurseryDetailsMapper;
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 TBNurseryDetails.
 */
@Service
@Transactional
public class TBNurseryDetailsServiceImpl implements TBNurseryDetailsService {

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

    private final TBNurseryDetailsRepository tBNurseryDetailsRepository;

    private final TBNurseryDetailsMapper tBNurseryDetailsMapper;

    private final TBNurseryDetailsSearchRepository tBNurseryDetailsSearchRepository;

    public TBNurseryDetailsServiceImpl(TBNurseryDetailsRepository tBNurseryDetailsRepository, TBNurseryDetailsMapper tBNurseryDetailsMapper, TBNurseryDetailsSearchRepository tBNurseryDetailsSearchRepository) {
        this.tBNurseryDetailsRepository = tBNurseryDetailsRepository;
        this.tBNurseryDetailsMapper = tBNurseryDetailsMapper;
        this.tBNurseryDetailsSearchRepository = tBNurseryDetailsSearchRepository;
    }

    /**
     * Save a tBNurseryDetails.
     *
     * @param tBNurseryDetailsDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBNurseryDetailsDTO save(TBNurseryDetailsDTO tBNurseryDetailsDTO) {
        log.debug("Request to save TBNurseryDetails : {}", tBNurseryDetailsDTO);

        TBNurseryDetails tBNurseryDetails = tBNurseryDetailsMapper.toEntity(tBNurseryDetailsDTO);
        tBNurseryDetails = tBNurseryDetailsRepository.save(tBNurseryDetails);
        TBNurseryDetailsDTO result = tBNurseryDetailsMapper.toDto(tBNurseryDetails);
        tBNurseryDetailsSearchRepository.save(tBNurseryDetails);
        return result;
    }

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


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

    /**
     * Delete the tBNurseryDetails by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBNurseryDetails : {}", id);
        tBNurseryDetailsRepository.deleteById(id);
        tBNurseryDetailsSearchRepository.deleteById(id);
    }

    /**
     * Search for the tBNurseryDetails 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<TBNurseryDetailsDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBNurseryDetails for query {}", query);
        return tBNurseryDetailsSearchRepository.search(queryStringQuery(query), pageable)
            .map(tBNurseryDetailsMapper::toDto);
    }
}
