backend/src/main/java/com/magamochi/image/queue/consumer/ImageFetchConsumer.java

32 lines
1.1 KiB
Java

package com.magamochi.image.queue.consumer;
import static java.util.Objects.nonNull;
import com.magamochi.common.queue.command.ImageFetchCommand;
import com.magamochi.common.queue.command.ImageUpdateCommand;
import com.magamochi.image.queue.producer.ImageUpdateProducer;
import com.magamochi.image.service.ImageFetchService;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;
@Log4j2
@Service
@RequiredArgsConstructor
public class ImageFetchConsumer {
private final ImageFetchService imageFetchService;
private final ImageUpdateProducer imageUpdateProducer;
@RabbitListener(queues = "${queues.image-fetch}")
public void receiveImageFetchCommand(ImageFetchCommand command) {
log.info("Received image fetch command: {}", command);
var imageId = imageFetchService.fetchImage(command.url(), command.contentType());
if (nonNull(imageId)) {
imageUpdateProducer.publishImageUpdateCommand(
new ImageUpdateCommand(command.entityId(), imageId), command.contentType());
}
}
}