Client fonctionnel communiquant avec tous les MS
This commit is contained in:
parent
717edf9812
commit
f272e9baa8
23 changed files with 683 additions and 7 deletions
|
|
@ -2,8 +2,10 @@ package com.mpaiement;
|
|||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableFeignClients("com.mpaiement")
|
||||
public class MpaiementApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,70 @@
|
|||
package com.mpaiement.beans;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
public class CommandeBean {
|
||||
|
||||
private int id;
|
||||
|
||||
private Integer productId;
|
||||
|
||||
private Date dateCommande;
|
||||
|
||||
private Integer quantite;
|
||||
|
||||
private Boolean commandePayee;
|
||||
|
||||
public CommandeBean() {
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Integer getProductId() {
|
||||
return productId;
|
||||
}
|
||||
|
||||
public void setProductId(Integer productId) {
|
||||
this.productId = productId;
|
||||
}
|
||||
|
||||
public Date getDateCommande() {
|
||||
return dateCommande;
|
||||
}
|
||||
|
||||
public void setDateCommande(Date dateCommande) {
|
||||
this.dateCommande = dateCommande;
|
||||
}
|
||||
|
||||
public Integer getQuantite() {
|
||||
return quantite;
|
||||
}
|
||||
|
||||
public void setQuantite(Integer quantite) {
|
||||
this.quantite = quantite;
|
||||
}
|
||||
|
||||
public Boolean getCommandePayee() {
|
||||
return commandePayee;
|
||||
}
|
||||
|
||||
public void setCommandePayee(Boolean commandePayee) {
|
||||
this.commandePayee = commandePayee;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "CommandeBean{" +
|
||||
"id=" + id +
|
||||
", productId=" + productId +
|
||||
", dateCommande=" + dateCommande +
|
||||
", quantite=" + quantite +
|
||||
", commandePayee=" + commandePayee +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
|
@ -15,14 +15,14 @@ public class Paiement {
|
|||
@Column(unique = true)
|
||||
private Integer idCommande;
|
||||
|
||||
private Integer montant;
|
||||
private Double montant;
|
||||
|
||||
private Long numeroCarte;
|
||||
|
||||
public Paiement() {
|
||||
}
|
||||
|
||||
public Paiement(int id, Integer idCommande, Integer montant, Long numeroCarte) {
|
||||
public Paiement(int id, Integer idCommande, Double montant, Long numeroCarte) {
|
||||
this.id = id;
|
||||
this.idCommande = idCommande;
|
||||
this.montant = montant;
|
||||
|
|
@ -45,11 +45,11 @@ public class Paiement {
|
|||
this.idCommande = idCommande;
|
||||
}
|
||||
|
||||
public Integer getMontant() {
|
||||
public Double getMontant() {
|
||||
return montant;
|
||||
}
|
||||
|
||||
public void setMontant(Integer montant) {
|
||||
public void setMontant(Double montant) {
|
||||
this.montant = montant;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,20 @@
|
|||
package com.mpaiement.proxies;
|
||||
|
||||
import com.mpaiement.beans.CommandeBean;
|
||||
import org.springframework.cloud.openfeign.FeignClient;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@FeignClient(name = "microservice-commandes", url = "localhost:9002")
|
||||
public interface MicroserviceCommandeProxy {
|
||||
|
||||
@GetMapping(value = "/commandes/{id}")
|
||||
Optional<CommandeBean> recupererUneCommande(@PathVariable("id") int id);
|
||||
|
||||
@PutMapping(value = "/commandes")
|
||||
void updateCommande(@RequestBody CommandeBean commande);
|
||||
}
|
||||
|
|
@ -1,7 +1,9 @@
|
|||
package com.mpaiement.web.controller;
|
||||
|
||||
import com.mpaiement.beans.CommandeBean;
|
||||
import com.mpaiement.dao.PaiementDao;
|
||||
import com.mpaiement.model.Paiement;
|
||||
import com.mpaiement.proxies.MicroserviceCommandeProxy;
|
||||
import com.mpaiement.web.exceptions.PaiementExistantException;
|
||||
import com.mpaiement.web.exceptions.PaiementImpossibleException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
|
@ -9,12 +11,20 @@ import org.springframework.http.HttpStatus;
|
|||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@RestController
|
||||
public class PaiementController {
|
||||
|
||||
@Autowired
|
||||
PaiementDao paiementDao;
|
||||
|
||||
@Autowired
|
||||
MicroserviceCommandeProxy microserviceCommandeProxy;
|
||||
|
||||
/*
|
||||
* Opération pour enregistrer un paiement et notifier le microservice commandes pour mettre à jour le statut de la commande en question
|
||||
**/
|
||||
@PostMapping(value = "/paiement")
|
||||
public ResponseEntity<Paiement> payerUneCommande(@RequestBody Paiement paiement){
|
||||
|
||||
|
|
@ -26,13 +36,22 @@ public class PaiementController {
|
|||
//Enregistrer le paiement
|
||||
Paiement nouveauPaiement = paiementDao.save(paiement);
|
||||
|
||||
|
||||
// si le DAO nous retourne null c'est que il ya eu un problème lors de l'enregistrement
|
||||
if(nouveauPaiement == null) throw new PaiementImpossibleException("Erreur, impossible d'établir le paiement, réessayez plus tard");
|
||||
|
||||
//On récupère la commande correspondant à ce paiement en faisant appel au Microservice commandes
|
||||
Optional<CommandeBean> commandeReq = microserviceCommandeProxy.recupererUneCommande(paiement.getIdCommande());
|
||||
|
||||
//commandeReq.get() permet d'extraire l'objet de type CommandeBean de Optional
|
||||
CommandeBean commande = commandeReq.get();
|
||||
|
||||
//TODO Nous allons appeler le Microservice Commandes ici pour lui signifier que le paiement est accepté
|
||||
//on met à jour l'objet pour marquer la commande comme étant payée
|
||||
commande.setCommandePayee(true);
|
||||
|
||||
//on envoi l'objet commande mis à jour au microservice commande afin de mettre à jour le status de la commande.
|
||||
microserviceCommandeProxy.updateCommande(commande);
|
||||
|
||||
//on renvoi 201 CREATED pour notifier le client au le paiement à été enregistré
|
||||
return new ResponseEntity<Paiement>(nouveauPaiement, HttpStatus.CREATED);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
spring.application.name=microservice-paiement
|
||||
|
||||
server.port 9003
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue