Add Administration API for teams's administration panel #97
Merged
maxime.batista
merged 7 commits from admin/api-teams
into master
1 year ago
@ -0,0 +1,79 @@
|
||||
<?php
|
||||
|
||||
namespace IQBall\Api\Controller;
|
||||
|
||||
use IQBall\Api\APIControl;
|
||||
use IQBall\Core\Data\Account;
|
||||
use IQBall\Core\Data\Team;
|
||||
use IQBall\Core\Data\TeamInfo;
|
||||
use IQBall\Core\Gateway\TeamGateway;
|
||||
use IQBall\Core\Http\HttpCodes;
|
||||
use IQBall\Core\Http\HttpRequest;
|
||||
use IQBall\Core\Http\HttpResponse;
|
||||
use IQBall\Core\Http\JsonHttpResponse;
|
||||
use IQBall\Core\Model\TeamModel;
|
||||
use IQBall\Core\Validation\DefaultValidators;
|
||||
|
||||
class APITeamController {
|
||||
private TeamModel $teamModel;
|
||||
|
||||
/**
|
||||
* @param TeamModel $teamModel
|
||||
*/
|
||||
public function __construct(TeamModel $teamModel) {
|
||||
$this->teamModel = $teamModel;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $req_params
|
||||
* @return HttpResponse
|
||||
*/
|
||||
public function listTeams(array $req_params): HttpResponse {
|
||||
return APIControl::runCheckedFrom($req_params, [
|
||||
'start' => [DefaultValidators::isUnsignedInteger()],
|
||||
'n' => [DefaultValidators::isUnsignedInteger()],
|
||||
], function (HttpRequest $req) {
|
||||
$teams = $this->teamModel->listAll(intval($req['start']), intval($req['n']));
|
||||
return new JsonHttpResponse([
|
||||
"totalCount" => $this->teamModel->countTeam(),
|
||||
"teams" => $teams,
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
||||
public function addTeam(): HttpResponse {
|
||||
return APIControl::runChecked([
|
||||
'name' => [DefaultValidators::name()],
|
||||
'picture' => [DefaultValidators::isURL()],
|
||||
'mainColor' => [DefaultValidators::hexColor()],
|
||||
'secondaryColor' => [DefaultValidators::hexColor()],
|
||||
|
||||
], function (HttpRequest $req) {
|
||||
$this->teamModel->createTeam($req['name'], $req['picture'], $req['mainColor'], $req['secondaryColor']);
|
||||
return HttpResponse::fromCode(HttpCodes::OK);
|
||||
});
|
||||
}
|
||||
|
||||
public function deleteTeamSelected(): HttpResponse {
|
||||
return APIControl::runChecked([
|
||||
'teams' => [],
|
||||
], function (HttpRequest $req) {
|
||||
$this->teamModel->deleteTeamSelected($req['teams']);
|
||||
return HttpResponse::fromCode(HttpCodes::OK);
|
||||
});
|
||||
}
|
||||
|
||||
public function updateTeam(int $id): HttpResponse {
|
||||
return APIControl::runChecked([
|
||||
'name' => [DefaultValidators::name()],
|
||||
'picture' => [DefaultValidators::isURL()],
|
||||
'mainColor' => [DefaultValidators::hexColor()],
|
||||
'secondaryColor' => [DefaultValidators::hexColor()],
|
||||
], function (HttpRequest $req) {
|
||||
$this->teamModel->editTeam($req['id'], $req['name'], $req['picture'], $req['mainColor'], $req['secondaryColor']);
|
||||
return HttpResponse::fromCode(HttpCodes::OK);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in new issue