-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7015dac
commit be7aa75
Showing
1 changed file
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
|
||
namespace App\Controller; | ||
|
||
use App\Config\Middleware; | ||
use App\Model\UserModel; | ||
|
||
class UserApiController extends Middleware | ||
{ | ||
|
||
public function __construct() | ||
{ | ||
parent::__construct(); | ||
} | ||
|
||
//* 'validateToken()' specifies if the method requires token validation to run (must be in the first line of a controller method) | ||
//* 'validateParams()' data type argument defaults to STRING, modify to INTEGER, BOOLEAN or EMAIL if needed | ||
|
||
// User create | ||
public function create() | ||
{ | ||
$user_id = Middleware::generateId(); | ||
$email = $this->validateParams('email', $this->param['email'], EMAIL); | ||
$password = $this->validateParams('password', $this->param['password'], STRING); | ||
$model = new UserModel; | ||
$model->setUserId($user_id); | ||
$model->setEmail($email); | ||
$model->setPassword(Middleware::hash($password)); | ||
$this->result = $model->create('user'); | ||
if ($this->result == false) { | ||
$message = 'User already exists.'; | ||
} else { | ||
$message = "User created successfully."; | ||
} | ||
Middleware::returnResponse(RESPONSE_MESSAGE, $message, $this->result); | ||
} | ||
|
||
// User read all | ||
public function readAll() | ||
{ | ||
$this->validateToken(); | ||
$model = new UserModel; | ||
$this->result = $model->readAll('user'); | ||
if ($this->result == false) { | ||
$message = 'Failed to fetch all Users.'; | ||
} else { | ||
$message = "All Users fetched successfully."; | ||
} | ||
Middleware::returnResponse(RESPONSE_MESSAGE, $message, $this->result); | ||
} | ||
|
||
// User read unique | ||
public function readUnique() | ||
{ | ||
$this->validateToken(); | ||
$user_id = $this->validateParams('user_id', $this->param['user_id'], STRING); | ||
$model = new UserModel; | ||
$model->setUserId($user_id); | ||
$this->result = $model->readUnique('user'); | ||
if ($this->result == false) { | ||
$message = 'Failed to fetch unique User.'; | ||
} else { | ||
$message = 'Unique User fetched successfully.'; | ||
} | ||
Middleware::returnResponse(RESPONSE_MESSAGE, $message, $this->result); | ||
} | ||
|
||
// User update | ||
public function update() | ||
{ | ||
$this->validateToken(); | ||
$user_id = $this->validateParams('user_id', $this->param['user_id'], STRING); | ||
$email = $this->validateParams('email', $this->param['email'], EMAIL); | ||
$password = $this->validateParams('password', $this->param['password'], STRING); | ||
$model = new UserModel; | ||
$model->setUserId($user_id); | ||
$model->setEmail($email); | ||
$model->setPassword(Middleware::hash($password)); | ||
$this->result = $model->update('user'); | ||
if ($this->result == false) { | ||
$message = 'User does not exist.'; | ||
} else { | ||
$message = "User informations updated successfully."; | ||
} | ||
Middleware::returnResponse(RESPONSE_MESSAGE, $message, $this->result); | ||
} | ||
|
||
// User Delete | ||
public function delete() | ||
{ | ||
$this->validateToken(); | ||
$user_id = $this->validateParams('user_id', $this->param['user_id'], STRING); | ||
$model = new UserModel; | ||
$model->setUserId($user_id); | ||
$this->result = $model->delete('user'); | ||
if ($this->result == false) { | ||
$message = 'User does not exist.'; | ||
} else { | ||
$message = "User deleted successfully."; | ||
} | ||
Middleware::returnResponse(RESPONSE_MESSAGE, $message, $this->result); | ||
} | ||
} |