-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestsResource.php
69 lines (60 loc) · 1.62 KB
/
TestsResource.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
/** Resource for adding a new test. URL: tests.
* Methods:
* <ul>
* <li>POST to create a new test.
* </li>
* </ul>
*
*/
require_once("HttpResource.php");
require_once("db.php");
class TestsResource extends HttpResource {
protected $userId;
protected $quizzId;
protected function do_post() {
if (empty($_POST["userId"]) || empty($_POST["quizzId"])) {
$this->exit_error(400, "userIdandquizzIdRequired");
}else{
if (is_numeric($_POST["userId"]) && is_numeric($_POST["quizzId"])) {
$this->userId = 0 + $_POST["userId"];
$this->quizzId = 0 + $_POST["quizzId"];
if (!is_int($this->userId) || $this->userId <= 0 || !is_int($this->quizzId) || $this->quizzId <= 0) {
$this->exit_error(400, "userIdOrquizzIdNotPositiveInteger");
}
}
else {
$this->exit_error(400, "userIdorquizzIdNotNumeric");
}
}
try {
$db = db::getConnection();
$sql = "INSERT INTO test (quiz_id, user_id) VALUES (:quizzId, :userId)";
$stmt = $db->prepare($sql);
$stmt->bindValue(":userId", $this->userId);
$stmt->bindValue(":quizzId", $this->quizzId);
$ok = $stmt->execute();
if ($ok) {
$nb = $stmt->rowCount();
if ($nb == 0) {
$this->exit_error(404);
}
else {
$last_insert = $db->lastInsertId();
//die($last_insert);
$this->statusCode = 204;
$this->headers[] = "New test created successfully with id: ".$last_insert;
}
}
else {
$erreur = $stmt->errorInfo();
$this->exit_error(409, $erreur[1]." : ".$erreur[2]);
}
}
catch (PDOException $e) {
$this->exit_error(500, $e->getMessage());
}
}
}
TestsResource::run();
?>