You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Application-Web/src/Api/Controller/APITeamController.php

80 lines
2.6 KiB

<?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);
});
}
}