93
frontend/modules/api/controllers/ManagerController.php
Normal file
93
frontend/modules/api/controllers/ManagerController.php
Normal file
@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
namespace frontend\modules\api\controllers;
|
||||
|
||||
use common\models\Manager;
|
||||
use common\models\ManagerEmployee;
|
||||
use common\models\User;
|
||||
use Yii;
|
||||
use yii\filters\auth\HttpBearerAuth;
|
||||
use yii\helpers\ArrayHelper;
|
||||
use yii\web\NotFoundHttpException;
|
||||
|
||||
class ManagerController extends \yii\rest\Controller
|
||||
{
|
||||
public function behaviors(): array
|
||||
{
|
||||
$behaviors = parent::behaviors();
|
||||
|
||||
$behaviors['authenticator']['authMethods'] = [
|
||||
HttpBearerAuth::className(),
|
||||
];
|
||||
|
||||
return $behaviors;
|
||||
}
|
||||
|
||||
public function verbs(): array
|
||||
{
|
||||
return [
|
||||
'get-manager' => ['get'],
|
||||
'get-employees-manager' => ['get'],
|
||||
'get-manager-list' => ['get'],
|
||||
];
|
||||
}
|
||||
|
||||
public function actionGetManagerList(): array
|
||||
{
|
||||
$managers = User::find()->select(['username','manager.id' , 'email'])
|
||||
->joinWith('manager')->where(['NOT',['manager.user_id' => null]])->all();
|
||||
|
||||
if(empty($managers)) {
|
||||
throw new NotFoundHttpException('Managers are not assigned');
|
||||
}
|
||||
|
||||
return $managers;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws NotFoundHttpException
|
||||
*/
|
||||
public function actionGetEmployeesManager()
|
||||
{
|
||||
$manager_id = Yii::$app->request->get('manager_id');
|
||||
if(empty($manager_id) or !is_numeric($manager_id))
|
||||
{
|
||||
throw new NotFoundHttpException('Incorrect manager ID');
|
||||
}
|
||||
|
||||
$users_list = User::find()->select(['user.id', 'user.username', 'user.email'])
|
||||
->joinWith('managerEmployee')
|
||||
->where(['manager_employee.manager_id' => $manager_id])
|
||||
->all();
|
||||
|
||||
if(empty($users_list)) {
|
||||
throw new NotFoundHttpException('Managers are not assigned or employees are not assigned to him');
|
||||
}
|
||||
|
||||
return $users_list;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws NotFoundHttpException
|
||||
*/
|
||||
public function actionGetManager(): array
|
||||
{
|
||||
$manager_id = Yii::$app->request->get('manager_id');
|
||||
if(empty($manager_id) or !is_numeric($manager_id))
|
||||
{
|
||||
throw new NotFoundHttpException('Incorrect manager ID');
|
||||
}
|
||||
|
||||
$manager = User::find()
|
||||
->select(['user.id', 'user.username', 'user.email'])
|
||||
->joinWith('manager')->where(['manager.id' => $manager_id])
|
||||
->all();
|
||||
|
||||
|
||||
if(empty($manager)) {
|
||||
throw new NotFoundHttpException('There is no such manager');
|
||||
}
|
||||
|
||||
return $manager;
|
||||
}
|
||||
}
|
149
frontend/modules/api/controllers/TaskController.php
Normal file
149
frontend/modules/api/controllers/TaskController.php
Normal file
@ -0,0 +1,149 @@
|
||||
<?php
|
||||
|
||||
namespace frontend\modules\api\controllers;
|
||||
|
||||
use common\models\Task;
|
||||
use Yii;
|
||||
use yii\base\InvalidConfigException;
|
||||
use yii\filters\auth\HttpBearerAuth;
|
||||
use yii\rest\Controller;
|
||||
use yii\web\BadRequestHttpException;
|
||||
use yii\web\NotFoundHttpException;
|
||||
use yii\web\ServerErrorHttpException;
|
||||
|
||||
class TaskController extends Controller
|
||||
{
|
||||
public function behaviors(): array
|
||||
{
|
||||
$behaviors = parent::behaviors();
|
||||
|
||||
$behaviors['authenticator']['authMethods'] = [
|
||||
HttpBearerAuth::className(),
|
||||
];
|
||||
|
||||
return $behaviors;
|
||||
}
|
||||
|
||||
public function verbs(): array
|
||||
{
|
||||
return [
|
||||
'get-task' => ['get'],
|
||||
'get-task-list' => ['get'],
|
||||
'create-task' => ['post'],
|
||||
'update-task' => ['put', 'patch'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws InvalidConfigException
|
||||
* @throws ServerErrorHttpException
|
||||
* @throws NotFoundHttpException
|
||||
*/
|
||||
public function actionUpdate(): ?Task
|
||||
{
|
||||
$model = $this->findModelTask(Yii::$app->request->post('task_id'));
|
||||
if(empty($model)) {
|
||||
throw new NotFoundHttpException('The task does not exist');
|
||||
}
|
||||
|
||||
$model->load(Yii::$app->request->getBodyParams(), '');
|
||||
if ($model->save() === false && !$model->hasErrors()) {
|
||||
throw new ServerErrorHttpException('Failed to update the object for unknown reason.');
|
||||
}
|
||||
|
||||
return $model;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @throws InvalidConfigException
|
||||
* @throws BadRequestHttpException
|
||||
* @throws ServerErrorHttpException
|
||||
*/
|
||||
public function actionCreateTask(): Task
|
||||
{
|
||||
$task = Yii::$app->getRequest()->getBodyParams();
|
||||
|
||||
$model = new Task();
|
||||
$model->load($task, '');
|
||||
|
||||
$this->validateTaskModel($model);
|
||||
$this->saveModel($model);
|
||||
|
||||
return $model;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ServerErrorHttpException
|
||||
*/
|
||||
protected function saveModel($model)
|
||||
{
|
||||
if ($model->save()) {
|
||||
$task = Yii::$app->getResponse();
|
||||
$task->setStatusCode(201);
|
||||
} elseif (!$model->hasErrors()) {
|
||||
throw new ServerErrorHttpException('Failed to create the object for unknown reason.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws BadRequestHttpException
|
||||
*/
|
||||
protected function validateTaskModel($model)
|
||||
{
|
||||
if(!$model->validate()) {
|
||||
throw new BadRequestHttpException(json_encode($model->errors));
|
||||
}
|
||||
|
||||
if (empty($model->project_id)or empty($model->status)
|
||||
or empty($model->description) or empty($model->title) or empty($model->project_user_id)) {
|
||||
throw new BadRequestHttpException(json_encode($model->errors));
|
||||
}
|
||||
}
|
||||
|
||||
public function actionGetTaskList(): array
|
||||
{
|
||||
$project_id = Yii::$app->request->get('project_id');
|
||||
if(empty($project_id) or !is_numeric($project_id))
|
||||
{
|
||||
throw new NotFoundHttpException('Incorrect project ID');
|
||||
}
|
||||
|
||||
$tasks = $this->findModelsById($project_id);
|
||||
|
||||
if(empty($tasks)) {
|
||||
throw new NotFoundHttpException('The project does not exist or there are no tasks for it');
|
||||
}
|
||||
|
||||
return $tasks;
|
||||
}
|
||||
|
||||
public function actionGetTask(): Task
|
||||
{
|
||||
$task_id = Yii::$app->request->get('task_id');
|
||||
if(empty($task_id) or !is_numeric($task_id))
|
||||
{
|
||||
throw new NotFoundHttpException('Incorrect task ID');
|
||||
}
|
||||
|
||||
$task = $this->findModelTask($task_id);
|
||||
|
||||
if(empty($task)) {
|
||||
throw new NotFoundHttpException('The task does not exist');
|
||||
}
|
||||
|
||||
return $task;
|
||||
|
||||
}
|
||||
|
||||
private function findModelTask($task_id): ?Task
|
||||
{
|
||||
return Task::findOne($task_id);
|
||||
}
|
||||
|
||||
private function findModelsById($project_id): array
|
||||
{
|
||||
return Task::find()->where(['project_id' => $project_id])->all();
|
||||
}
|
||||
}
|
73
frontend/modules/api/controllers/TaskUserController.php
Normal file
73
frontend/modules/api/controllers/TaskUserController.php
Normal file
@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace frontend\modules\api\controllers;
|
||||
|
||||
use common\models\TaskUser;
|
||||
use Yii;
|
||||
use yii\filters\auth\HttpBearerAuth;
|
||||
use yii\rest\Controller;
|
||||
use yii\web\BadRequestHttpException;
|
||||
use yii\web\NotFoundHttpException;
|
||||
|
||||
class TaskUserController extends Controller
|
||||
{
|
||||
public function behaviors(): array
|
||||
{
|
||||
$behaviors = parent::behaviors();
|
||||
|
||||
$behaviors['authenticator']['authMethods'] = [
|
||||
HttpBearerAuth::className(),
|
||||
];
|
||||
|
||||
return $behaviors;
|
||||
}
|
||||
|
||||
public function verbs(): array
|
||||
{
|
||||
return [
|
||||
'get-task-users' => ['get'],
|
||||
'set-task-user' => ['post', 'patch'],
|
||||
];
|
||||
}
|
||||
|
||||
public function actionSetTaskUser()
|
||||
{
|
||||
$taskUserModel = new TaskUser();
|
||||
|
||||
$params = Yii::$app->request->post();
|
||||
$taskUserModel->attributes = $params;
|
||||
|
||||
if(!$taskUserModel->validate()){
|
||||
throw new BadRequestHttpException(json_encode($taskUserModel->errors));
|
||||
}
|
||||
|
||||
$taskUserModel->save();
|
||||
|
||||
return $taskUserModel->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws NotFoundHttpException
|
||||
*/
|
||||
public function actionGetTaskUsers()
|
||||
{
|
||||
$task_id = Yii::$app->request->get('task_id');
|
||||
if(empty($task_id) or !is_numeric($task_id))
|
||||
{
|
||||
throw new NotFoundHttpException('Incorrect task ID');
|
||||
}
|
||||
|
||||
$tasks = $this->findUsers($task_id);
|
||||
|
||||
if(empty($tasks)) {
|
||||
throw new NotFoundHttpException('The task does not exist or there are no employees for it');
|
||||
}
|
||||
|
||||
return $tasks;
|
||||
}
|
||||
|
||||
private function findUsers($project_id): array
|
||||
{
|
||||
return TaskUser::find()->where(['task_id' => $project_id])->all();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user