tacticModel = $tacticModel; $this->teamModel = $teamModel; } /** * update name of tactic, specified by tactic identifier, given in url. * @param int $tactic_id * @param Account $account * @return HttpResponse */ public function updateName(int $tactic_id, Account $account): HttpResponse { return APIControl::runChecked([ "name" => [DefaultValidators::lenBetween(1, 50), DefaultValidators::nameWithSpaces()], ], function (HttpRequest $request) use ($tactic_id, $account) { $failures = $this->tacticModel->updateName($tactic_id, $request["name"], $account->getUser()->getId()); if (!empty($failures)) { //TODO find a system to handle Unauthorized error codes more easily from failures. return new JsonHttpResponse($failures, HttpCodes::BAD_REQUEST); } return HttpResponse::fromCode(HttpCodes::OK); }); } /** * @param int $id * @param Account $account * @return HttpResponse */ public function saveContent(int $id, Account $account): HttpResponse { return APIControl::runChecked([ "content" => [], ], function (HttpRequest $req) use ($id) { if ($fail = $this->tacticModel->updateContent($id, json_encode($req["content"]))) { return new JsonHttpResponse([$fail], HttpCodes::BAD_REQUEST); } return HttpResponse::fromCode(HttpCodes::OK); }); } /** * @param int $tacticId * @param Account $account * @return HttpResponse */ public function canShareTactic(int $tacticId, Account $account): HttpResponse { if ($this->tacticModel->canShareTactic($tacticId, $account)) { return HttpResponse::fromCode(HttpCodes::OK); } return new JsonHttpResponse(["message" => "Vous ne pouvez pas partager cette tactique"], HttpCodes::FORBIDDEN); } /** * @param int $tacticId * @param Account $account * @return HttpResponse */ public function canShareTacticToTeam(int $tacticId, Account $account): HttpResponse { return Control::runChecked([ "id" => [], "name" => [], "picture" => [], "main_color" => [], "second_color" => [], ], function (HttpRequest $request) use ($tacticId, $account) { if ($this->canShareTactic($tacticId, $account)->getCode() == HttpCodes::OK) { if ($this->teamModel->canShareTacticToTeam($request["id"], $account->getUser()->getEmail())) { return HttpResponse::fromCode(HttpCodes::OK); } } return new JsonHttpResponse(["message" => "Action non autorisée"], HttpCodes::FORBIDDEN); }); } /** * @param int $tacticId * @param Account $account * @return HttpResponse */ public function shareTacticToTeam(int $tacticId, Account $account): HttpResponse { return Control::runChecked([ "id" => [], "name" => [], "picture" => [], "main_color" => [], "second_color" => [], ], function (HttpRequest $request) use ($tacticId) { $this->teamModel->shareTacticToTeam($request["id"], $tacticId); return HttpResponse::fromCode(HttpCodes::OK); }); } /** * @param int $tacticId * @param Account $account * @return HttpResponse */ public function shareTacticToAccount(int $tacticId, Account $account): HttpResponse { return Control::runChecked([ "email" => [], ], function (HttpRequest $request) use ($tacticId) { $this->tacticModel->shareTacticToAccountMail($request["email"], $tacticId); return HttpResponse::fromCode(HttpCodes::OK); }); } /** * @param int $tacticId * @param Account $account * @return HttpResponse */ public function unshareTacticToTeam(int $tacticId, Account $account): HttpResponse { return Control::runChecked([ "id" => [], "name" => [], "picture" => [], "main_color" => [], "second_color" => [], ], function (HttpRequest $request) use ($tacticId, $account) { if ($this->teamModel->canShareTacticToTeam($request["id"], $account->getUser()->getEmail())) { $this->teamModel->unshareTacticToTeam($tacticId, $request["id"]); return HttpResponse::fromCode(HttpCodes::OK); } return new JsonHttpResponse(["message" => "Action non autorisée"], HttpCodes::FORBIDDEN); }); } /** * @param int $userId * @return HttpResponse given user information. */ public function getUserTactics(int $userId): HttpResponse { $tactics = $this->tacticModel->listAllOf($userId); $response = array_map(fn(TacticInfo $t) => [ 'id' => $t->getId(), 'name' => $t->getName(), 'court' => $t->getCourtType(), 'creation_date' => $t->getCreationDate(), ], $tactics); return new JsonHttpResponse($response); } }