Premier commit

This commit is contained in:
TestAccountsUser 2018-03-11 20:48:28 +01:00
commit 30dc8cbce7
43 changed files with 1979 additions and 0 deletions

View file

@ -0,0 +1,12 @@
package com.mproduits;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MproduitsApplication {
public static void main(String[] args) {
SpringApplication.run(MproduitsApplication.class, args);
}
}

View file

@ -0,0 +1,10 @@
package com.mproduits.dao;
import com.mproduits.model.Product;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface ProductDao extends JpaRepository<Product, Integer>{
}

View file

@ -0,0 +1,87 @@
package com.mproduits.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Product {
@Id
@GeneratedValue
private int id;
private String titre;
private String description;
private String image;
private Double prix;
public Product() {
}
public Product(int id, String titre, String description, String image, Double prix) {
this.id = id;
this.titre = titre;
this.description = description;
this.image = image;
this.prix = prix;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitre() {
return titre;
}
public void setTitre(String titre) {
this.titre = titre;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public Double getPrix() {
return prix;
}
public void setPrix(Double prix) {
this.prix = prix;
}
@Override
public String toString() {
return "Product{" +
"id=" + id +
", titre='" + titre + '\'' +
", description='" + description + '\'' +
", image='" + image + '\'' +
", prix=" + prix +
'}';
}
}

View file

@ -0,0 +1,43 @@
package com.mproduits.web.controller;
import com.mproduits.dao.ProductDao;
import com.mproduits.model.Product;
import com.mproduits.web.exceptions.ProductNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Optional;
@RestController
public class ProductController {
@Autowired
ProductDao productDao;
// Affiche la liste de tous les produits disponibles
@GetMapping(value = "/Produits")
public List<Product> listeDesProduits(){
List<Product> products = productDao.findAll();
if(products.isEmpty()) throw new ProductNotFoundException("Aucun produit n'est disponible à la vente");
return products;
}
//Récuperer un produit par son id
@GetMapping( value = "/Produits/{id}")
public Optional<Product> recupererUnProduit(@PathVariable int id) {
Optional<Product> product = productDao.findById(id);
if(!product.isPresent()) throw new ProductNotFoundException("Le produit correspondant à l'id " + id + " n'existe pas");
return product;
}
}

View file

@ -0,0 +1,13 @@
package com.mproduits.web.exceptions;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(HttpStatus.NOT_FOUND)
public class ProductNotFoundException extends RuntimeException {
public ProductNotFoundException(String message) {
super(message);
}
}

View file

@ -0,0 +1,9 @@
server.port 9001
#Configurations H2
spring.jpa.show-sql=true
spring.h2.console.enabled=true
#défini l'encodage pour data.sql
spring.datasource.sql-script-encoding=UTF-8

View file

@ -0,0 +1,15 @@
INSERT INTO PRODUCT (ID , TITRE ,DESCRIPTION ,IMAGE ,PRIX ) VALUES (0, 'Bougie fonctionnant au feu', 'bougie qui fonctionne comme une ampoule mais sans électricité !', 'https://s3.eu-west-3.amazonaws.com/oc-images-cours/bougies.jpg', 22.0);
INSERT INTO PRODUCT (ID , TITRE ,DESCRIPTION ,IMAGE ,PRIX ) VALUES (1, 'Chaise pour s''assoire', 'Chaise rare avec non pas 1 ni 2 mais 3 pieds', 'https://s3.eu-west-3.amazonaws.com/oc-images-cours/chaise.jpg', 95.0);
INSERT INTO PRODUCT (ID , TITRE ,DESCRIPTION ,IMAGE ,PRIX ) VALUES (2, 'Cheval pour nains', 'Ce cheval ne portera certainement pas blanche neige, mais sans problème les nains', 'https://s3.eu-west-3.amazonaws.com/oc-images-cours/cheval.jpg', 360.0);
INSERT INTO PRODUCT (ID , TITRE ,DESCRIPTION ,IMAGE ,PRIX ) VALUES (3, 'Coq of steel, le superman des volailles', 'Ne passe pas au four', 'https://s3.eu-west-3.amazonaws.com/oc-images-cours/coq.jpg', 620.0);
INSERT INTO PRODUCT (ID , TITRE ,DESCRIPTION ,IMAGE ,PRIX ) VALUES (4, 'Flacon à frotter avec un génie dedans', 'Vous donne droit à l''équivalent de 3/0 voeux', 'https://s3.eu-west-3.amazonaws.com/oc-images-cours/flacon.jpg', 1200.0);
INSERT INTO PRODUCT (ID , TITRE ,DESCRIPTION ,IMAGE ,PRIX ) VALUES (5, 'Horloge quantique', 'Donne l''heure, les minutes et même les secondes. Ne fait pas de café', 'https://s3.eu-west-3.amazonaws.com/oc-images-cours/horloge.jpg', 180.0);
INSERT INTO PRODUCT (ID , TITRE ,DESCRIPTION ,IMAGE ,PRIX ) VALUES (7, 'Table d''opération pour Hamsters', 'Pour réaliser vos opérations chirugicales sur votre Hamster!', 'https://s3.eu-west-3.amazonaws.com/oc-images-cours/table.jpg', 210.0);
INSERT INTO PRODUCT (ID , TITRE ,DESCRIPTION ,IMAGE ,PRIX ) VALUES (8, 'Vase ayant appartenu a Zeus', 'Risque de choc électrique', 'https://s3.eu-west-3.amazonaws.com/oc-images-cours/vase.jpg', 730.0);

View file

@ -0,0 +1,16 @@
package com.mproduits;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class MproduitsApplicationTests {
@Test
public void contextLoads() {
}
}