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

Feature/microservice p1 exercice3 #3

Open
wants to merge 3 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
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
@@ -1,5 +1,6 @@
package com.ecommerce.microcommerce.model;

import com.ecommerce.microcommerce.web.exceptions.ProduitGratuitException;
import com.fasterxml.jackson.annotation.JsonFilter;
import org.hibernate.validator.constraints.Length;

Expand All @@ -19,7 +20,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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.ecommerce.microcommerce.dao.ProductDao;
import com.ecommerce.microcommerce.model.Product;
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 @@ -11,12 +12,22 @@
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.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 javax.validation.Valid;
import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Api( description="API pour es opérations CRUD sur les produits.")
Expand All @@ -35,7 +46,11 @@ public class ProductController {
public MappingJacksonValue listeProduits() {

Iterable<Product> produits = productDao.findAll();
productPriceValidation(produits);
return filterMappingJacksonValue(produits);
}

private MappingJacksonValue filterMappingJacksonValue(Iterable<Product> produits) {
SimpleBeanPropertyFilter monFiltre = SimpleBeanPropertyFilter.serializeAllExcept("prixAchat");

FilterProvider listDeNosFiltres = new SimpleFilterProvider().addFilter("monFiltreDynamique", monFiltre);
Expand All @@ -53,9 +68,8 @@ public MappingJacksonValue listeProduits() {
@GetMapping(value = "/Produits/{id}")

public Product afficherUnProduit(@PathVariable int id) {

Product produit = productDao.findById(id);

productPriceValidation(produit);
if(produit==null) throw new ProduitIntrouvableException("Le produit avec l'id " + id + " est INTROUVABLE. Écran Bleu si je pouvais.");

return produit;
Expand All @@ -68,7 +82,7 @@ public Product afficherUnProduit(@PathVariable int id) {
@PostMapping(value = "/Produits")

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

productPriceValidation(product);
Product productAdded = productDao.save(product);

if (productAdded == null)
Expand All @@ -91,7 +105,7 @@ public void supprimerProduit(@PathVariable int id) {

@PutMapping (value = "/Produits")
public void updateProduit(@RequestBody Product product) {

productPriceValidation(product);
productDao.save(product);
}

Expand All @@ -103,6 +117,35 @@ public List<Product> testeDeRequetes(@PathVariable int prix) {
return productDao.chercherUnProduitCher(400);
}

@GetMapping(value = "/AdminProduits")
public Map<Product, Integer> calculerMargeProduit() {
Map<Product, Integer> productsMargin = new HashMap<>();
List<Product> products = productDao.findAll();
productPriceValidation(products);
products.forEach(product -> productsMargin.put(product, product.getPrix() - product.getPrixAchat()));
int marge = 0;
return productsMargin;
}

@GetMapping(value = "/Produits/Trie")
public MappingJacksonValue trierProduitsParOrdreAlphabetique() {
List<Product> allByOrderByNomAsc = productDao.findAllByOrderByNomAsc();
productPriceValidation(allByOrderByNomAsc);
return filterMappingJacksonValue(allByOrderByNomAsc);
}

private void productPriceValidation(Iterable<Product> products) {
products.forEach(product -> {
productPriceValidation(product);
});

}

private void productPriceValidation(Product product) {
if (product.getPrix() == 0) {
throw new ProduitGratuitException(product.getNom());
}
}


}
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 message) {
super(String.format("le produit %s ne peux pas etre gratuit: Rien n'est gratuit dans la vie!", message));
}
}