package com.emonster.taroaichat.service;

import com.emonster.taroaichat.domain.TarotCard;
import com.emonster.taroaichat.repository.TarotCardRepository;
import com.emonster.taroaichat.service.dto.TarotCardDTO;
import com.emonster.taroaichat.service.mapper.TarotCardMapper;
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 com.emonster.taroaichat.domain.TarotCard}.
 */
@Service
@Transactional
public class TarotCardService {

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

    private final TarotCardRepository tarotCardRepository;

    private final TarotCardMapper tarotCardMapper;

    public TarotCardService(TarotCardRepository tarotCardRepository, TarotCardMapper tarotCardMapper) {
        this.tarotCardRepository = tarotCardRepository;
        this.tarotCardMapper = tarotCardMapper;
    }

    /**
     * Save a tarotCard.
     *
     * @param tarotCardDTO the entity to save.
     * @return the persisted entity.
     */
    public TarotCardDTO save(TarotCardDTO tarotCardDTO) {
        LOG.debug("Request to save TarotCard : {}", tarotCardDTO);
        TarotCard tarotCard = tarotCardMapper.toEntity(tarotCardDTO);
        tarotCard = tarotCardRepository.save(tarotCard);
        return tarotCardMapper.toDto(tarotCard);
    }

    /**
     * Update a tarotCard.
     *
     * @param tarotCardDTO the entity to save.
     * @return the persisted entity.
     */
    public TarotCardDTO update(TarotCardDTO tarotCardDTO) {
        LOG.debug("Request to update TarotCard : {}", tarotCardDTO);
        TarotCard tarotCard = tarotCardMapper.toEntity(tarotCardDTO);
        tarotCard.setIsPersisted();
        tarotCard = tarotCardRepository.save(tarotCard);
        return tarotCardMapper.toDto(tarotCard);
    }

    /**
     * Partially update a tarotCard.
     *
     * @param tarotCardDTO the entity to update partially.
     * @return the persisted entity.
     */
    public Optional<TarotCardDTO> partialUpdate(TarotCardDTO tarotCardDTO) {
        LOG.debug("Request to partially update TarotCard : {}", tarotCardDTO);

        return tarotCardRepository
            .findById(tarotCardDTO.getId())
            .map(existingTarotCard -> {
                tarotCardMapper.partialUpdate(existingTarotCard, tarotCardDTO);

                return existingTarotCard;
            })
            .map(tarotCardRepository::save)
            .map(tarotCardMapper::toDto);
    }

    /**
     * Get one tarotCard by id.
     *
     * @param id the id of the entity.
     * @return the entity.
     */
    @Transactional(readOnly = true)
    public Optional<TarotCardDTO> findOne(Long id) {
        LOG.debug("Request to get TarotCard : {}", id);
        return tarotCardRepository.findById(id).map(tarotCardMapper::toDto);
    }

    /**
     * Delete the tarotCard by id.
     *
     * @param id the id of the entity.
     */
    public void delete(Long id) {
        LOG.debug("Request to delete TarotCard : {}", id);
        tarotCardRepository.deleteById(id);
    }
}
