Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.5 kB
2
Indexable
Never
@Service
public class ProductImageServiceImpl implements PersistProductImageService {

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

    @Override
    @Transactional
    public String 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 {
            createImage(product, input);
            blobService.commitFile(input.fileName, input.version, input.targetObjectId);
            return product.getId().toString();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new ServiceException(ErrorCategory.BAD_REQUEST, translation.getFormattedMessage(TranslationConstants.ERRO_PERSISTIR_IMAGEM, e.getMessage()));
        }
    }

    private void 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);
        product.setE075derImage(image);
        e075derBaseRepository.save(product);
    }
}