package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBNotificationService;
import vn.azteam.tpf.domain.TBNotification;
import vn.azteam.tpf.repository.TBNotificationRepository;
import vn.azteam.tpf.repository.search.TBNotificationSearchRepository;
import vn.azteam.tpf.service.dto.TBNotificationDTO;
import vn.azteam.tpf.service.mapper.TBNotificationMapper;
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 TBNotification.
 */
@Service
@Transactional
public class TBNotificationServiceImpl implements TBNotificationService {

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

    private final TBNotificationRepository tBNotificationRepository;

    private final TBNotificationMapper tBNotificationMapper;

    private final TBNotificationSearchRepository tBNotificationSearchRepository;

    public TBNotificationServiceImpl(TBNotificationRepository tBNotificationRepository, TBNotificationMapper tBNotificationMapper, TBNotificationSearchRepository tBNotificationSearchRepository) {
        this.tBNotificationRepository = tBNotificationRepository;
        this.tBNotificationMapper = tBNotificationMapper;
        this.tBNotificationSearchRepository = tBNotificationSearchRepository;
    }

    /**
     * Save a tBNotification.
     *
     * @param tBNotificationDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBNotificationDTO save(TBNotificationDTO tBNotificationDTO) {
        log.debug("Request to save TBNotification : {}", tBNotificationDTO);

        TBNotification tBNotification = tBNotificationMapper.toEntity(tBNotificationDTO);
        tBNotification = tBNotificationRepository.save(tBNotification);
        TBNotificationDTO result = tBNotificationMapper.toDto(tBNotification);
        tBNotificationSearchRepository.save(tBNotification);
        return result;
    }

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


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

    /**
     * Delete the tBNotification by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBNotification : {}", id);
        tBNotificationRepository.deleteById(id);
        tBNotificationSearchRepository.deleteById(id);
    }

    /**
     * Search for the tBNotification 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<TBNotificationDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBNotifications for query {}", query);
        return tBNotificationSearchRepository.search(queryStringQuery(query), pageable)
            .map(tBNotificationMapper::toDto);
    }
}
