package vn.azteam.tpf.service.impl;

import vn.azteam.tpf.service.TBAddressService;
import vn.azteam.tpf.domain.TBAddress;
import vn.azteam.tpf.repository.TBAddressRepository;
import vn.azteam.tpf.repository.search.TBAddressSearchRepository;
import vn.azteam.tpf.service.dto.TBAddressDTO;
import vn.azteam.tpf.service.mapper.TBAddressMapper;
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 TBAddress.
 */
@Service
@Transactional
public class TBAddressServiceImpl implements TBAddressService {

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

    private final TBAddressRepository tBAddressRepository;

    private final TBAddressMapper tBAddressMapper;

    private final TBAddressSearchRepository tBAddressSearchRepository;

    public TBAddressServiceImpl(TBAddressRepository tBAddressRepository, TBAddressMapper tBAddressMapper, TBAddressSearchRepository tBAddressSearchRepository) {
        this.tBAddressRepository = tBAddressRepository;
        this.tBAddressMapper = tBAddressMapper;
        this.tBAddressSearchRepository = tBAddressSearchRepository;
    }

    /**
     * Save a tBAddress.
     *
     * @param tBAddressDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public TBAddressDTO save(TBAddressDTO tBAddressDTO) {
        log.debug("Request to save TBAddress : {}", tBAddressDTO);

        TBAddress tBAddress = tBAddressMapper.toEntity(tBAddressDTO);
        tBAddress = tBAddressRepository.save(tBAddress);
        TBAddressDTO result = tBAddressMapper.toDto(tBAddress);
        tBAddressSearchRepository.save(tBAddress);
        return result;
    }

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


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

    /**
     * Delete the tBAddress by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete TBAddress : {}", id);
        tBAddressRepository.deleteById(id);
        tBAddressSearchRepository.deleteById(id);
    }

    /**
     * Search for the tBAddress 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<TBAddressDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of TBAddresses for query {}", query);
        return tBAddressSearchRepository.search(queryStringQuery(query), pageable)
            .map(tBAddressMapper::toDto);
    }
}
