Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.3 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.E075derBaseRepository;
import br.com.senior.erpxfnd.produto.E075derEntity;
import br.com.senior.erpxfnd.produto.E075derImageBaseRepository;
import br.com.senior.erpxfnd.produto.E075derImageEntity;
import br.com.senior.erpxfnd.produto.PersistProductImageInput;
import br.com.senior.messaging.ErrorCategory;
import br.com.senior.messaging.model.ServiceException;
import br.com.senior.platform.translationhub.api.TranslationHubApi;
import br.com.senior.utils.TranslationConstants;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class ProductImageServiceImpl {

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

    @Transactional
    public void persistProductImage(PersistProductImageInput input) {
        E075derEntity product = e075derBaseRepository.findById(UUID.fromString(input.productId))
                .orElseThrow(() -> new ServiceException(ErrorCategory.OBJECT_NOT_FOUND, translation.getMessage(TranslationConstants.PRODUTO_IMAGEM_NAO_ECONTRADO)));

        try {
            E075derImageEntity image = createImage(product, input);
            blobService.commitFile(image.getFileName(), image.getVersion(), image.getTargetObjectId());
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new ServiceException(ErrorCategory.BAD_REQUEST, translation.getFormattedMessage(TranslationConstants.ERRO_PERSISTIR_IMAGEM, e.getMessage()));
        }
    }
    
    private E075derImageEntity createImage(E075derEntity product, PersistProductImageInput input) {
        var image = new E075derImageEntity();
        image.setE075der(product);
        image.setTargetObjectId(input.targetObjectId);
        image.setExcluido(false);
        image.setFileName(input.fileName);
        image.setVersion(input.version);
        return e075derImageBaseRepository.save(image);
    }
}