· 5 years ago · Feb 09, 2021, 05:52 PM
1 <?php
2 require_once("./httpResponseCode.php");
3
4 $requestType = $_SERVER["REQUEST_METHOD"];
5 $oid = null;
6
7 function getObjectIdParam(string $requestType) {
8 $objId = null;
9 if($requestType) {
10 if($requestType === "POST") {
11 $objId = filter_input(INPUT_POST, 'oid', FILTER_SANITIZE_STRING);
12 }
13 if($requestType === "GET") {
14 $objId = filter_input(INPUT_GET, 'oid', FILTER_SANITIZE_STRING);
15 }
16 }
17 return $objId;
18 }
19
20 $oid = getObjectIdParam($requestType);
21
22
23
24
25
26 // if($requestType == "GET") {
27 // $oid = filter_input(INPUT_POST, 'oid', FILTER_SANITIZE_STRING);
28 // }
29
30 // if($requestType == "POST") {
31 // $oid = filter_input(INPUT_GET, 'oid', FILTER_SANITIZE_STRING);
32 // if(isset($oid) == false || strlen($oid) > 32) {
33 // http_response_code(ResponseCode::BAD_REQUEST);
34 // }
35
36 // // - oid (Object Id)
37 // // Example: oid=AB123456X
38 // // Alphanumerical String Maximum length 32 characters
39 // // - name
40 // // Example: name=marcus%20winter
41 // // URL encoded string, maximum length 64 characters
42 // // - comment
43 // // Example: this%20is%20a%20comment
44 // // URL encoded string, no maximum length
45 // } else {
46 // http_response_code(ResponseCode::BAD_REQUEST);
47 // }
48
49/**
50 Create RESTFUL api:
51 - be written in oop php
52 - use MYSQL database to store and retrieve comments
53 - carry out security checks and error handling
54 - conform exactly to specification provided in Appendix 1
55 - not to use any external libraries or framworks
56
57
58 CRUD operations:
59 - Create and Read
60
61
62 Create:
63 Required Params:
64 - oid (Object Id)
65 Example: oid=AB123456X
66 Alphanumerical String Maximum length 32 characters
67 - name
68 Example: name=marcus%20winter
69 URL encoded string, maximum length 64 characters
70 - comment
71 Example: this%20is%20a%20comment
72 URL encoded string, no maximum length
73
74
75 HTTP Status Codes:
76 201 - OK, record created!
77 400 - Bad Request (parameter missing or invalid)
78 500 - Internal Server error (database connection failed)
79 Example 201:
80 {
81 id: 10
82 }
83
84 Read:
85 Required Params:
86 oid (Object Id) Alphanumerica string, max length 32 characters.
87 HTTP status codes:
88 200 - OK
89 204 - Ok, no content (e.g no comments for this object id)
90 400 - Bad request (e.g. parameter missing or invalid)
91 500 - Internal Serve Error (database connection failed)
92
93 Example Response 200:
94 {
95 "oid": "AB123456X",
96 "comments": [
97 {
98 "name": "Mary",
99 "comment": "this is a comment"
100 }, {
101 "name": "Anonymouse",
102 "comment": "Another comment"
103 }
104 ]
105 }
106
107
108
109 Database Structure:
110 oid: char 32
111 name: char 64
112 comment: varchar, no maximum length
113
114 [Object]
115 id char [primary key]
116 [Comment]
117 id integer [primary key]
118 objectId char [foreign key]
119 name char 64
120 comment varchar
121
122 [Object]
123 AB123456X <- char 32
124 [Comment]
125 id 0
126 objectId AB123456X
127 name "Mary"
128 comment "this is a comment"
129 [Comment]
130 id 1
131 objectId AB123456X
132 name "Anon"
133 comment "Another comment"
134
135 SELECT * FROM 'Comment' WHERE objectId = 'AB123456X';
136
137 */
138
139