package com.bares.service.impl;

import com.bares.service.BarService;
import com.bares.domain.Bar;
import com.bares.repository.BarRepository;
import com.bares.service.dto.BarDTO;
import com.bares.service.mapper.BarMapper;
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;

/**
 * Service Implementation for managing Bar.
 */
@Service
@Transactional
public class BarServiceImpl implements BarService {

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

    private final BarRepository barRepository;

    private final BarMapper barMapper;

    public BarServiceImpl(BarRepository barRepository, BarMapper barMapper) {
        this.barRepository = barRepository;
        this.barMapper = barMapper;
    }

    /**
     * Save a bar.
     *
     * @param barDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public BarDTO save(BarDTO barDTO) {
        log.debug("Request to save Bar : {}", barDTO);
        Bar bar = barMapper.toEntity(barDTO);
        bar = barRepository.save(bar);
        return barMapper.toDto(bar);
    }

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


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

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