Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tuto Construisez des Microservices #23

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://github.com/ordre66/Mcommerce-partie1/
11 changes: 11 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
</dependencies>

<build>
Expand All @@ -67,6 +73,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.ecommerce.microcommerce.web"))
.paths(PathSelectors.regex("/Produits.*"))
.paths(PathSelectors.regex("/(Admin|Tri)?Produits.*"))
.build();
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/ecommerce/microcommerce/dao/ProductDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ public interface ProductDao extends JpaRepository<Product, Integer> {

@Query("SELECT id, nom, prix FROM Product p WHERE p.prix > :prixLimit")
List<Product> chercherUnProduitCher(@Param("prixLimit") int prix);

List<Product> findAllByOrderByNomAsc();
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class Product {
@Length(min=3, max=20, message = "Nom trop long ou trop court. Et oui messages sont plus stylés que ceux de Spring")
private String nom;

@Min(value = 1)
@Min(value = 0)
private int prix;

//information que nous ne souhaitons pas exposer
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
package com.ecommerce.microcommerce.web.controller;

import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import com.ecommerce.microcommerce.dao.ProductDao;
import com.ecommerce.microcommerce.model.Product;
import com.ecommerce.microcommerce.web.exceptions.ProduitIntrouvableException;
import com.ecommerce.microcommerce.web.exceptions.ProduitPrixVenteZeroException;
import com.fasterxml.jackson.databind.ser.FilterProvider;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;

import javax.validation.Valid;
import java.net.URI;
import java.util.List;


@Api( description="API pour es opérations CRUD sur les produits.")
Expand Down Expand Up @@ -60,15 +74,19 @@ public Product afficherUnProduit(@PathVariable int id) {

return produit;
}





//ajouter un produit
@PostMapping(value = "/Produits")

public ResponseEntity<Void> ajouterProduit(@Valid @RequestBody Product product) {

//J'ai dû mettre à 0 min value du prix dans Product sinon cela ne passait pas la validation @Valid
//que je souhaitais conserver
if(product.getPrix() == 0) {
throw new ProduitPrixVenteZeroException("Le produit que vous souhaitez ajouter ne peut pas avoir un prix de vente à 0.");
}

Product productAdded = productDao.save(product);

if (productAdded == null)
Expand Down Expand Up @@ -103,6 +121,25 @@ public List<Product> testeDeRequetes(@PathVariable int prix) {
return productDao.chercherUnProduitCher(400);
}

@ApiOperation(value = "Calcule la marge de chaque produit (différence entre prix d‘achat et prix de vente!")
@GetMapping(value = "/AdminProduits")
public Map<Product, Integer> calculerMargeProduit() {

Iterable<Product> produits = productDao.findAll();
HashMap<Product, Integer> result = new HashMap<Product, Integer>();
for (Product product : produits) {

result.put(product, (product.getPrix()-product.getPrixAchat()));
}

return result;
}

@ApiOperation(value = "Retourne la liste de tous les produits triés par nom croissant!")
@GetMapping(value = "/TriProduits")
public List<Product> trierProduitsParOrdreAlphabetique() {

return productDao.findAllByOrderByNomAsc();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.ecommerce.microcommerce.web.exceptions;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(HttpStatus.BAD_REQUEST)
public class ProduitPrixVenteZeroException extends RuntimeException {

public ProduitPrixVenteZeroException(String s) {
super(s);
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
{
"info": {
"_postman_id": "dd084666-dd50-4ae0-b60f-4c6d13252aea",
"name": "TutoMicroServices",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "Partie 1 - Affichage de la marge",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "http://localhost:9090/AdminProduits",
"protocol": "http",
"host": [
"localhost"
],
"port": "9090",
"path": [
"AdminProduits"
]
},
"description": "Partie 1 - Affichage de la marge"
},
"response": []
},
{
"name": "Partie 2 - Tri par ordre alphabétique",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "http://localhost:9090/AdminProduits/orderbyNomAsc",
"protocol": "http",
"host": [
"localhost"
],
"port": "9090",
"path": [
"AdminProduits",
"orderbyNomAsc"
]
},
"description": "Partie 2 - Tri par ordre alphabétique"
},
"response": []
},
{
"name": "Partie 3 - Validation du prix de vente",
"request": {
"method": "POST",
"header": [
{
"key": "Content-Type",
"name": "Content-Type",
"value": "application/json",
"type": "text"
}
],
"body": {
"mode": "raw",
"raw": "{\r\n \"id\": 4,\r\n \"nom\": \"Ordinateur portable\",\r\n \"prix\": 0,\r\n \"prixAchat\": 120\r\n }",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "http://localhost:9090/Produits",
"protocol": "http",
"host": [
"localhost"
],
"port": "9090",
"path": [
"Produits"
]
},
"description": "Partie 3 - Validation du prix de vente"
},
"response": []
}
],
"protocolProfileBehavior": {}
}