Skip to content

Commit e890317

Browse files
committed
Add web services part
1 parent 1786b40 commit e890317

File tree

234 files changed

+4386
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

234 files changed

+4386
-0
lines changed
680 KB
Loading

Back/pom.xml

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>PronoMaster</groupId>
8+
<artifactId>PronoMaster</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
<packaging>war</packaging>
11+
12+
<properties>
13+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
14+
<maven.compiler.source>1.8</maven.compiler.source>
15+
<maven.compiler.target>1.8</maven.compiler.target>
16+
</properties>
17+
18+
<dependencies>
19+
20+
<dependency>
21+
<groupId>org.mariadb.jdbc</groupId>
22+
<artifactId>mariadb-java-client</artifactId>
23+
<version>2.3.0</version>
24+
</dependency>
25+
26+
<dependency>
27+
<groupId>de.mkammerer</groupId>
28+
<artifactId>argon2-jvm</artifactId>
29+
<version>2.2</version>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>com.fasterxml.jackson.core</groupId>
34+
<artifactId>jackson-databind</artifactId>
35+
<version>2.9.7</version>
36+
</dependency>
37+
38+
<dependency>
39+
<groupId>org.glassfish.jersey.containers</groupId>
40+
<artifactId>jersey-container-servlet</artifactId>
41+
<version>2.27</version>
42+
</dependency>
43+
44+
<dependency>
45+
<groupId>org.glassfish.jersey.inject</groupId>
46+
<artifactId>jersey-hk2</artifactId>
47+
<version>2.27</version>
48+
</dependency>
49+
50+
<dependency>
51+
<groupId>org.glassfish.jersey.media</groupId>
52+
<artifactId>jersey-media-json-jackson</artifactId>
53+
<version>2.27</version>
54+
</dependency>
55+
56+
</dependencies>
57+
58+
<build>
59+
<finalName>webservices</finalName>
60+
</build>
61+
62+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package controllers;
2+
3+
import entities.Class;
4+
import entities.Teacher;
5+
import managers.ClassLibrary;
6+
import managers.TeacherLibrary;
7+
8+
import javax.ws.rs.*;
9+
import javax.ws.rs.core.MediaType;
10+
import java.io.IOException;
11+
import java.util.List;
12+
13+
@Path("/class")
14+
public class ClassController {
15+
16+
@GET
17+
@Path("/{teacher}")
18+
@Produces(MediaType.APPLICATION_JSON)
19+
public List<Class> getClassFromTeacher(@PathParam("teacher") String teacherLogin) throws IOException {
20+
21+
Teacher teacher = TeacherLibrary.getInstance().getTeacherFromLogin(teacherLogin);
22+
System.out.println("#########"+teacher.getIdTeacher());
23+
24+
return ClassLibrary.getInstance().getClassFromTeacher(teacher);
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package controllers;
2+
3+
import entities.Student;
4+
import managers.StudentLibrary;
5+
6+
import javax.ws.rs.*;
7+
import javax.ws.rs.core.MediaType;
8+
import java.io.IOException;
9+
import java.util.List;
10+
11+
@Path("/students")
12+
public class StudentController {
13+
14+
@GET
15+
@Path("/{idClass}")
16+
@Produces(MediaType.APPLICATION_JSON)
17+
public List<Student> listStudents(@PathParam("idClass") String idClassAsString) throws IOException {
18+
int idClass = Integer.parseInt(idClassAsString);
19+
return StudentLibrary.getInstance().listStudentsFromClass(idClass);
20+
}
21+
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package controllers;
2+
3+
import entities.Teacher;
4+
import managers.TeacherLibrary;
5+
6+
import javax.ws.rs.*;
7+
import javax.ws.rs.core.MediaType;
8+
import java.io.IOException;
9+
import java.util.List;
10+
11+
@Path("teacher")
12+
public class TeacherController {
13+
14+
@GET
15+
@Produces(MediaType.APPLICATION_JSON)
16+
public List<Teacher> listTeachers() throws IOException {
17+
18+
return TeacherLibrary.getInstance().listTeachers();
19+
}
20+
21+
@GET
22+
@Path("/{teacher}")
23+
@Produces(MediaType.APPLICATION_JSON)
24+
public Teacher getTeacher(@PathParam("teacher") String teacherLogin) throws IOException {
25+
26+
return TeacherLibrary.getInstance().getTeacherFromLogin(teacherLogin);
27+
}
28+
29+
@POST
30+
@Produces(MediaType.APPLICATION_JSON)
31+
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
32+
public Teacher validTeacher(@FormParam("login") String login, @FormParam("password") String password){
33+
Teacher teacher = TeacherLibrary.getInstance().getTeacherFromLogin(login);
34+
System.out.println("Login reçut : "+login);
35+
if (teacher!=null && teacher.getPassword().equals(password)) {
36+
return teacher;
37+
} else {
38+
return null;
39+
}
40+
}
41+
42+
43+
@POST
44+
@Path("/register")
45+
@Produces(MediaType.APPLICATION_JSON)
46+
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
47+
public Teacher registerTeacher(@FormParam("email") String email, @FormParam("login") String login,
48+
@FormParam("password") String password){
49+
Teacher teacher = new Teacher( email, login, password);
50+
System.out.println("Login à indcrire : "+login);
51+
if (teacher!=null) {
52+
TeacherLibrary.getInstance().registerTeacher(teacher);
53+
return teacher;
54+
} else {
55+
return null;
56+
}
57+
}
58+
59+
60+
61+
}

Back/src/main/java/dao/ClassDao.java

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package dao;
2+
3+
import entities.Class;
4+
import entities.Teacher;
5+
6+
import java.io.IOException;
7+
import java.util.List;
8+
9+
public interface ClassDao {
10+
11+
List<Class> getClassFromTeacher(Teacher teacher) throws IOException;
12+
}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package dao;
2+
3+
import entities.Student;
4+
5+
import java.io.IOException;
6+
import java.sql.SQLException;
7+
import java.util.List;
8+
9+
public interface StudentDao {
10+
11+
List<Student> listStudentsFromClass(int idClass) throws IOException;
12+
}
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package dao;
2+
3+
import entities.Teacher;
4+
import exceptions.MatchAlreadyExistsException;
5+
6+
import java.io.IOException;
7+
8+
import java.util.List;
9+
10+
public interface TeacherDao {
11+
12+
Teacher getTeacherFromLogin(String login);
13+
14+
List<Teacher> listTeachers() throws IOException;
15+
16+
Teacher registerTeacher(Teacher teacher);
17+
18+
19+
20+
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package dao.impl;
2+
3+
4+
import dao.ClassDao;
5+
import entities.Class;
6+
import entities.Teacher;
7+
8+
import java.io.IOException;
9+
import java.sql.*;
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
13+
import static dao.impl.DataSourceProvider.getDataSource;
14+
15+
public class ClassDaoImpl implements ClassDao {
16+
17+
@Override
18+
public List<Class> getClassFromTeacher(Teacher teacher) throws IOException {
19+
20+
List<Class> listClass = new ArrayList<>();
21+
22+
try (Connection connection = getDataSource().getConnection()) {
23+
String sqlQuery = "SELECT e.id_prof, e.id_classe, c.domaine, c.url_photo FROM enseigne e INNER JOIN classe c ON c.id_classe = e.id_classe WHERE e.id_prof=?;";
24+
try (PreparedStatement statement = connection.prepareStatement(sqlQuery)) {
25+
statement.setInt(1, teacher.getIdTeacher() );
26+
try (ResultSet results = statement.executeQuery()) {
27+
while (results.next()) {
28+
Class tmpClass = new Class(results.getInt("id_classe"),results.getString("domaine"),results.getString("url_photo"));
29+
listClass.add(tmpClass);
30+
}
31+
}
32+
}
33+
} catch (SQLException e) {
34+
System.err.println("Vous n\'êtes pas connecté à la base de données, vérifiez qu\'elle est bien allumée et rééssayez");
35+
}
36+
return listClass;
37+
}
38+
}
39+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package dao.impl;
2+
3+
import org.mariadb.jdbc.MariaDbDataSource;
4+
5+
import javax.sql.DataSource;
6+
import java.io.IOException;
7+
import java.sql.SQLException;
8+
import java.util.Properties;
9+
10+
public class DataSourceProvider {
11+
12+
private static MariaDbDataSource dataSource;
13+
private static Properties properties = new Properties();
14+
15+
public static DataSource getDataSource() throws SQLException{
16+
try {
17+
properties.load(DataSourceProvider.class.getResourceAsStream("/application.properties"));
18+
19+
if (dataSource == null) {
20+
dataSource = new MariaDbDataSource();
21+
dataSource.setServerName(properties.getProperty("servername"));
22+
dataSource.setPort(Integer.parseInt(properties.getProperty("port")));
23+
dataSource.setDatabaseName(properties.getProperty("databasename"));
24+
dataSource.setUser(properties.getProperty("user"));
25+
if ( properties.getProperty("password").isEmpty()) {
26+
dataSource.setPassword("");
27+
} else {
28+
dataSource.setPassword(properties.getProperty("password"));}
29+
}
30+
}catch (IOException e) {
31+
System.err.println("Vous n\'êtes pas connecté à la base de données, vérifiez qu\'elle est bien allumée et rééssayez");
32+
}
33+
return dataSource;
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package dao.impl;
2+
3+
import java.io.IOException;
4+
import java.sql.*;
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
import dao.StudentDao;
9+
10+
import entities.Student;
11+
12+
import static dao.impl.DataSourceProvider.getDataSource;
13+
14+
public class StudentDaoImpl implements StudentDao {
15+
16+
//récupérer la liste des utilisateurs avec en premier les admins
17+
public List<Student> listStudentsFromClass(int idClass) throws IOException {
18+
List<Student> listStudents = new ArrayList<>();
19+
try (Connection connection = getDataSource().getConnection()) {
20+
String sqlQuery = "SELECT * FROM `eleve` WHERE id_classe=?";
21+
try (PreparedStatement statement = connection.prepareStatement(sqlQuery)) {
22+
statement.setInt(1, idClass);
23+
try (ResultSet results = statement.executeQuery()) {
24+
while (results.next()) {
25+
Student student = new Student(results.getInt("id_eleve"),results.getString("nom"),results.getString("prenom"),results.getInt("id_classe"));
26+
listStudents.add(student);
27+
}
28+
}
29+
}
30+
} catch (SQLException e) {
31+
e.printStackTrace(); }
32+
return listStudents;
33+
}
34+
35+
}
36+
37+

0 commit comments

Comments
 (0)