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

Partie4 #15

Open
wants to merge 7 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
74 changes: 74 additions & 0 deletions TestsPostman/TestsExercicesFinal.postman_collection.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{
"info": {
"_postman_id": "f8b18f49-a0ae-45dd-8aad-38680b76c8e5",
"name": "TestsExercicesFinal",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "AdminProduits",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "localhost:9090/AdminProduits",
"host": [
"localhost"
],
"port": "9090",
"path": [
"AdminProduits"
]
}
},
"response": []
},
{
"name": "ProduitsTrier",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "localhost:9090/ProduitsTrier",
"host": [
"localhost"
],
"port": "9090",
"path": [
"ProduitsTrier"
]
}
},
"response": []
},
{
"name": "Produits",
"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\": \"Poney en bois\",\r\n \"prix\": 0\r\n}"
},
"url": {
"raw": "localhost:9090/Produits",
"host": [
"localhost"
],
"port": "9090",
"path": [
"Produits"
]
}
},
"response": []
}
]
}
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();
}
3 changes: 0 additions & 3 deletions src/main/java/com/ecommerce/microcommerce/model/Product.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package com.ecommerce.microcommerce.model;

import com.fasterxml.jackson.annotation.JsonFilter;
import org.hibernate.validator.constraints.Length;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.validation.constraints.Min;

@Entity
//@JsonFilter("monFiltreDynamique")
Expand All @@ -19,7 +17,6 @@ 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)
private int prix;

//information que nous ne souhaitons pas exposer
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/com/ecommerce/microcommerce/model/ProductAdmin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.ecommerce.microcommerce.model;

public class ProductAdmin {
Product produit;

int marge;

public ProductAdmin(Product produit, int marge) {
this.produit = produit;
this.marge = marge;
}

public Product getProduit() {
return produit;
}

public void setProduit(Product produit) {
this.produit = produit;
}

public int getMarge() {
return marge;
}

public void setMarge(int marge) {
this.marge = marge;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.ecommerce.microcommerce.dao.ProductDao;
import com.ecommerce.microcommerce.model.Product;
import com.ecommerce.microcommerce.model.ProductAdmin;
import com.ecommerce.microcommerce.web.exceptions.ProduitGratuitException;
import com.ecommerce.microcommerce.web.exceptions.ProduitIntrouvableException;
import com.fasterxml.jackson.databind.ser.FilterProvider;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
Expand All @@ -16,6 +18,7 @@

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


Expand Down Expand Up @@ -66,9 +69,10 @@ public Product afficherUnProduit(@PathVariable int id) {

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

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

if(product.getPrix() == 0) throw new ProduitGratuitException("Le prix de vente ne peut pas être 0.");

Product productAdded = productDao.save(product);

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

@GetMapping(value = "/AdminProduits")
public MappingJacksonValue calculerMargeProduit() {
List<Product> produits = productDao.findAll();

List<ProductAdmin> productAdmins = new ArrayList<>();
for (Product produit : produits) {
productAdmins.add(new ProductAdmin(produit, produit.getPrix() - produit.getPrixAchat()));
}

SimpleBeanPropertyFilter monFiltre = SimpleBeanPropertyFilter.serializeAllExcept("prixAchat");

FilterProvider listDeNosFiltres = new SimpleFilterProvider().addFilter("monFiltreDynamique", monFiltre);

MappingJacksonValue produitsFiltres = new MappingJacksonValue(productAdmins);

produitsFiltres.setFilters(listDeNosFiltres);

return produitsFiltres;
}

@GetMapping(value = "/ProduitsTrier")
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 ProduitGratuitException extends RuntimeException {

public ProduitGratuitException(String s) {
super(s);
}
}