package it.sw.pa.comune.artegna.service.impl;

import it.sw.pa.comune.artegna.domain.Liberatoria;
import it.sw.pa.comune.artegna.repository.LiberatoriaRepository;
import it.sw.pa.comune.artegna.service.LiberatoriaService;
import it.sw.pa.comune.artegna.service.dto.LiberatoriaDTO;
import it.sw.pa.comune.artegna.service.mapper.LiberatoriaMapper;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
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;

/**
 * Service Implementation for managing {@link it.sw.pa.comune.artegna.domain.Liberatoria}.
 */
@Service
@Transactional
public class LiberatoriaServiceImpl implements LiberatoriaService {

    private static final Logger LOG = LoggerFactory.getLogger(LiberatoriaServiceImpl.class);

    private final LiberatoriaRepository liberatoriaRepository;

    private final LiberatoriaMapper liberatoriaMapper;

    public LiberatoriaServiceImpl(LiberatoriaRepository liberatoriaRepository, LiberatoriaMapper liberatoriaMapper) {
        this.liberatoriaRepository = liberatoriaRepository;
        this.liberatoriaMapper = liberatoriaMapper;
    }

    @Override
    public LiberatoriaDTO save(LiberatoriaDTO liberatoriaDTO) {
        LOG.debug("Request to save Liberatoria : {}", liberatoriaDTO);
        Liberatoria liberatoria = liberatoriaMapper.toEntity(liberatoriaDTO);
        liberatoria = liberatoriaRepository.save(liberatoria);
        return liberatoriaMapper.toDto(liberatoria);
    }

    @Override
    public LiberatoriaDTO update(LiberatoriaDTO liberatoriaDTO) {
        LOG.debug("Request to update Liberatoria : {}", liberatoriaDTO);
        Liberatoria liberatoria = liberatoriaMapper.toEntity(liberatoriaDTO);
        liberatoria = liberatoriaRepository.save(liberatoria);
        return liberatoriaMapper.toDto(liberatoria);
    }

    @Override
    public Optional<LiberatoriaDTO> partialUpdate(LiberatoriaDTO liberatoriaDTO) {
        LOG.debug("Request to partially update Liberatoria : {}", liberatoriaDTO);

        return liberatoriaRepository
            .findById(liberatoriaDTO.getId())
            .map(existingLiberatoria -> {
                liberatoriaMapper.partialUpdate(existingLiberatoria, liberatoriaDTO);

                return existingLiberatoria;
            })
            .map(liberatoriaRepository::save)
            .map(liberatoriaMapper::toDto);
    }

    @Override
    @Transactional(readOnly = true)
    public List<LiberatoriaDTO> findAll() {
        LOG.debug("Request to get all Liberatorias");
        return liberatoriaRepository.findAll().stream().map(liberatoriaMapper::toDto).collect(Collectors.toCollection(LinkedList::new));
    }

    public Page<LiberatoriaDTO> findAllWithEagerRelationships(Pageable pageable) {
        return liberatoriaRepository.findAllWithEagerRelationships(pageable).map(liberatoriaMapper::toDto);
    }

    @Override
    @Transactional(readOnly = true)
    public Optional<LiberatoriaDTO> findOne(Long id) {
        LOG.debug("Request to get Liberatoria : {}", id);
        return liberatoriaRepository.findOneWithEagerRelationships(id).map(liberatoriaMapper::toDto);
    }

    @Override
    public void delete(Long id) {
        LOG.debug("Request to delete Liberatoria : {}", id);
        liberatoriaRepository.deleteById(id);
    }
}
