package com.bares.service.impl;

import com.bares.service.MesaService;
import com.bares.domain.Mesa;
import com.bares.repository.MesaRepository;
import com.bares.service.dto.MesaDTO;
import com.bares.service.mapper.MesaMapper;
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 Mesa.
 */
@Service
@Transactional
public class MesaServiceImpl implements MesaService {

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

    private final MesaRepository mesaRepository;

    private final MesaMapper mesaMapper;

    public MesaServiceImpl(MesaRepository mesaRepository, MesaMapper mesaMapper) {
        this.mesaRepository = mesaRepository;
        this.mesaMapper = mesaMapper;
    }

    /**
     * Save a mesa.
     *
     * @param mesaDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public MesaDTO save(MesaDTO mesaDTO) {
        log.debug("Request to save Mesa : {}", mesaDTO);
        Mesa mesa = mesaMapper.toEntity(mesaDTO);
        mesa = mesaRepository.save(mesa);
        return mesaMapper.toDto(mesa);
    }

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


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

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