Browse Source

API finalisée (nofake)

Brendan Abolivier 9 years ago
parent
commit
ff8b71cb5f

web/api/view/palmares.php → web/api/controller/palmares.php View File

1
 <?php
1
 <?php
2
 
2
 
3
-try {
3
+if(!empty($_GET['login']) && !empty($_GET['score'])) {
4
+	Score::add($_GET['login'], $_GET['score']);
5
+	$scArray = array(
6
+		"status" => 1,
7
+		"message" => "score_add_success"
8
+	);
9
+} else {
4
 	$scores = Score::getScores(10);
10
 	$scores = Score::getScores(10);
5
 	$scArray = array();
11
 	$scArray = array();
6
 	for($i = 0; $i < sizeof($scores); $i++) {
12
 	for($i = 0; $i < sizeof($scores); $i++) {
9
 			"score" => $scores[$i]->getScore()
15
 			"score" => $scores[$i]->getScore()
10
 		));
16
 		));
11
 	}
17
 	}
12
-}catch(Exception $e) {
13
-	echo json_encode(array("erreur" => $e->getMessage()));
14
 }
18
 }

web/api/view/questions.php → web/api/controller/questions.php View File


+ 32
- 8
web/api/index.php View File

4
 
4
 
5
 header("Content-Type:application/json");
5
 header("Content-Type:application/json");
6
 
6
 
7
-if(!empty($_GET['page'])) {
8
-  if($_GET['page'] == "palmares") {
9
-    include("view/palmares.php");
10
-    echo json_encode($scArray);
11
-  }
12
-} else {
13
-  include("view/questions.php");
14
-	echo json_encode($categories);
7
+try {
8
+	if(!empty($_GET['page'])) {
9
+		if($_GET['page'] == "palmares") {
10
+			if(include("controller/palmares.php")) {
11
+				$scArray["status"] = 1;
12
+				echo json_encode($scArray);
13
+			} else {
14
+				echo json_encode(array(
15
+					"status" => 0,
16
+					"source" => "root",
17
+					"message" => "Erreur d'inclusion"
18
+				));
19
+			}
20
+		}
21
+	} else {
22
+		if(include("controller/questions.php")) {
23
+			$categories["status"] = 1;
24
+			echo json_encode($categories);
25
+		} else {
26
+			echo json_encode(array(
27
+				"status" => 0,
28
+				"source" => "root",
29
+				"message" => "Erreur d'inclusion"
30
+			));
31
+		}
32
+	}
33
+} catch(Exception $e) {
34
+	echo json_encode(array(
35
+		"status" => 0,
36
+		"source" => $e->getTrace()[0]["class"],
37
+		"message" => $e->getMessage()
38
+	));
15
 }
39
 }

+ 1
- 1
web/api/model/class.categorie.php View File

4
 	private $bdd;
4
 	private $bdd;
5
 	private $nomCat;
5
 	private $nomCat;
6
 	private $questsets;
6
 	private $questsets;
7
-	
7
+
8
 	function __construct($nomCat) {
8
 	function __construct($nomCat) {
9
 		$this->bdd = new Connector();
9
 		$this->bdd = new Connector();
10
 		$this->selectCat($nomCat);
10
 		$this->selectCat($nomCat);

+ 5
- 9
web/api/model/class.connector.php View File

34
 				$whereClause = " $upName ";
34
 				$whereClause = " $upName ";
35
 				foreach($value as $array) {
35
 				foreach($value as $array) {
36
 					if(sizeof($array) != 3) {
36
 					if(sizeof($array) != 3) {
37
-						throw new Exception('Nombre de paramètres incorrect (WHERE). Les paramètres passés sont : '
38
-							.implode(',',$array));
37
+						throw new Exception('wrong_arg_nmbr_where');
39
 					}
38
 					}
40
 
39
 
41
 					$whereClause .= $array[0]." ".$array[1]." :".$array[0]." AND ";
40
 					$whereClause .= $array[0]." ".$array[1]." :".$array[0]." AND ";
44
 				$request .= substr($whereClause, 0, -5);
43
 				$request .= substr($whereClause, 0, -5);
45
 			} else if(($upName = strtoupper($name)) == "ORDER BY") {
44
 			} else if(($upName = strtoupper($name)) == "ORDER BY") {
46
 				if(sizeof($value) != 2 && substr($value[0], -2) != "()") {
45
 				if(sizeof($value) != 2 && substr($value[0], -2) != "()") {
47
-					throw new Exception('Nombre de paramètres incorrects (ORDER BY). Les paramètres passés sont : '
48
-						.implode(',', $value));
46
+					throw new Exception('wrong_arg_nmbr_order_by');
49
 				}
47
 				}
50
 
48
 
51
 				$request .= " ".$upName." ".implode(' ', $value);
49
 				$request .= " ".$upName." ".implode(' ', $value);
57
 					// La colonne "limit" contient un index de départ et un nombre de champs
55
 					// La colonne "limit" contient un index de départ et un nombre de champs
58
 					$request .= " $upName ".$value[0].",".$value[1];
56
 					$request .= " $upName ".$value[0].",".$value[1];
59
 				} else {
57
 				} else {
60
-					throw new Exception('Nombre de paramètres incorrects (LIMIT). Les paramètres passés sont : '
61
-						.implode(',', $value));
58
+					throw new Exception('wrong_arg_numbr_limit');
62
 				}
59
 				}
63
 			} else {
60
 			} else {
64
-				throw new Exception('Argument '.strtoupper($name).' inconnu');
61
+				throw new Exception('unknown_arg');
65
 			}
62
 			}
66
 		}
63
 		}
67
 
64
 
99
 			array_push($arrayVerif, $value);
96
 			array_push($arrayVerif, $value);
100
 		}
97
 		}
101
 		$request = substr($request, 0, -1)." WHERE ";
98
 		$request = substr($request, 0, -1)." WHERE ";
102
-		var_dump($request);
103
 		foreach($update['where'] as $value) {
99
 		foreach($update['where'] as $value) {
104
 			$request .= $value[0].$value[1]."? AND ";
100
 			$request .= $value[0].$value[1]."? AND ";
105
-			array_push($arrayVerif, $value[2]);			
101
+			array_push($arrayVerif, $value[2]);
106
 		}
102
 		}
107
 		$request = substr($request, 0, -5);
103
 		$request = substr($request, 0, -5);
108
 
104
 

+ 2
- 0
web/api/model/class.score.php View File

69
           "set" => array("score" => $score)
69
           "set" => array("score" => $score)
70
         );
70
         );
71
         $bdd->Update("scores", $update);
71
         $bdd->Update("scores", $update);
72
+      } else {
73
+        throw new Exception('higher_score_present');
72
       }
74
       }
73
     }
75
     }
74
   }
76
   }

+ 1
- 5
web/api/tests.php View File

2
 
2
 
3
 require_once('model/classes.php');
3
 require_once('model/classes.php');
4
 
4
 
5
-try {
6
-	Score::add("Yolo", 200);
7
-}catch(Exception $e) {
8
-	echo("Erreur : ".$e->getMessage());
9
-}
5
+Score::add("Yolo", 200);