package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBWardService;
import vn.azteam.tpf.domain.TBWard;
import vn.azteam.tpf.repository.TBWardRepository;
import vn.azteam.tpf.repository.search.TBWardSearchRepository;
import vn.azteam.tpf.service.dto.TBWardDTO;
import vn.azteam.tpf.service.mapper.TBWardMapper;
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 TBWard.
 */
@Service
@Transactional
public class TBWardServiceImpl implements TBWardService {

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

    private final TBWardRepository tBWardRepository;

    private final TBWardMapper tBWardMapper;

    private final TBWardSearchRepository tBWardSearchRepository;

    public TBWardServiceImpl(TBWardRepository tBWardRepository, TBWardMapper tBWardMapper, TBWardSearchRepository tBWardSearchRepository) {
        this.tBWardRepository = tBWardRepository;
        this.tBWardMapper = tBWardMapper;
        this.tBWardSearchRepository = tBWardSearchRepository;
    }

    /**
     * Save a tBWard.
     *
     * @param tBWardDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBWardDTO save(TBWardDTO tBWardDTO) {
        log.debug("Request to save TBWard : {}", tBWardDTO);

        TBWard tBWard = tBWardMapper.toEntity(tBWardDTO);
        tBWard = tBWardRepository.save(tBWard);
        TBWardDTO result = tBWardMapper.toDto(tBWard);
        tBWardSearchRepository.save(tBWard);
        return result;
    }

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


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

    /**
     * Delete the tBWard by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBWard : {}", id);
        tBWardRepository.deleteById(id);
        tBWardSearchRepository.deleteById(id);
    }

    /**
     * Search for the tBWard 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<TBWardDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBWards for query {}", query);
        return tBWardSearchRepository.search(queryStringQuery(query), pageable)
            .map(tBWardMapper::toDto);
    }
}
