Skip to content

Commit

Permalink
Restored user controller
Browse files Browse the repository at this point in the history
  • Loading branch information
KhomsiAdam committed Nov 21, 2021
1 parent 7015dac commit be7aa75
Showing 1 changed file with 103 additions and 0 deletions.
103 changes: 103 additions & 0 deletions app/controllers/UserApiController.php
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);
}
}

0 comments on commit be7aa75

Please sign in to comment.