package com.bares.service.impl;

import com.bares.service.ComandaService;
import com.bares.domain.Comanda;
import com.bares.repository.ComandaRepository;
import com.bares.service.dto.ComandaDTO;
import com.bares.service.mapper.ComandaMapper;
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 Comanda.
 */
@Service
@Transactional
public class ComandaServiceImpl implements ComandaService {

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

    private final ComandaRepository comandaRepository;

    private final ComandaMapper comandaMapper;

    public ComandaServiceImpl(ComandaRepository comandaRepository, ComandaMapper comandaMapper) {
        this.comandaRepository = comandaRepository;
        this.comandaMapper = comandaMapper;
    }

    /**
     * Save a comanda.
     *
     * @param comandaDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public ComandaDTO save(ComandaDTO comandaDTO) {
        log.debug("Request to save Comanda : {}", comandaDTO);
        Comanda comanda = comandaMapper.toEntity(comandaDTO);
        comanda = comandaRepository.save(comanda);
        return comandaMapper.toDto(comanda);
    }

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


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

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