initial commit

This commit is contained in:
alexandre.iwanesko@gmail.com 2021-12-12 00:25:40 +01:00
parent 05ebf8b259
commit 6d87a8fcc1
44 changed files with 1980 additions and 0 deletions

View file

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

View file

@ -0,0 +1,9 @@
package com.mcommandes.dao;
import com.mcommandes.model.Commande;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface CommandesDao extends JpaRepository<Commande, Integer> {
}

View file

@ -0,0 +1,85 @@
package com.mcommandes.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.util.Date;
@Entity
public class Commande {
@Id
@GeneratedValue
private int id;
private Integer productId;
private Date dateCommande;
private Integer quantite;
private Boolean commandePayee;
public Commande() {
}
public Commande(int id, Integer productId, Date dateCommande, Integer quantite, Boolean commandePayee) {
this.id = id;
this.productId = productId;
this.dateCommande = dateCommande;
this.quantite = quantite;
this.commandePayee = commandePayee;
}
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 "commande{" +
"id=" + id +
", productId=" + productId +
", dateCommande=" + dateCommande +
", quantite=" + quantite +
", commandePayee=" + commandePayee +
'}';
}
}

View file

@ -0,0 +1,40 @@
package com.mcommandes.web.controller;
import com.mcommandes.dao.CommandesDao;
import com.mcommandes.model.Commande;
import com.mcommandes.web.exceptions.CommandeNotFoundException;
import com.mcommandes.web.exceptions.ImpossibleAjouterCommandeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.Optional;
@RestController
public class CommandeController {
@Autowired
CommandesDao commandesDao;
@PostMapping (value = "/commandes")
public ResponseEntity<Commande> ajouterCommande(@RequestBody Commande commande){
Commande nouvelleCommande = commandesDao.save(commande);
if(nouvelleCommande == null) throw new ImpossibleAjouterCommandeException("Impossible d'ajouter cette commande");
return new ResponseEntity<Commande>(commande, HttpStatus.CREATED);
}
@GetMapping(value = "/commandes/{id}")
public Optional<Commande> recupererUneCommande(@PathVariable int id){
Optional<Commande> commande = commandesDao.findById(id);
if(!commande.isPresent()) throw new CommandeNotFoundException("Cette commande n'existe pas");
return commande;
}
}

View file

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

View file

@ -0,0 +1,17 @@
package com.mcommandes.web.exceptions;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
/*
Si on en arrive à cette exception, c'est qu'il y a eu une erreur interne
Si la requête était mal formée, elle aurait déclenché 400 Bad Request automatiquement
**/
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public class ImpossibleAjouterCommandeException extends RuntimeException {
public ImpossibleAjouterCommandeException(String message) {
super(message);
}
}

View file

@ -0,0 +1,9 @@
server.port 9002
#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