construct of Tearcher's server part finished

feature/ProfSection
Roxane ROSSETTO 1 year ago
parent 799af18ef7
commit d57ed6e020

@ -1,34 +1,5 @@
<?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"
<<<<<<< HEAD
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.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>WebService</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>tp2</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</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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>
@ -50,13 +21,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
@ -66,18 +35,6 @@
<artifactId>jakarta.persistence-api</artifactId>
</dependency>
</dependencies>
>>>>>>> origin/feature/response
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
@ -86,5 +43,4 @@
</plugin>
</plugins>
</build>
</project>

@ -1,5 +1,7 @@
package SAE.ApiREST.WebService.controller;
import SAE.ApiREST.WebService.exception.TeacherAdvice;
import SAE.ApiREST.WebService.exception.TeacherException;
import SAE.ApiREST.WebService.model.Teacher;
import SAE.ApiREST.WebService.service.ITeacherService;
import org.springframework.beans.factory.annotation.Autowired;
@ -10,8 +12,10 @@ import org.springframework.web.bind.annotation.*;
import javax.print.attribute.standard.Media;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
//Todo() Response = Type de retour pour toutes les méthodes qui ont void
@Controller
@RequestMapping("/ProfWebService")
public class TeacherController {
@ -25,16 +29,56 @@ public class TeacherController {
@GetMapping(value = "/all", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK)
public List<Teacher> getAllTeacher(){
public @ResponseBody List<Teacher> getAllTeacher(){
return iTeacherServ.getAllTeacher();
}
@PostMapping(produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
@PostMapping(value = "addTeacher",produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.CREATED)
public Teacher createTeacher( @RequestBody Teacher teach){
public @ResponseBody Teacher createTeacher( @RequestBody Teacher teach){
return teach;
}
//@GetMapping(value = "/{id}")
//public
@GetMapping(value = "/getid/{id}")
public @ResponseBody Teacher getTeachById(@PathVariable("id") Integer id){
Teacher tt = iTeacherServ.getTeacherById(id);
if( tt == null ){
throw new TeacherException("No teacher found for this id !");
}
return tt;
}
@GetMapping(value = "/getusername/{username}")
public @ResponseBody Teacher getTeachByUsername(@PathVariable("username") String username) {
Teacher tt = iTeacherServ.getTeacherByUsername(username);
if (tt == null) {
throw new TeacherException("No teacher found for this username");
}
return tt;
}
@GetMapping( value = "/getmail/{mail}" )
public @ResponseBody Teacher getTeachByMail(@PathVariable("mail") String mail) {
Teacher tt = iTeacherServ.getTeacherByMail(mail);
if( tt == null ) {
throw new TeacherException("No teacher found for this mail");
}
return tt;
}
@GetMapping( value = "/getdate/{date}" )
public @ResponseBody Teacher getTeachByDate(@PathVariable("date") String date) {
Teacher tt = iTeacherServ.getTeacherByMail(date);
if( tt == null ) {
throw new TeacherException("No teacher found for this mail");
}
return tt;
}
@PutMapping( value = "/modify/{username}" )
public @ResponseBody Teacher modifyTeachUsername(@PathVariable("username") String username, Teacher tt){
if( username == "" ){
throw new TeacherException("Username provided for modification is empty");
}
iTeacherServ.modifyUsername(tt,username);
return tt;
}
@DeleteMapping( value = "delete")
public @ResponseBody List<Teacher> deleteTeacher(Integer id){
return iTeacherServ.deleteTeacher(id);
}
}

@ -0,0 +1,17 @@
package SAE.ApiREST.WebService.exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
@ControllerAdvice
public class TeacherAdvice {
@ResponseBody
@ExceptionHandler(TeacherException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public String teacherNFHandler( TeacherException e) {
return e.getMessage();
}
}

@ -0,0 +1,7 @@
package SAE.ApiREST.WebService.exception;
public class TeacherException extends RuntimeException {
public TeacherException(String exception) {
super(exception);
}
}

@ -12,11 +12,8 @@ import jakarta.persistence.Id;
public class Article {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
<<<<<<< HEAD
=======
String id;
>>>>>>> origin/feature/response
Integer id;
String title;
String URL;
LocalDate dateAdded;
@ -28,7 +25,7 @@ public class Article {
public Article() {}
public Article(String title, String URL, LocalDate dateAdded, LocalDate datePublished, Boolean visibility, Integer type) {
this.id = "1";
this.id = 1;
this.title = title;
this.URL = URL;
this.dateAdded = dateAdded;
@ -37,11 +34,11 @@ public class Article {
this.type = type;
}
public String getId() {
public Integer getId() {
return this.id;
}
public void setId(String id) {
public void setId(Integer id) {
this.id = id;
}

@ -21,7 +21,7 @@ public class Teacher {
public Teacher(Integer id, String date, String mail, String username) {
this.id = id;
this.date = LocalDate.parse(date, DateTimeFormatter.ISO_DATE);
this.date = LocalDate.parse(date, DateTimeFormatter.ofPattern("dd-MM-yyyy"));
this.mail = mail;
this.username = username;
}
@ -38,8 +38,8 @@ public class Teacher {
return date;
}
public void setDate(LocalDate date) {
this.date = date;
public void setDate(String date) {
this.date = LocalDate.parse(date, DateTimeFormatter.ofPattern("dd-MM-yyyy"));
}
public String getMail() {

@ -1,21 +1,18 @@
package SAE.ApiREST.WebService.service;
import SAE.ApiREST.WebService.Response;
import SAE.ApiREST.WebService.model.Teacher;
import java.time.LocalDate;
import java.util.List;
public interface ITeacherService {
//Todo() by id, by mail, by username, allProf, by date (order), suppression, ajout, FAIRE DES REGIONS!
public List<Teacher> getAllTeacher();
Teacher getTeacherById(Integer id);
public Teacher getTeacherById(Integer id);
public Teacher getTeacherByUsername(String username);
public Teacher getTeacherByMail(String mail);
public Teacher getTeacherByDate(String date);
public List<Teacher> addTeacher(Teacher t);
public List<Teacher> deleteTeacher(Integer id);
public Response modifyUsername(Teacher t, String newUsername);
}

@ -1,5 +1,7 @@
package SAE.ApiREST.WebService.service;
import SAE.ApiREST.WebService.Response;
import SAE.ApiREST.WebService.exception.TeacherException;
import SAE.ApiREST.WebService.model.Teacher;
import org.springframework.stereotype.Service;
@ -31,9 +33,7 @@ public class TeacherServiceStub implements ITeacherService {
}
@Override
public Teacher getTeacherByUsername(String username) {
return new Teacher(12, "30-08-2020", "dadadou@gmail.com", username);
}
public Teacher getTeacherByUsername(String username) { return new Teacher(12, "30-08-2020", "dadadou@gmail.com", username); }
@Override
public Teacher getTeacherByMail(String mail) {
@ -59,7 +59,15 @@ public class TeacherServiceStub implements ITeacherService {
allTeacher.add(new Teacher(1,"12-01-2023", "aline.alipres@gmail.com", "MsGarconManque"));
allTeacher.add(new Teacher(2, "20-08-2023", "Viviane.Delvecchio@gmail.com", "MmeMath"));
allTeacher.remove(getTeacherById(id));
return allTeacher;
if(allTeacher.remove(getTeacherById(id))){
return allTeacher;
} else {
throw new TeacherException(String.format("Teacher {id} isn't removed", id));
}
}
public Response modifyUsername(Teacher t, String newUsername){
t.setUsername(newUsername);
return new Response(t.getId(),String.format("This user %s has changed username", t.getMail()));
}
}

Loading…
Cancel
Save