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

import it.sw.pa.comune.artegna.domain.Notifica;
import it.sw.pa.comune.artegna.repository.NotificaRepository;
import it.sw.pa.comune.artegna.service.NotificaService;
import it.sw.pa.comune.artegna.service.dto.NotificaDTO;
import it.sw.pa.comune.artegna.service.mapper.NotificaMapper;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Service Implementation for managing {@link it.sw.pa.comune.artegna.domain.Notifica}.
 */
@Service
@Transactional
public class NotificaServiceImpl implements NotificaService {

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

    private final NotificaRepository notificaRepository;

    private final NotificaMapper notificaMapper;

    public NotificaServiceImpl(NotificaRepository notificaRepository, NotificaMapper notificaMapper) {
        this.notificaRepository = notificaRepository;
        this.notificaMapper = notificaMapper;
    }

    @Override
    public NotificaDTO save(NotificaDTO notificaDTO) {
        LOG.debug("Request to save Notifica : {}", notificaDTO);
        Notifica notifica = notificaMapper.toEntity(notificaDTO);
        notifica = notificaRepository.save(notifica);
        return notificaMapper.toDto(notifica);
    }

    @Override
    public NotificaDTO update(NotificaDTO notificaDTO) {
        LOG.debug("Request to update Notifica : {}", notificaDTO);
        Notifica notifica = notificaMapper.toEntity(notificaDTO);
        notifica = notificaRepository.save(notifica);
        return notificaMapper.toDto(notifica);
    }

    @Override
    public Optional<NotificaDTO> partialUpdate(NotificaDTO notificaDTO) {
        LOG.debug("Request to partially update Notifica : {}", notificaDTO);

        return notificaRepository
            .findById(notificaDTO.getId())
            .map(existingNotifica -> {
                notificaMapper.partialUpdate(existingNotifica, notificaDTO);

                return existingNotifica;
            })
            .map(notificaRepository::save)
            .map(notificaMapper::toDto);
    }

    @Override
    @Transactional(readOnly = true)
    public Optional<NotificaDTO> findOne(Long id) {
        LOG.debug("Request to get Notifica : {}", id);
        return notificaRepository.findById(id).map(notificaMapper::toDto);
    }

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