The Goal of This Challenge Is To Build An API That Will Manage
The Goal of This Challenge Is To Build An API That Will Manage
update, list, delete) players with some skills and select the best
players with the desired position/skill.
name
position
list of skills
'defender'
'midfielder'
'forward'
skill name
value
'defense'
'attack'
'speed'
'strength'
'stamina'
The player needs to have at least one skill, but it does not need to
have values for all available skills. A valid player in JSON format
is:
Automatic Testing
Team lead will use their test suite to run automatic tests to verify your
implementation.
You are also encouraged to write your own test cases under tests
directory.
API Description
Basic Config:
The app needs to be served at http://localhost:3000 and the API
requests should be available at http://localhost:3000/api/.
If the field is inside an array, the error message should show the
field inside it, for example if the skill of the player (inside
playerSkills array) has an invalid value, the error should contain
the "skill" field and the invalid value for that skill.
The solution should return only the first error found. If the request
to create the player has invalid values for position and skill fields,
the solution should return only the message for one of those
fields. The validation rules do not need to follow any specific
order.
[
{
"id": 1,
"name": "player name 1",
"position": "defender",
"playerSkills": [
{
"id": 1,
"skill": "defense",
"value": 60,
"playerId": 1
},
{
"id": 2,
"skill": "speed",
"value": 80,
"playerId": 1
}
]
},
{
"id": 2,
"name": "player name 2",
"position": "midfielder",
"playerSkills": [
{
"id": 3,
"skill": "attack",
"value": 20,
"playerId": 2
},
{
"id": 4,
"skill": "speed",
"value": 70,
"playerId": 2
}
]
}
]
The expected result from this endpoint is the best list of players
from the database according to the requirements ex:
[
{
"name": "player name 2",
"position": "midfielder",
"playerSkills": [
{
"skill": "speed",
"value": 90
}
]
},
{
"name": "player name 3",
"position": "defender",
"playerSkills": [
{
"skill": "strength",
"value": 50
},
{
"skill": "stamina",
"value": 2
}
]
},
{
"name": "player name 4",
"position": "defender",
"playerSkills": [
{
"skill": "strength",
"value": 37
}
]
}
];
https://github.com/git-guides/install-git
02742dd143ee4f43ecd4cec7b29fa9d4064828d7
use Illuminate\Http\Request;
use App\Models\Player;
use App\Models\PlayerSkill;
use Validator;
if ($validator->fails()) {
return response()->json(['errors' => $validator->errors()], 400);
}
$selectedPlayers = [];
// If no players found with the desired skill, find the best players in the
position
if ($players->isEmpty()) {
$players = Player::where('position', $position)
->orderByDesc('playerSkills.value')
->get();
}