Skip to content

Commit

Permalink
Added listing of player and team
Browse files Browse the repository at this point in the history
  • Loading branch information
HancilSequeira committed Mar 29, 2020
1 parent 4279ecb commit e5bc6dd
Show file tree
Hide file tree
Showing 10 changed files with 176 additions and 32 deletions.
4 changes: 4 additions & 0 deletions config/routing/player.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@
<default key="_controller">App\Controller\PlayerController::deleteAction</default>
</route>

<route id="player_list_base_on_id_name" path="/players/{id}" methods="GET">
<default key="_controller">App\Controller\PlayerController::playerListBasedOnIdOrNameAction</default>
</route>

</routes>
12 changes: 12 additions & 0 deletions config/routing/team.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,16 @@
<default key="_controller">App\Controller\TeamController::indexAction</default>
</route>

<route id="add_player_to_team" path="/team-players" methods="POST|PUT">
<default key="_controller">App\Controller\TeamController::addPlayerToTeamAction</default>
</route>

<route id="team_list" path="/players" methods="GET">
<default key="_controller">App\Controller\TeamController::teamListAction</default>
</route>

<route id="player_list_base_on_team" path="/players/{id}" methods="GET">
<default key="_controller">App\Controller\TeamController::playerListBasedOnTeamAction</default>
</route>

</routes>
22 changes: 22 additions & 0 deletions src/AbstractClass/AbstractValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,28 @@ public function validateName($name){
return $errors;
}

public function validatePlayerId($playerId){
$playerIdConstraint = new Assert\NotBlank();
$playerIdConstraint->message ="Player Id should not be null";
$errors = $this->validator->validate(
$playerId,
$playerIdConstraint
);

return $errors;
}

public function validateTeamId($teamId){
$teamId = new Assert\NotBlank();
$teamId->message ="Player Id should not be null";
$errors = $this->validator->validate(
$teamId,
$teamId
);

return $errors;
}



}
30 changes: 22 additions & 8 deletions src/Controller/PlayerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,15 @@ public function indexAction(Request $request){
return new JsonResponse($this->utilityService->JSONResponseCreation($error, null, CustomResponseConstant::VALIDATION_ERROR, Response::HTTP_OK));
}
$data = $request->request->all();
$playerObject = $this->em->getRepository(Players::class)->find($request->query->get('id'));

if ($playerObject instanceof Players) {
$playerObject->setFirstName($data["firstName"]);
$playerObject->setLastName($data["lastName"]);
$playerObject->setPlayerImageUri($data["playerImageURI"]);
$this->em->persist($playerObject);
} else {
if(!empty($request->query->get('id'))) {
$playerObject = $this->em->getRepository(Players::class)->find($request->query->get('id'));
if ($playerObject instanceof Players) {
$playerObject->setFirstName($data["firstName"]);
$playerObject->setLastName($data["lastName"]);
$playerObject->setPlayerImageUri($data["playerImageURI"]);
$this->em->persist($playerObject);
}
}else {
$playerObject = new Players();
$playerObject->setFirstName($data["firstName"]);
$playerObject->setLastName($data["lastName"]);
Expand Down Expand Up @@ -97,4 +98,17 @@ public function deleteAction(Request $request){
}
}

public function playerListBasedOnIdOrNameAction(Request$request,$id){
try{
$playerBasedOnTeamArray = $this->em->getRepository(Players::class)->getPlayerListBasedOnIdOrName($id);
if(empty($playerBasedOnTeamArray)){
return new JsonResponse($this->utilityService->JSONResponseCreation(null, null, CustomResponseConstant::NO_DATA, Response::HTTP_OK));
} else {
return new JsonResponse($this->utilityService->JSONResponseCreation(null, $playerBasedOnTeamArray, CustomResponseConstant::USER_DATA_SET, Response::HTTP_OK));
}
} catch (\Exception $e){
return new JsonResponse($this->utilityService->JSONResponseCreation($e->getMessage(), null, CustomResponseConstant::INTERNAL_SERVER, Response::HTTP_INTERNAL_SERVER_ERROR));
}
}

}
74 changes: 68 additions & 6 deletions src/Controller/TeamController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\constant\CustomResponseConstant;
use App\Entity\Players;
use App\Entity\Team;
use App\Entity\TeamPlayer;
use App\Service\PlayerService;
use App\Service\TeamService;
use App\Service\UtilityService;
Expand Down Expand Up @@ -47,12 +48,13 @@ public function indexAction(Request $request){
return new JsonResponse($this->utilityService->JSONResponseCreation($error, null, CustomResponseConstant::VALIDATION_ERROR, Response::HTTP_OK));
}
$data = $request->request->all();
$teamObject = $this->em->getRepository(Team::class)->find($request->query->get('id'));

if ($teamObject instanceof Team) {
$teamObject->setName($data["name"]);
$teamObject->setLogoURI($data["logoURI"]);
$this->em->persist($teamObject);
if(!empty($request->query->get('id'))){
$teamObject = $this->em->getRepository(Team::class)->find($request->query->get('id'));
if ($teamObject instanceof Team) {
$teamObject->setName($data["name"]);
$teamObject->setLogoURI($data["logoURI"]);
$this->em->persist($teamObject);
}
} else {
$teamObject = new Team();
$teamObject->setName($data["name"]);
Expand All @@ -66,4 +68,64 @@ public function indexAction(Request $request){
return new JsonResponse($this->utilityService->JSONResponseCreation($e->getMessage(), null, CustomResponseConstant::INTERNAL_SERVER, Response::HTTP_INTERNAL_SERVER_ERROR));
}
}

public function addPlayerToTeamAction(Request $request){
try {
$error = $this->teamService->validateTeamPlayerData($request->request->all());

if (!empty($error)) {
return new JsonResponse($this->utilityService->JSONResponseCreation($error, null, CustomResponseConstant::VALIDATION_ERROR, Response::HTTP_OK));
}
$data = $request->request->all();
$teamObject = $this->em->getRepository(Team::class)->find($data["teamId"]);

if (!$teamObject instanceof Team) {
return new JsonResponse($this->utilityService->JSONResponseCreation(null, null, CustomResponseConstant::INVALID_TEAM, Response::HTTP_OK));
}

$playerObject = $this->em->getRepository(Players::class)->find($data["playerId"]);

if (!$playerObject instanceof Players) {
return new JsonResponse($this->utilityService->JSONResponseCreation(null, null, CustomResponseConstant::INVALID_USER, Response::HTTP_OK));
}

$teamPlayer = new TeamPlayer();
$teamPlayer->setPlayer($playerObject);
$teamPlayer->setTeam($teamObject);
$this->em->persist($teamPlayer);
$this->em->flush();

return new JsonResponse($this->utilityService->JSONResponseCreation(null, $request->request->all(), CustomResponseConstant::USER_DATA_SET, Response::HTTP_OK));
} catch (\Exception $e){
return new JsonResponse($this->utilityService->JSONResponseCreation($e->getMessage(), null, CustomResponseConstant::INTERNAL_SERVER, Response::HTTP_INTERNAL_SERVER_ERROR));
}
}

public function teamListAction(Request $request){
try{
$teamListArray = $this->em->getRepository(Team::class)->getTeamList();
if(empty($teamListArray)){
return new JsonResponse($this->utilityService->JSONResponseCreation(null, null, CustomResponseConstant::NO_DATA, Response::HTTP_OK));
} else {
return new JsonResponse($this->utilityService->JSONResponseCreation(null, $teamListArray, CustomResponseConstant::USER_DATA_SET, Response::HTTP_OK));
}
} catch (\Exception $e){
return new JsonResponse($this->utilityService->JSONResponseCreation($e->getMessage(), null, CustomResponseConstant::INTERNAL_SERVER, Response::HTTP_INTERNAL_SERVER_ERROR));
}
}

public function playerListBasedOnTeamAction(Request $request, $id){
try{
$playerBasedOnTeamArray = $this->em->getRepository(Team::class)->getPlayerListBasedOnTeam($id);
if(empty($playerBasedOnTeamArray)){
return new JsonResponse($this->utilityService->JSONResponseCreation(null, null, CustomResponseConstant::NO_DATA, Response::HTTP_OK));
} else {
return new JsonResponse($this->utilityService->JSONResponseCreation(null, $playerBasedOnTeamArray, CustomResponseConstant::USER_DATA_SET, Response::HTTP_OK));
}
} catch (\Exception $e){
return new JsonResponse($this->utilityService->JSONResponseCreation($e->getMessage(), null, CustomResponseConstant::INTERNAL_SERVER, Response::HTTP_INTERNAL_SERVER_ERROR));
}
}


}
12 changes: 12 additions & 0 deletions src/Repository/PlayersRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,16 @@ public function deletePlayerById($playerId)
;
}

public function getPlayerListBasedOnIdOrName($id){
return $this->createQueryBuilder('p')
->select('p.firstName,p.lastName, p.playerImageUri')
->Where('p.id = :id')
->orWhere('p.firstName = :name')
->orWhere('p.lastName = :name')
->setParameter('id', $id)
->setParameter('name', $id)
->getQuery()
->getArrayResult();
}

}
30 changes: 14 additions & 16 deletions src/Repository/TeamRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,30 @@ public function __construct(ManagerRegistry $registry)
parent::__construct($registry, Team::class);
}

// /**
// * @return Team[] Returns an array of Team objects
// */
/*
public function findByExampleField($value)
/**
* @return Team[] Returns an array of Team objects
*/
public function getTeamList()
{
return $this->createQueryBuilder('t')
->andWhere('t.exampleField = :val')
->setParameter('val', $value)
->orderBy('t.id', 'ASC')
->setMaxResults(10)
->select('t.name, t.logoURI')
->getQuery()
->getResult()
;
}
*/

/*
public function findOneBySomeField($value): ?Team
public function getPlayerListBasedOnTeam($id)
{
return $this->createQueryBuilder('t')
->andWhere('t.exampleField = :val')
->setParameter('val', $value)
->select('p.firstName,p.lastName, p.playerImageUri')
->leftJoin('t.teamPlayer','tp')
->leftJoin('tp.player', 'p')
->Where('t.id = :id')
->orWhere('t.name = :name')
->setParameter('id', $id)
->setParameter('name', $id)
->getQuery()
->getOneOrNullResult()
->getArrayResult();
;
}
*/
}
18 changes: 18 additions & 0 deletions src/Service/TeamService.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,22 @@ public function validatePlayerData($playerData){

}

public function validateTeamPlayerData($playerTeamData){
$playerIdError = $this->validatePlayerId($playerTeamData['playerId']);

if(!empty($playerIdError)){
foreach ($playerIdError as $err) {
return $err;
}
}

$teamIdError = $this->validateTeamId($playerTeamData['teamId']);

if(!empty($teamIdError)){
foreach ($teamIdError as $err) {
return $err;
}
}
}

}
5 changes: 3 additions & 2 deletions src/Service/UtilityService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ public function JSONResponseCreation($error = null, $data = null , $message, $co
$response["error"] = $message;
}

if($data){
if($data) {
$response["data"] = $data;
$response["message"] = $message;
}
$response["message"] = $message;

$response["code"] = $code;

return $response;
Expand Down
1 change: 1 addition & 0 deletions src/constant/CustomResponseConstant.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ class CustomResponseConstant
const TEAM_ALREADY_EXISTS = "TEAM_ALREADY_EXISTS";
const VALIDATION_ERROR = "Validation errors in your request";
const INTERNAL_SERVER = "Internal Server";
const NO_DATA = "No Data";
}

0 comments on commit e5bc6dd

Please sign in to comment.