-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adiciona endpoint de inicia-preparo
- Loading branch information
Showing
21 changed files
with
315 additions
and
20 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
...idoapi/application/controllers/cozinha/pedidos/preparo/IniciaPreparoPedidoController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.fiap.mspedidoapi.application.controllers.cozinha.pedidos.preparo; | ||
|
||
import com.fiap.mspedidoapi.application.controllers.cozinha.pedidos.preparo.requests.StoreIniciaPreparoProdutoRequest; | ||
import com.fiap.mspedidoapi.application.response.GenericResponse; | ||
import com.fiap.mspedidoapi.application.response.PresenterResponse; | ||
import com.fiap.mspedidoapi.domain.generic.output.OutputInterface; | ||
import com.fiap.mspedidoapi.domain.output.pedido.PedidoProntoOutput; | ||
import com.fiap.mspedidoapi.domain.presenters.pedido.PedidoProntoPresenter; | ||
import com.fiap.mspedidoapi.domain.useCase.pedido.IniciaPreparoPedidoUseCase; | ||
import com.fiap.mspedidoapi.infra.adpter.repository.pedido.PreparaPedidoRepository; | ||
import com.fiap.mspedidoapi.infra.kafka.producers.PreparaPedidoProducer; | ||
import com.fiap.mspedidoapi.infra.repository.PedidosMongoRepository; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("cozinha/pedido") | ||
public class IniciaPreparoPedidoController { | ||
private final PedidosMongoRepository pedidosMongoRepository; | ||
@Value("${spring.kafka.producer.bootstrap-servers}") | ||
private String servers; | ||
|
||
@PostMapping("/inicia-preparo") | ||
@Operation(tags = {"cozinha"}) | ||
public ResponseEntity<Object> iniciaPreparoPedido(@RequestBody StoreIniciaPreparoProdutoRequest iniciaPreparoProdutoRequest) { | ||
IniciaPreparoPedidoUseCase useCase = new IniciaPreparoPedidoUseCase( | ||
new PreparaPedidoRepository(pedidosMongoRepository), | ||
new PreparaPedidoProducer(servers)); | ||
useCase.execute(iniciaPreparoProdutoRequest.uuid(), iniciaPreparoProdutoRequest.tempoDePreparo()); | ||
OutputInterface outputInterface = useCase.getOutputInterface(); | ||
|
||
if (outputInterface.getOutputStatus().getCode() != 200) { | ||
return new GenericResponse().response(outputInterface); | ||
} | ||
|
||
PedidoProntoPresenter presenter = new PedidoProntoPresenter((PedidoProntoOutput) outputInterface); | ||
return new PresenterResponse().response(presenter); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...cation/controllers/cozinha/pedidos/preparo/requests/StoreIniciaPreparoProdutoRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.fiap.mspedidoapi.application.controllers.cozinha.pedidos.preparo.requests; | ||
|
||
import java.util.UUID; | ||
|
||
public record StoreIniciaPreparoProdutoRequest( | ||
UUID uuid, | ||
Integer tempoDePreparo | ||
){} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/main/java/com/fiap/mspedidoapi/domain/exception/pedido/PedidoNaoEncontradoException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.fiap.mspedidoapi.domain.exception.pedido; | ||
|
||
public class PedidoNaoEncontradoException extends Exception { | ||
public PedidoNaoEncontradoException(String message) { | ||
super(message); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/fiap/mspedidoapi/domain/gateway/pedido/PreparaPedidoInterface.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.fiap.mspedidoapi.domain.gateway.pedido; | ||
|
||
import com.fiap.mspedidoapi.domain.entity.pedido.PedidoEntity; | ||
import com.fiap.mspedidoapi.domain.exception.pedido.PedidoNaoEncontradoException; | ||
|
||
import java.util.UUID; | ||
|
||
public interface PreparaPedidoInterface { | ||
PedidoEntity encontraPedidoPorUuid(UUID pedidoUuid) throws PedidoNaoEncontradoException; | ||
PedidoEntity movePedidoParaEmPreparacao(UUID pedidoUuid, Integer tempoDePreparoEmMinutos) throws PedidoNaoEncontradoException; | ||
} |
7 changes: 7 additions & 0 deletions
7
...in/java/com/fiap/mspedidoapi/domain/gateway/producers/PreparaPedidoProducerInterface.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.fiap.mspedidoapi.domain.gateway.producers; | ||
|
||
import com.fiap.mspedidoapi.domain.output.pedido.PedidoEmPreparacaoOutput; | ||
|
||
public interface PreparaPedidoProducerInterface { | ||
void send(PedidoEmPreparacaoOutput pedidoEmPreparacaoOutput); | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/com/fiap/mspedidoapi/domain/output/pedido/PedidoEmPreparacaoOutput.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.fiap.mspedidoapi.domain.output.pedido; | ||
|
||
import com.fiap.mspedidoapi.domain.entity.pedido.PedidoEntity; | ||
import com.fiap.mspedidoapi.domain.generic.output.OutputInterface; | ||
import com.fiap.mspedidoapi.domain.generic.output.OutputStatus; | ||
import lombok.Data; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.Setter; | ||
|
||
@Data | ||
@RequiredArgsConstructor | ||
@Getter | ||
@Setter | ||
public class PedidoEmPreparacaoOutput implements OutputInterface { | ||
private PedidoEntity pedido; | ||
private OutputStatus outputStatus; | ||
|
||
public PedidoEmPreparacaoOutput(PedidoEntity pedido, OutputStatus outputStatus) { | ||
this.pedido = pedido; | ||
this.outputStatus = outputStatus; | ||
} | ||
|
||
@Override | ||
public Object getBody() { | ||
return this.pedido; | ||
} | ||
|
||
@Override | ||
public OutputStatus getOutputStatus() { | ||
return this.outputStatus; | ||
} | ||
|
||
} |
73 changes: 73 additions & 0 deletions
73
src/main/java/com/fiap/mspedidoapi/domain/useCase/pedido/IniciaPreparoPedidoUseCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.fiap.mspedidoapi.domain.useCase.pedido; | ||
|
||
import com.fiap.mspedidoapi.domain.entity.pedido.PedidoEntity; | ||
import com.fiap.mspedidoapi.domain.enums.pedido.StatusPedido; | ||
import com.fiap.mspedidoapi.domain.exception.pedido.PedidoNaoEncontradoException; | ||
import com.fiap.mspedidoapi.domain.gateway.pedido.PreparaPedidoInterface; | ||
import com.fiap.mspedidoapi.domain.gateway.producers.PreparaPedidoProducerInterface; | ||
import com.fiap.mspedidoapi.domain.generic.output.OutputError; | ||
import com.fiap.mspedidoapi.domain.generic.output.OutputInterface; | ||
import com.fiap.mspedidoapi.domain.generic.output.OutputStatus; | ||
import com.fiap.mspedidoapi.domain.output.pedido.PedidoEmPreparacaoOutput; | ||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
import java.util.UUID; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public class IniciaPreparoPedidoUseCase { | ||
|
||
private final PreparaPedidoInterface preparaPedidoInterface; | ||
private final PreparaPedidoProducerInterface preparaPedidoProducerInterface; | ||
private OutputInterface outputInterface; | ||
|
||
public void execute(UUID pedidoUUID, Integer tempoDePreparoEmMinutos) { | ||
try { | ||
|
||
if(tempoDePreparoEmMinutos <= 0){ | ||
this.outputInterface = new OutputError( | ||
"Error", | ||
new OutputStatus(404, "Falha", "Tempo de preparo precisa ser positivo") | ||
); | ||
return; | ||
} | ||
|
||
PedidoEntity pedidoEncontrado = preparaPedidoInterface.encontraPedidoPorUuid(pedidoUUID); | ||
|
||
if(pedidoEncontrado.getStatusPedido() != StatusPedido.RECEBIDO) { | ||
this.outputInterface = new OutputError( | ||
"Error", | ||
new OutputStatus(404, "Status invalido", "Pedido precisa estar com status RECEBIDO para dar inicio ao preparo") | ||
); | ||
return; | ||
} | ||
|
||
PedidoEntity pedidoEmPreparacao = preparaPedidoInterface.movePedidoParaEmPreparacao(pedidoUUID, tempoDePreparoEmMinutos); | ||
this.outputInterface = new PedidoEmPreparacaoOutput( | ||
pedidoEmPreparacao, | ||
new OutputStatus(200, "OK", "Pedido atualizado.") | ||
); | ||
|
||
}catch (PedidoNaoEncontradoException e) { | ||
System.out.println("Pedido nao enconstrado"); | ||
this.outputInterface = new OutputError( | ||
e.getMessage(), | ||
new OutputStatus(404, "Not found", "Pedido não encontrado") | ||
); | ||
} catch (Exception e) { | ||
outputInterface = new OutputError( | ||
e.getMessage(), | ||
new OutputStatus(500, "Internal Server Error", "Erro no servidor") | ||
); | ||
} finally { | ||
if (this.preparaPedidoProducerInterface != null | ||
&& this.outputInterface instanceof PedidoEmPreparacaoOutput && | ||
this.outputInterface.getOutputStatus().getCode() == 200 | ||
) { | ||
this.preparaPedidoProducerInterface.send((PedidoEmPreparacaoOutput) outputInterface); | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...ain/java/com/fiap/mspedidoapi/infra/adpter/repository/pedido/PreparaPedidoRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.fiap.mspedidoapi.infra.adpter.repository.pedido; | ||
|
||
import com.fiap.mspedidoapi.domain.enums.pedido.StatusPedido; | ||
import com.fiap.mspedidoapi.domain.exception.pedido.PedidoNaoEncontradoException; | ||
import com.fiap.mspedidoapi.domain.gateway.pedido.PreparaPedidoInterface; | ||
import com.fiap.mspedidoapi.infra.collection.pedido.Pedido; | ||
import com.fiap.mspedidoapi.infra.repository.PedidosMongoRepository; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
import java.util.Optional; | ||
import java.util.UUID; | ||
|
||
@RequiredArgsConstructor | ||
public class PreparaPedidoRepository implements PreparaPedidoInterface{ | ||
public final PedidosMongoRepository pedidosMongoRepository; | ||
|
||
@Override | ||
public com.fiap.mspedidoapi.domain.entity.pedido.PedidoEntity encontraPedidoPorUuid(UUID pedidoUuid) throws PedidoNaoEncontradoException { | ||
Optional<Pedido> pedidoEncontrado = pedidosMongoRepository.findByUuidPedido(pedidoUuid); | ||
if(pedidoEncontrado.isPresent()){ | ||
Pedido pedidoModel = pedidoEncontrado.get(); | ||
return new com.fiap.mspedidoapi.domain.entity.pedido.PedidoEntity( | ||
pedidoModel.getUuidPedido(), | ||
pedidoModel.getClienteUuid(), | ||
pedidoModel.getStatusPedido(), | ||
pedidoModel.getStatusPagamento(), | ||
pedidoModel.getTempoDePreparo(), | ||
pedidoModel.getTotal() | ||
); | ||
}else{ | ||
throw new PedidoNaoEncontradoException("Pedido não encontrado"); | ||
} | ||
} | ||
|
||
@Override | ||
public com.fiap.mspedidoapi.domain.entity.pedido.PedidoEntity movePedidoParaEmPreparacao(UUID pedidoUuid, Integer tempoDePreparoEmMinutos) throws PedidoNaoEncontradoException { | ||
Optional<Pedido> pedido = pedidosMongoRepository.findByUuidPedido(pedidoUuid); | ||
if(pedido.isPresent()){ | ||
Pedido pedidoModel = pedido.get(); | ||
pedidoModel.setTempoDePreparo(tempoDePreparoEmMinutos); | ||
pedidoModel.setStatusPedido(StatusPedido.EM_PREPARACAO); | ||
pedidosMongoRepository.save(pedidoModel); | ||
|
||
return new com.fiap.mspedidoapi.domain.entity.pedido.PedidoEntity( | ||
pedidoModel.getUuidPedido(), | ||
pedidoModel.getClienteUuid(), | ||
pedidoModel.getStatusPedido(), | ||
pedidoModel.getStatusPagamento(), | ||
pedidoModel.getTempoDePreparo(), | ||
pedidoModel.getTotal() | ||
); | ||
}else{ | ||
throw new PedidoNaoEncontradoException("Pedido não encontrado"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/main/java/com/fiap/mspedidoapi/infra/kafka/producers/PreparaPedidoProducer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.fiap.mspedidoapi.infra.kafka.producers; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import com.fiap.mspedidoapi.domain.gateway.producers.PreparaPedidoProducerInterface; | ||
import com.fiap.mspedidoapi.domain.output.pedido.PedidoEmPreparacaoOutput; | ||
import com.fiap.mspedidoapi.infra.dependecy.kafka.resolvers.producers.KafkaProducerResolver; | ||
import com.fiap.mspedidoapi.infra.dependecy.kafka.resolvers.producers.KafkaSenderConfig; | ||
|
||
import java.util.UUID; | ||
|
||
public class PreparaPedidoProducer extends KafkaSenderConfig implements PreparaPedidoProducerInterface { | ||
private final ObjectMapper objectMapper = new ObjectMapper(); | ||
|
||
public PreparaPedidoProducer(String servers) { | ||
super(servers, new KafkaProducerResolver().getPedidoProducer()); | ||
} | ||
|
||
@Override | ||
public void send(PedidoEmPreparacaoOutput pedidoEmPreparacaoOutput) { | ||
try { | ||
ObjectNode jsonNode = objectMapper.createObjectNode(); | ||
jsonNode.put("uuid_pedido", pedidoEmPreparacaoOutput.getPedido().getPedidoId().toString()); | ||
jsonNode.put("status_pedido", pedidoEmPreparacaoOutput.getPedido().getStatusPedido().toString()); | ||
jsonNode.put("numero_pedido", pedidoEmPreparacaoOutput.getPedido().getNumeroPedido()); | ||
jsonNode.put("tempo_preparo", pedidoEmPreparacaoOutput.getPedido().getTempoDePreparoEmMinutos()); | ||
String json = jsonNode.toString(); | ||
send(UUID.randomUUID().toString(), json); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.