package com.bares.service.impl;

import com.bares.service.ArticuloService;
import com.bares.domain.Articulo;
import com.bares.repository.ArticuloRepository;
import com.bares.service.dto.ArticuloDTO;
import com.bares.service.mapper.ArticuloMapper;
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 Articulo.
 */
@Service
@Transactional
public class ArticuloServiceImpl implements ArticuloService {

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

    private final ArticuloRepository articuloRepository;

    private final ArticuloMapper articuloMapper;

    public ArticuloServiceImpl(ArticuloRepository articuloRepository, ArticuloMapper articuloMapper) {
        this.articuloRepository = articuloRepository;
        this.articuloMapper = articuloMapper;
    }

    /**
     * Save a articulo.
     *
     * @param articuloDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public ArticuloDTO save(ArticuloDTO articuloDTO) {
        log.debug("Request to save Articulo : {}", articuloDTO);
        Articulo articulo = articuloMapper.toEntity(articuloDTO);
        articulo = articuloRepository.save(articulo);
        return articuloMapper.toDto(articulo);
    }

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


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

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