ptut-vhost/build/api/module/ProfessorController.php

116 lines
2.6 KiB
PHP

<?php
/**
* Created by PhpStorm.
* User: lucas
* Date: 27/02/18
* Time: 16:19
*/
namespace api\module;
use database\core\Repo;
use database\repo\professor;
use error\core\Error;
use error\core\Err;
class ProfessorController{
/* (1) Returns 1 or all professors
*
* @prof_id<int> [OPT] The professor UID
*
* @return professors<array> The professor(s) data
*
---------------------------------------------------------*/
public static function get($args){
$prof_id = null;
extract($args);
/* Get the professor repo */
/** @var professor $prof_repo */
$prof_repo = Repo::getRepo('professor');
/* (1) If @prof_id is set -> getById()
---------------------------------------------------------*/
if( !is_null($prof_id) ){
/* (1) Only get one professor by its id */
$fetch_prof = $prof_repo->getById((int) $prof_id);
/* (2) If nothing found -> return empty set */
if( is_null($fetch_prof) )
return ['professors' => []];
/* (3) Else -> return the professor */
return ['professors' => [$fetch_prof]];
}
/* (2) Else -> getAll()
---------------------------------------------------------*/
return ['professors' => $prof_repo->getAll()];
}
/* (2) Creates a new professor
*
* @lastName<String> The professor's lastName
* @firstName<String> The professor's firstName
* @category<int> The professor's category ID
* @hoursToDo<int> The professor's number of hours to do
* @initials<int> The professor's initials
* @isAdmin<bool> Whether the professor is an admin
* @casLogin<String> The professor's CAS username
*
* @return prof_id<int> The created professor UID (if no error)
*
---------------------------------------------------------*/
public static function post($args){
$lastName = "";
$firstName = "";
$category = 0;
$hoursToDo = 0;
$initials = "";
$isAdmin = false;
$casLogin = "";
extract($args);
/* Get the professor repo */
/** @var professor $prof_repo */
$prof_repo = Repo::getRepo('professor');
/* (1) Check if professor already exists */
$exists = $prof_repo->exists($lastName, $firstName);
/* (2) If found -> already exists */
if( !is_null($exists) )
return ['error' => new Error(Err::AlreadyExists)];
/* (3) Else try to create */
$repo_rtn = $prof_repo->create(
$lastName,
$firstName,
$category,
$hoursToDo,
$initials,
$isAdmin,
is_null($casLogin) ? "" : $casLogin
);
/* (4) If repo error -> return it */
if( is_null($repo_rtn) )
return ['error' => new Error(Err::RepoError)];
/* (5) Else return UID */
return ['created_uid' => $repo_rtn];
}
}