Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
projet-cdaw
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Thibaut Felten
projet-cdaw
Commits
cff2d3ee
Commit
cff2d3ee
authored
Nov 10, 2020
by
thibaut-felten
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
API REST
parent
89ae27da
Changes
7
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
151 additions
and
56 deletions
+151
-56
test-PDO-crud.php
backend/tp1/test-PDO-crud.php
+8
-6
DatabaseConnector.php
backend/tp2/DatabaseConnector.php
+4
-4
UserController.php
backend/tp2/UserController.php
+0
-39
UserModel.php
backend/tp2/UserModel.php
+32
-2
UsersController.php
backend/tp2/UsersController.php
+96
-0
api.php
backend/tp2/api.php
+6
-0
config.php
backend/tp2/config.php
+5
-5
No files found.
backend/tp1/test-PDO-crud.php
View file @
cff2d3ee
...
...
@@ -4,14 +4,16 @@
class
User
{
private
$id
;
private
$name
;
private
$email
;
// public function __construct($props = array()) { $this−>props = $props; }
protected
$props
;
// public function __get($prop) { return $this−>props[$prop]; }
// public function __set($prop, $val) { $this−>props[$prop] = $val; }
public
function
__get
(
$prop
)
{
return
$this
->
props
[
$prop
];
}
public
function
__set
(
$prop
,
$val
)
{
$this
->
props
[
$prop
]
=
$val
;
}
private
static
function
getAllUsers
(){
...
...
backend/tp2/DatabaseConnector.php
View file @
cff2d3ee
...
...
@@ -14,14 +14,14 @@ class DatabaseConnector {
protected
static
function
createPDO
()
{
// $db = new PDO("sqlite::memory");
$connectionString
=
"mysql:host="
.
_MYSQL
_HOST
;
$connectionString
=
"mysql:host="
.
DB
_HOST
;
if
(
defined
(
'_MYSQL_PORT'
))
$connectionString
.=
";port="
.
_MYSQL
_PORT
;
$connectionString
.=
";port="
.
DB
_PORT
;
$connectionString
.=
";dbname="
.
_MYSQL_DATABAS
E
;
$connectionString
.=
";dbname="
.
DB_DBNAM
E
;
static
::
$pdo
=
new
PDO
(
$connectionString
,
_MYSQL_USERNAME
,
_MYSQL
_PASSWORD
);
static
::
$pdo
=
new
PDO
(
$connectionString
,
DB_USER
,
DB
_PASSWORD
);
static
::
$pdo
->
setAttribute
(
PDO
::
ATTR_ERRMODE
,
PDO
::
ERRMODE_EXCEPTION
);
}
}
\ No newline at end of file
backend/tp2/UserController.php
deleted
100644 → 0
View file @
89ae27da
<?php
class
UsersController
{
private
$requestMethod
;
public
function
__construct
(
$requestMethod
)
{
$this
->
requestMethod
=
$requestMethod
;
}
public
function
processRequest
()
{
switch
(
$this
->
requestMethod
)
{
case
'GET'
:
$response
=
$this
->
getAllUsers
();
break
;
default
:
$response
=
$this
->
notFoundResponse
();
break
;
}
header
(
$response
[
'status_code_header'
]);
if
(
$response
[
'body'
])
{
echo
$response
[
'body'
];
}
}
private
function
getAllUsers
()
{
// TODO ...
}
private
function
notFoundResponse
()
{
$response
[
'status_code_header'
]
=
'HTTP/1.1 404 Not Found'
;
$response
[
'body'
]
=
null
;
return
$response
;
}
}
backend/tp2/UserModel.php
View file @
cff2d3ee
...
...
@@ -2,7 +2,37 @@
class
UserModel
{
public
static
function
getAllUsers
()
{
// TODO ... (cf. TP1)
public
static
function
getAllUsers
(){
$pdo
=
DatabaseConnector
::
current
();
$request
=
$pdo
->
prepare
(
"select * from users"
);
$request
->
execute
();
$allUsers
=
$request
->
fetchAll
(
PDO
::
FETCH_CLASS
|
PDO
::
FETCH_PROPS_LATE
,
get_called_class
());
return
$allUsers
;
}
public
static
function
getUserById
(
$id
){
$pdo
=
DatabaseConnector
::
current
();
$request
=
$pdo
->
prepare
(
"select * from users where id="
.
$id
);
$request
->
execute
();
$allUsers
=
$request
->
fetchAll
(
PDO
::
FETCH_CLASS
|
PDO
::
FETCH_PROPS_LATE
,
get_called_class
());
return
$allUsers
;
}
public
static
function
createUser
(
$name
,
$email
){
$pdo
=
DatabaseConnector
::
current
();
$request
=
$pdo
->
prepare
(
"insert into users(id, name, email) values (NULL, '"
.
$name
.
"','"
.
$email
.
"')"
);
$request
->
execute
();
}
public
static
function
deleteUser
(
$id
){
$pdo
=
DatabaseConnector
::
current
();
$request
=
$pdo
->
prepare
(
"delete from users where id="
.
$id
);
$request
->
execute
();
}
public
static
function
editUser
(
$id
,
$name
,
$email
){
$pdo
=
DatabaseConnector
::
current
();
$request
=
$pdo
->
prepare
(
"update users set name='"
.
$name
.
"', email='"
.
$email
.
"' where id="
.
$id
);
$request
->
execute
();
}
}
\ No newline at end of file
backend/tp2/UsersController.php
0 → 100644
View file @
cff2d3ee
<?php
class
UsersController
{
private
$requestMethod
;
private
$params
;
public
function
__construct
(
$requestMethod
,
$params
=
null
)
{
$this
->
requestMethod
=
$requestMethod
;
$this
->
params
=
$params
;
}
public
function
processRequest
()
{
switch
(
$this
->
requestMethod
)
{
case
'GET'
:
if
(
isset
(
$this
->
params
))
{
$response
=
$this
->
getUserById
((
$this
->
params
)[
0
]);
}
else
{
$response
=
$this
->
getAllUsers
();
}
break
;
case
'POST'
:
$response
=
$this
->
createUser
(
$_POST
[
'name'
],
$_POST
[
'email'
]);
break
;
case
'DELETE'
:
$response
=
$this
->
deleteUser
((
$this
->
params
)[
0
]);
break
;
case
'PUT'
:
if
(
isset
(
$this
->
params
))
{
$response
=
$this
->
editUser
((
$this
->
params
)[
0
],
(
$this
->
params
)[
1
],
(
$this
->
params
)[
2
]);
}
break
;
default
:
$response
=
$this
->
notFoundResponse
();
break
;
}
header
(
$response
[
'status_code_header'
]);
if
(
$response
[
'body'
])
{
echo
$response
[
'body'
];
}
}
private
function
createUser
(
$name
,
$email
)
{
$user
=
UserModel
::
createUser
(
$name
,
$email
);
$response
=
[];
$response
[
'status_code_header'
]
=
http_response_code
(
200
);
$response
[
'body'
]
=
"User created"
;
return
$response
;
}
private
function
editUser
(
$id
,
$name
,
$email
)
{
$user
=
UserModel
::
editUser
(
$id
,
$name
,
$email
);
$response
=
[];
$response
[
'status_code_header'
]
=
http_response_code
(
200
);
$response
[
'body'
]
=
"User updated"
;
return
$response
;
}
private
function
deleteUser
(
$id
)
{
$user
=
UserModel
::
deleteUser
(
$id
);
$response
=
[];
$response
[
'status_code_header'
]
=
http_response_code
(
200
);
$response
[
'body'
]
=
"User deleted"
;
return
$response
;
}
private
function
getUserById
(
$id
)
{
$user
=
UserModel
::
getUserById
(
$id
);
$response
=
[];
$response
[
'status_code_header'
]
=
http_response_code
(
200
);
$response
[
'body'
]
=
json_encode
(
$user
);
return
$response
;
}
private
function
getAllUsers
()
{
$users
=
UserModel
::
getAllUsers
();
$response
=
[];
$response
[
'status_code_header'
]
=
http_response_code
(
200
);
$response
[
'body'
]
=
json_encode
(
$users
);
return
$response
;
}
private
function
notFoundResponse
()
{
$response
[
'status_code_header'
]
=
'HTTP/1.1 404 Not Found'
;
$response
[
'body'
]
=
null
;
return
$response
;
}
}
backend/tp2/api.php
View file @
cff2d3ee
...
...
@@ -42,12 +42,18 @@ $requestMethod = $_SERVER["REQUEST_METHOD"];
$controllerName
=
$route
[
'controller'
];
switch
(
$controllerName
)
{
case
'users'
:
// GET api.php?/users
// POST api.php?/users
$controller
=
new
UsersController
(
$requestMethod
);
break
;
case
'user'
:
$params
=
$route
[
'params'
];
$controller
=
new
UsersController
(
$requestMethod
,
$params
);
break
;
default
:
header
(
"HTTP/1.1 404 Not Found"
);
exit
();
...
...
backend/tp2/config.php
View file @
cff2d3ee
<?php
define
(
'_MYSQL_HOST'
,
'localhost'
);
define
(
'_MYSQL_PORT'
,
3306
);
define
(
'_MYSQL_DBNAME'
,
'dbtest'
);
define
(
'_MYSQL_USER'
,
'root'
);
define
(
'_MYSQL_PASSWORD'
,
'root'
);
\ No newline at end of file
define
(
'DB_HOST'
,
'localhost'
);
define
(
'DB_PORT'
,
3306
);
define
(
'DB_DBNAME'
,
'dbtest'
);
define
(
'DB_USER'
,
'root'
);
define
(
'DB_PASSWORD'
,
'root'
);
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment