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.
55 lines
1.5 KiB
55 lines
1.5 KiB
<?php
|
|
|
|
namespace App\Controller\Api;
|
|
|
|
use App\Controller\Control;
|
|
use App\Http\HttpCodes;
|
|
use App\Http\HttpRequest;
|
|
use App\Http\HttpResponse;
|
|
use App\Http\JsonHttpResponse;
|
|
use App\Model\TacticModel;
|
|
use App\Validation\Validators;
|
|
|
|
/**
|
|
* API endpoint related to tactics
|
|
*/
|
|
class APITacticController {
|
|
private TacticModel $model;
|
|
|
|
/**
|
|
* @param TacticModel $model
|
|
*/
|
|
public function __construct(TacticModel $model) {
|
|
$this->model = $model;
|
|
}
|
|
|
|
public function updateName(int $tactic_id): HttpResponse {
|
|
return Control::runChecked([
|
|
"name" => [Validators::lenBetween(1, 50), Validators::nameWithSpaces()]
|
|
], function (HttpRequest $request) use ($tactic_id) {
|
|
$this->model->updateName($tactic_id, $request["name"]);
|
|
return HttpResponse::fromCode(HttpCodes::OK);
|
|
});
|
|
}
|
|
|
|
public function newTactic(): HttpResponse {
|
|
return Control::runChecked([
|
|
"name" => [Validators::lenBetween(1, 50), Validators::nameWithSpaces()]
|
|
], function (HttpRequest $request) {
|
|
$tactic = $this->model->makeNew($request["name"]);
|
|
$id = $tactic->getId();
|
|
return new JsonHttpResponse(["id" => $id]);
|
|
});
|
|
}
|
|
|
|
public function getTacticInfo(int $id): HttpResponse {
|
|
$tactic_info = $this->model->get($id);
|
|
|
|
if ($tactic_info == null) {
|
|
return new JsonHttpResponse("could not find tactic #$id", HttpCodes::NOT_FOUND);
|
|
}
|
|
|
|
return new JsonHttpResponse($tactic_info);
|
|
}
|
|
|
|
} |