Skip to content

Commit

Permalink
#39 adding jersey_number parameter to user creation and modification …
Browse files Browse the repository at this point in the history
…API, adding jersey_number parameter to player_at_roster creation API
  • Loading branch information
YoSarin committed Jan 4, 2020
1 parent 82276b8 commit 4a06e3d
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 8 deletions.
4 changes: 2 additions & 2 deletions data/migrations/deploy/00009_adding_numbers_to_players.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
ALTER TABLE player
ADD COLUMN jersey_number NUMBER;
ADD COLUMN jersey_number int(11);

ALTER TABLE player_at_roster
ADD COLUMN jersey_number NUMBER;
ADD COLUMN jersey_number int(11);
8 changes: 8 additions & 0 deletions src/App/Controller/Player.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function create(\Slim\Http\Request $request, $response, $args)
$gdprConsent = trim($request->getParam("gdpr_consent"));
$nationalityID = trim($request->getParam("nationality_id"));
$personalIdentificationNumber = trim($request->getParam("personal_identification_number"));
$jerseyNumber = trim($request->getParam("jersey_number"));

$user = UserModel::loggedUser($request->getToken());

Expand All @@ -44,6 +45,9 @@ public function create(\Slim\Http\Request $request, $response, $args)
if (!empty($personalIdentificationNumber)) {
$p->setPersonalIdentificationNumber($personalIdentificationNumber);
}
if (!empty($jerseyNumber) && is_numeric($jerseyNumber)) {
$p->setJerseyNumber($jerseyNumber);
}
$p->save();

// Render index view
Expand All @@ -67,6 +71,7 @@ public function update(\Slim\Http\Request $request, $response, $args)
$gdprConsent = trim($request->getParam("gdpr_consent"));
$nationalityID = trim($request->getParam("nationality_id"));
$personalIdentificationNumber = trim($request->getParam("personal_identification_number"));
$jerseyNumber = trim($request->getParam("jersey_number"));

$p = \App\Model\Player::loadById($playerId);
if (!empty($email)) {
Expand Down Expand Up @@ -100,6 +105,9 @@ public function update(\Slim\Http\Request $request, $response, $args)
if (!empty($personalIdentificationNumber)) {
$p->setPersonalIdentificationNumber($personalIdentificationNumber);
}
if (!empty($jerseyNumber) && is_numeric($jerseyNumber)) {
$p->setJerseyNumber($jerseyNumber);
}

$p->save();

Expand Down
3 changes: 2 additions & 1 deletion src/App/Controller/Roster.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public function addPlayer(\App\Request $request, $response, $args)
{
list($rosterId, $playerId) = $request->requireParams(['roster_id', 'player_id']);
$role = trim($request->getParam("role", PlayerAtRoster::DEFAULT_ROLE));
$jerseyNumber = trim($request->getParam("jersey_number", null));

$roster = \App\Model\Roster::loadById($rosterId);
if (!$roster) {
Expand All @@ -93,7 +94,7 @@ public function addPlayer(\App\Request $request, $response, $args)
throw new Http400("Player is already on roster of another team");
}

$roster = PlayerAtRoster::create($playerId, $rosterId, $role);
$roster = PlayerAtRoster::create($playerId, $rosterId, $role, $jerseyNumber);
$roster->save();

return $this->container->view->render(
Expand Down
17 changes: 14 additions & 3 deletions src/App/Model/Player.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ class Player extends \App\Model
self::SEX_FEMALE,
];

protected static $fields = ["id", "first_name", "last_name", "birth_date", "created_at", "email", "phone", "sex", "state", "nationality_id", "gdpr_consent", "personal_identification_number"];
protected static $fields = [
"id", "first_name", "last_name",
"birth_date", "created_at", "email",
"phone", "sex", "state",
"nationality_id", "gdpr_consent", "personal_identification_number",
"jersey_number"
];

public function getFullName()
{
Expand All @@ -35,8 +41,12 @@ public function getHomeTeam()
{
}

public static function create($firstName, $lastName, $sex, $email = null, $birthDate = null, $phone = null, $state = self::STATE_ACTIVE, $nationalityID = null, $gdprConsent = false, $personalIdentificationNumber = null)
{
public static function create($firstName, $lastName, $sex, $email = null, $birthDate = null, $phone = null, $state = self::STATE_ACTIVE, $nationalityID = null, $gdprConsent = false, $personalIdentificationNumber = null, $jerseyNumber = null)
{
if ($jerseyNumber !== null && $jerseyNumber !== "" && (int)$jerseyNumber <= 0) {
throw new WrongParam("Jersey number '{$this->getJerseyNumber()}' is not valid - it has to be higher than 0");
}

$i = new self();
$i->setFirstName($firstName);
$i->setLastName($lastName);
Expand All @@ -49,6 +59,7 @@ public static function create($firstName, $lastName, $sex, $email = null, $birth
$i->setGdprConsent($gdprConsent);
$i->setCreatedAt(date("Y-m-d H:i:s", time()));
$i->setPersonalIdentificationNumber($personalIdentificationNumber);
$i->setJerseyNumber($jerseyNumber);
return $i;
}

Expand Down
14 changes: 12 additions & 2 deletions src/App/Model/PlayerAtRoster.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,25 @@

class PlayerAtRoster extends \App\Model
{
protected static $fields = ["id", "player_id", "roster_id", "role"];
protected static $fields = ["id", "player_id", "roster_id", "role", "jersey_number"];

protected static $allowedRoles = ["player", "captain", "spirit_captain", "medical", "coach", "other_support"];

const DEFAULT_ROLE = "player";

public static function create($playerId, $rosterId, $role = PlayerAtRoster::DEFAULT_ROLE)
public static function create($playerId, $rosterId, $role = PlayerAtRoster::DEFAULT_ROLE, $jerseyNumber = null)
{
$i = new self();

if (empty($jerseyNumber) || !is_numeric($jerseyNumber)) {
$player = Player::loadById($playerId);
$jerseyNumber = $player->getJerseyNumber();
}

$i->setPlayerId($playerId);
$i->setRosterId($rosterId);
$i->setRole($role);
$i->setJerseyNumber($jerseyNumber);
$i->setSince(date("Y-m-d H:i:s", time()));

return $i;
Expand All @@ -28,6 +35,9 @@ protected function onSaveValidation()
if (!in_array($this->getRole(), self::$allowedRoles)) {
throw new WrongParam("Role {$this->getRole()} does not exist.");
}
if ($this->getJerseyNumber() < 0) {
throw new WrongParam("Jersey number '{$this->getJerseyNumber()}' is not valid - it has to be higher than 0");
}
if (self::exists(["player_id" => $this->getPlayerId(), "roster_id" => $this->getRosterId(), "role" => $this->getRole()])) {
throw new Duplicate("Player is already part of this team with same role");
}
Expand Down

0 comments on commit 4a06e3d

Please sign in to comment.