Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.1 kB
2
Indexable
Never
package br.com.senior.erpxfnd.produto.service;

import java.util.UUID;

import javax.inject.Inject;
import javax.transaction.Transactional;

import org.springframework.stereotype.Service;

import br.com.senior.erpxfnd.produto.E075derImageBaseRepository;
import br.com.senior.erpxfnd.produto.E075derImageEntity;
import br.com.senior.messaging.ErrorCategory;
import br.com.senior.messaging.model.ServiceException;
import br.com.senior.platform.blobservice.BlobDetails;
import br.com.senior.platform.translationhub.api.TranslationHubApi;
import br.com.senior.utils.TranslationConstants;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class DeleteProductImageServiceImpl implements DeleteProductImageService {

    @Inject
    private E075derImageBaseRepository e075derImageBaseRepository;
    @Inject
    private TranslationHubApi translation;
    @Inject
    private BlobService blobService;

    @Override
    @Transactional
    public void deleteProductImage(String productId) {
        E075derImageEntity productImage = e075derImageBaseRepository.findById(UUID.fromString(productId))
                .orElseThrow(() -> new ServiceException(ErrorCategory.OBJECT_NOT_FOUND, translation.getMessage(TranslationConstants.IMAGEM_NAO_ECONTRADA_PRODUTO)));
        try {
            var blobDetails = new BlobDetails();
            blobDetails.fileName = productImage.getFileName();
            blobDetails.targetObjectId = productImage.getTargetObjectId();
            boolean isDeleted = blobService.deleteFile(blobDetails);
            if(!isDeleted) {
                throw new ServiceException(ErrorCategory.BAD_REQUEST, translation.getMessage(TranslationConstants.ERRO_EXCLUIR_IMAGEM_PRODUTO));
            }
            e075derImageBaseRepository.delete(productImage);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new ServiceException(ErrorCategory.BAD_REQUEST, translation.getMessage(TranslationConstants.ERRO_EXCLUIR_IMAGEM_PRODUTO));
        }
    }

}