diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 diff --git a/.mvn/wrapper/maven-wrapper.jar b/.mvn/wrapper/maven-wrapper.jar old mode 100644 new mode 100755 diff --git a/.mvn/wrapper/maven-wrapper.properties b/.mvn/wrapper/maven-wrapper.properties old mode 100644 new mode 100755 diff --git a/R5.09 - TP508/GET All Student By Group.bru b/R5.09 - TP508/GET All Student By Group.bru new file mode 100644 index 0000000000000000000000000000000000000000..534b3420ab01b9b7f51a45281f52e647d636d384 --- /dev/null +++ b/R5.09 - TP508/GET All Student By Group.bru @@ -0,0 +1,15 @@ +meta { + name: GET All Student By Group + type: http + seq: 3 +} + +get { + url: localhost:8080/etudiant/select?groupe=S + body: none + auth: none +} + +query { + groupe: S +} diff --git a/R5.09 - TP508/GET All Student.bru b/R5.09 - TP508/GET All Student.bru new file mode 100755 index 0000000000000000000000000000000000000000..1bb934548e3c45338d9c7423007acde849dbd15a --- /dev/null +++ b/R5.09 - TP508/GET All Student.bru @@ -0,0 +1,11 @@ +meta { + name: GET All Student + type: http + seq: 1 +} + +get { + url: localhost:8080/etudiant + body: none + auth: none +} diff --git a/R5.09 - TP508/GET One Student.bru b/R5.09 - TP508/GET One Student.bru new file mode 100755 index 0000000000000000000000000000000000000000..b4f28a0dd126508bfde0294cfc76e3d37fa34dc3 --- /dev/null +++ b/R5.09 - TP508/GET One Student.bru @@ -0,0 +1,11 @@ +meta { + name: GET One Student + type: http + seq: 2 +} + +get { + url: localhost:8080/etudiant/2 + body: none + auth: none +} diff --git a/R5.09 - TP508/POST One Student.bru b/R5.09 - TP508/POST One Student.bru new file mode 100644 index 0000000000000000000000000000000000000000..388b82933d0bc576534ce667aa38c45a43c30f52 --- /dev/null +++ b/R5.09 - TP508/POST One Student.bru @@ -0,0 +1,21 @@ +meta { + name: POST One Student + type: http + seq: 4 +} + +post { + url: localhost:8080/etudiant + body: json + auth: none +} + +body:json { + { + "id": 10, + "prenom": "Bruce", + "nom": "Banner", + "age": 100, + "groupe": "S" + } +} diff --git a/R5.09 - TP508/bruno.json b/R5.09 - TP508/bruno.json new file mode 100755 index 0000000000000000000000000000000000000000..6c06f59171f9c556a85f9bbf3d160d1854ec3549 --- /dev/null +++ b/R5.09 - TP508/bruno.json @@ -0,0 +1,9 @@ +{ + "version": "1", + "name": "R5.09 - TP508", + "type": "collection", + "presets": { + "requestType": "http", + "requestUrl": "localhost:8080" + } +} \ No newline at end of file diff --git a/mvnw.cmd b/mvnw.cmd old mode 100644 new mode 100755 diff --git a/pom.xml b/pom.xml old mode 100644 new mode 100755 index 4d36190f1b38801913bdaa9278e84c2525ad3182..5a63e78583baf8bf578c0ff17a4696b88cf02643 --- a/pom.xml +++ b/pom.xml @@ -1,69 +1,74 @@ <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> - <modelVersion>4.0.0</modelVersion> - <parent> - <groupId>org.springframework.boot</groupId> - <artifactId>spring-boot-starter-parent</artifactId> - <version>3.2.0</version> - <relativePath/> <!-- lookup parent from repository --> - </parent> - <groupId>fr.but3</groupId> - <artifactId>tp508</artifactId> - <version>0.0.1-SNAPSHOT</version> - <name>demo</name> - <description>Demo project for Spring Boot</description> - <properties> - <java.version>17</java.version> - </properties> - <dependencies> - <dependency> - <groupId>org.springframework.boot</groupId> - <artifactId>spring-boot-starter-data-jpa</artifactId> - </dependency> - <dependency> - <groupId>org.springframework.boot</groupId> - <artifactId>spring-boot-starter-web</artifactId> - </dependency> + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> +<modelVersion>4.0.0</modelVersion> +<parent> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-parent</artifactId> + <version>3.2.0</version> + <relativePath/> <!-- lookup parent from repository --> +</parent> +<groupId>fr.but3</groupId> +<artifactId>tp508</artifactId> +<version>0.0.1-SNAPSHOT</version> +<name>demo</name> +<description>Demo project for Spring Boot</description> +<properties> + <java.version>17</java.version> +</properties> +<dependencies> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-data-jpa</artifactId> + </dependency> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-web</artifactId> + </dependency> - <dependency> - <groupId>org.springframework.boot</groupId> - <artifactId>spring-boot-devtools</artifactId> - <scope>runtime</scope> - <optional>true</optional> - </dependency> - <dependency> - <groupId>com.h2database</groupId> - <artifactId>h2</artifactId> - <scope>runtime</scope> - </dependency> - <dependency> - <groupId>org.projectlombok</groupId> - <artifactId>lombok</artifactId> - <optional>true</optional> - </dependency> - <dependency> - <groupId>org.springframework.boot</groupId> - <artifactId>spring-boot-starter-test</artifactId> - <scope>test</scope> - </dependency> - </dependencies> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-devtools</artifactId> + <scope>runtime</scope> + <optional>true</optional> + </dependency> + <dependency> + <groupId>com.h2database</groupId> + <artifactId>h2</artifactId> + <scope>runtime</scope> + </dependency> + <dependency> + <groupId>org.projectlombok</groupId> + <artifactId>lombok</artifactId> + <optional>true</optional> + </dependency> + <dependency> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-starter-test</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.springdoc</groupId> + <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId> + <version>2.3.0</version> + </dependency> +</dependencies> - <build> - <plugins> - <plugin> - <groupId>org.springframework.boot</groupId> - <artifactId>spring-boot-maven-plugin</artifactId> - <configuration> - <excludes> - <exclude> - <groupId>org.projectlombok</groupId> - <artifactId>lombok</artifactId> - </exclude> - </excludes> - </configuration> - </plugin> - </plugins> - </build> +<build> + <plugins> + <plugin> + <groupId>org.springframework.boot</groupId> + <artifactId>spring-boot-maven-plugin</artifactId> + <configuration> + <excludes> + <exclude> + <groupId>org.projectlombok</groupId> + <artifactId>lombok</artifactId> + </exclude> + </excludes> + </configuration> + </plugin> + </plugins> +</build> </project> diff --git a/src/main/java/fr/but3/tp508/ControleurEtudiantRest.java b/src/main/java/fr/but3/tp508/ControleurEtudiantRest.java new file mode 100755 index 0000000000000000000000000000000000000000..8aee8f029204dbde3db287cdd3f4f103b3cd982b --- /dev/null +++ b/src/main/java/fr/but3/tp508/ControleurEtudiantRest.java @@ -0,0 +1,58 @@ +package fr.but3.tp508; + +import java.util.List; +import java.net.http.HttpResponse; +import java.util.ArrayList; +import java.util.Optional; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +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.RequestBody; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; + +@RestController +@RequestMapping(value = "etudiant", produces={MediaType.APPLICATION_JSON_VALUE}) +public class ControleurEtudiantRest { + + @Autowired + private EtudiantRepository etudiantRepository; + + @GetMapping + public List<Etudiant> getAllStudent() { + return (List<Etudiant>) this.etudiantRepository.findAll(); + } + + @PostMapping + public Etudiant saveStudent(@RequestBody Etudiant student) { + Etudiant query_result = this.etudiantRepository.save(student); + + return query_result; + } + + @GetMapping(value = "/{id}") + ResponseEntity<Etudiant> getOneStudent(@PathVariable int id) { + Optional<Etudiant> query_result = this.etudiantRepository.findById(id); + + if(query_result.isPresent()) return ResponseEntity.ok(query_result.get()); + + return ResponseEntity.notFound().build(); + } + + @GetMapping(value = "/select") + List<Etudiant> select(@RequestParam String groupe) { + List<Etudiant> students = new ArrayList<>(); + + this.etudiantRepository.findAllByGroupe(groupe).forEach(students::add); + + return students; + } + +} diff --git a/src/main/java/fr/but3/tp508/DemoApplication.java b/src/main/java/fr/but3/tp508/DemoApplication.java old mode 100644 new mode 100755 diff --git a/src/main/java/fr/but3/tp508/Etudiant.java b/src/main/java/fr/but3/tp508/Etudiant.java new file mode 100755 index 0000000000000000000000000000000000000000..899a7435b397b919ba223d30dd627f78ea22e295 --- /dev/null +++ b/src/main/java/fr/but3/tp508/Etudiant.java @@ -0,0 +1,22 @@ +package fr.but3.tp508; + +import lombok.Data; +import lombok.ToString; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.GeneratedValue; + +@Entity +@ToString +@Data +public class Etudiant { + @GeneratedValue + @Id + int id; + String prenom; + String nom; + int age; + String groupe; + +} + diff --git a/src/main/java/fr/but3/tp508/EtudiantRepository.java b/src/main/java/fr/but3/tp508/EtudiantRepository.java new file mode 100755 index 0000000000000000000000000000000000000000..d0bdf3da94714acb76a4e947f7174fcd5eee7e8d --- /dev/null +++ b/src/main/java/fr/but3/tp508/EtudiantRepository.java @@ -0,0 +1,15 @@ +package fr.but3.tp508; + +import java.util.ArrayList; +import java.util.List; + +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface EtudiantRepository extends CrudRepository<Etudiant, Integer> { + + public List<Etudiant> findAllByGroupe(String groupe); + +} + diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties old mode 100644 new mode 100755 index 8b137891791fe96927ad78e64b0aad7bded08bdc..622b67489df3e9102b2bfb4ae94f0b0373c12771 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1,13 @@ +server.servlet.contextPath=/ +spring.datasource.url=jdbc:h2:mem:tp508 +spring.datasource.driverClassName=org.h2.Driver +spring.datasource.username=spring +spring.datasource.password=password +spring.jpa.database-platform=org.hibernate.dialect.H2Dialect +spring.h2.console.enabled=true + +spring.jpa.defer-datasource-initialization=true + +logging.level.org.hibernate.SQL=DEBUG +server.error.include-stacktrace=never diff --git a/src/main/resources/data.sql b/src/main/resources/data.sql new file mode 100755 index 0000000000000000000000000000000000000000..58cd5a73e9c055c91fb15021d294bacc762533ef --- /dev/null +++ b/src/main/resources/data.sql @@ -0,0 +1,3 @@ +insert into etudiant(id, prenom, nom, age, groupe) values (1, 'Albert','Einstein', 52, 'S'); +insert into etudiant(id, prenom, nom, age, groupe) values (2, 'John','Wick', 24, 'J'); + diff --git a/src/main/resources/schema.sql b/src/main/resources/schema.sql new file mode 100755 index 0000000000000000000000000000000000000000..624bb2425fe090ad7a4e7a139d4f6020257b0f8e --- /dev/null +++ b/src/main/resources/schema.sql @@ -0,0 +1,9 @@ +drop table if exists etudiant; + +create table etudiant ( + id int not null, + prenom varchar(255) not null, + nom varchar(255) not null, + age int not null, + groupe varchar(1) not null +); diff --git a/src/main/resources/static/index.html b/src/main/resources/static/index.html new file mode 100755 index 0000000000000000000000000000000000000000..e3ba4d0fbe2234dd983e307d2975480a58766232 --- /dev/null +++ b/src/main/resources/static/index.html @@ -0,0 +1,7 @@ +<html> + <head> + </head> + <body> + <h1>Hello World !</h1> + </body> +</html> diff --git a/src/test/java/fr/but3/tp508/DemoApplicationTests.java b/src/test/java/fr/but3/tp508/DemoApplicationTests.java old mode 100644 new mode 100755