Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moussaab/teams #57

Merged
merged 11 commits into from
Dec 8, 2023
59 changes: 58 additions & 1 deletion app/Http/Controllers/API/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Http\Repositories\GeneralRepository;
use App\Http\Repositories\UserRepository;
use App\Http\Repositories\TrackRepository;
use App\Http\Repositories\TeamRepository;
use Illuminate\Http\Request;

class AdminController extends BaseController
Expand All @@ -14,11 +15,14 @@ class AdminController extends BaseController
private $challengeRepository;
private $trackRepository;
private $generalRepository;
public function __construct(UserRepository $userRepository, ChallengeRepository $challengeRepository, TrackRepository $trackRepository, GeneralRepository $generalRepository) {
private $teamRepository;
public function __construct(UserRepository $userRepository, ChallengeRepository $challengeRepository, TrackRepository $trackRepository, GeneralRepository $generalRepository, TeamRepository $teamRepository)
{
$this->userRepository = $userRepository;
$this->challengeRepository = $challengeRepository;
$this->trackRepository = $trackRepository;
$this->generalRepository = $generalRepository;
$this->teamRepository = $teamRepository;
}

public function get_all_users() {
Expand Down Expand Up @@ -133,4 +137,57 @@ public function get_stats() {
return $this->sendResponse($response['data'], $response['message']);
}

public function get_teams()
{
$response = $this->teamRepository->getTeams();
return $this->sendResponse($response['data'], $response['message']);
}

public function get_team($id)
{
$response = $this->teamRepository->getTeam($id);
return $this->sendResponse($response['data'], $response['message']);

}
MoussaabBadla marked this conversation as resolved.
Show resolved Hide resolved

public function create_team(Request $request)
{
$response = $this->teamRepository->createTeam($request);
if (!$response['success'])
return $this->sendError($response['message'], $response['data']);
return $this->sendResponse($response['data'], $response['message']);

}

public function update_team(Request $request, $id)
{
$response = $this->teamRepository->updateTeam($request, $id);
if (!$response['success'])
return $this->sendError($response['message'], $response['data']);
return $this->sendResponse($response['data'], $response['message']);

}
public function delete_team($id)
{
$response = $this->teamRepository->deleteTeam($id);
return $this->sendResponse($response['data'], $response['message']);

}

public function add_member(Request $request, $id)
{
$response = $this->teamRepository->addMember($request, $id);
if (!$response['success']) return $this->sendError($response['message'], $response['data']);

return $this->sendResponse($response['data'], $response['message']);

}

public function remove_member(Request $request)
{
$response = $this->teamRepository->removeMember($request);
if (!$response['success']) return $this->sendError($response['message'], $response['data']);
return $this->sendResponse($response['data'], $response['message']);

}
}
1 change: 1 addition & 0 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,6 @@ class Kernel extends HttpKernel
'onlySPA' =>\App\Http\Middleware\OnlySPA::class,
'verifyAuthStep' =>\App\Http\Middleware\EnsureReachedRightStep::class,
'hasAccessToTrack' => \App\Http\Middleware\EnsureHasAccessToTrack::class,
'teamExists' => \App\Http\Middleware\EnsureTeamExists::class
];
}
29 changes: 29 additions & 0 deletions app/Http/Middleware/EnsureTeamExists.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Http\Middleware;

use App\Models\Team;
use Closure;
use Illuminate\Http\Request;

class EnsureTeamExists
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
*/
public function handle(Request $request, Closure $next)
{
$team = Team::find($request->route('id'));
if (!$team)
return response()->json([
'success' => false,
'message' => 'Team can not be found!'
], 400);

return $next($request);
}
}
159 changes: 159 additions & 0 deletions app/Http/Repositories/TeamRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
<?php

namespace App\Http\Repositories;

use App\Http\Resources\TeamResource;
use App\Http\Resources\User\ParticipantResource;
use App\Models\Team;
use App\Models\User;

use Illuminate\Support\Facades\Validator;

class TeamRepository
{
public function getTeams()
{
$response = [];
$teams = Team::all();
$response['success'] = true;
$response['data'] = TeamResource::collection($teams);
$response['message'] = 'Successfully retrieved all the teams!';
return $response;

}
public function getTeam($id)
{
$response = [];
$team = Team::find($id);
$response['success'] = true;
$response['data'] = new TeamResource($team);
$response['message'] = 'Successfully retrieved the team!';
return $response;
}

public function createTeam($request)
{
$response = [];
$validator = Validator::make($request->all(),
['name' => 'required|unique:teams,name',
'token' => 'required|unique:teams,token|min:8',
]);
if ($validator->fails()) {
$response['success'] = false;
$response['message'] = 'Validation failed!';
$response['data'] = $validator->errors();
return $response;
}
$team = Team::create([
'name' => $request->name,
'token'=> $request->token,
]);
$response['success'] = true;
$response['data'] = new TeamResource($team);
$response['message'] = 'Successfully created the team!';
return $response;
}

public function updateTeam($request, $id)
{

$response = [];

$validator = Validator::make($request->all(),
['name' => 'required|unique:teams,name',
]);

if ($validator->fails()) {
$response['success'] = false;
$response['message'] = 'Validation failed!';
$response['data'] = $validator->errors();
return $response;
}

$team = Team::find($id);
$team->name = $request->name;
MoussaabBadla marked this conversation as resolved.
Show resolved Hide resolved
$team->save();
$response['success'] = true;
$response['data'] = new TeamResource($team);
$response['message'] = 'Successfully updated the team!';
return $response;
}

public function deleteTeam($id)
{
$response = [];
$team = Team::find($id);
$team->delete();
$response['success'] = true;
$response['data'] = [];
$response['message'] = 'Successfully deleted the team!';
return $response;
}

public function addMember($request, $id)
{
$response = [];

$validator = Validator::make($request->all(),
['participant_id' => 'required',
]);

if ($validator->fails()) {
$response['success'] = false;
$response['message'] = 'Validation failed!';
$response['data'] = $validator->errors();
return $response;
}


$member = User::find($request->participant_id);
if (!$member) {
$response['success'] = false;
$response['message'] = 'Participant not found!';
$response['data'] = [];
return $response;
}


$member->team_id = $id;
$member->save();
$response['success'] = true;
$response['data'] = new ParticipantResource($member);
$response['message'] = 'Successfully added the member!';
return $response;
}


public function removeMember($request)
{
$response = [];

$validator = Validator::make($request->all(),
['participant_id' => 'required',
]);

if ($validator->fails()) {
$response['success'] = false;
$response['message'] = 'Validation failed!';
$response['data'] = $validator->errors();
return $response;
}



$member = User::find($request->participant_id);
if (!$member) {
$response['success'] = false;
$response['message'] = 'Participant not found!';
$response['data'] = [];
return $response;
}

$member->team_id = null;
$member->save();
$response['success'] = true;
$response['data'] = new ParticipantResource($member);
$response['message'] = 'Successfully removed the member!';
return $response;
}
}
23 changes: 23 additions & 0 deletions app/Http/Resources/TeamResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Http\Resources;

use App\Http\Resources\User\ParticipantResource;
use App\Models\User;
use Illuminate\Http\Resources\Json\JsonResource;


class TeamResource extends JsonResource
{

public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'token'=> $this->token,
'participants' => ParticipantResource::collection($this->participants),
];
}

}
1 change: 1 addition & 0 deletions app/Http/Resources/User/ParticipantResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function toArray($request)
'email_verified' => $this->email_verified_at ? true: false,
'track' => $this->track?->type,
'submissions' => SubmissionResource::collection($this->submissions),
'team' => $this->team?->name,
];
}
}
19 changes: 19 additions & 0 deletions app/Models/Team.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Team extends Model
{
use HasFactory;

protected $fillable = ['name', 'token'];

public function participants()
{
return $this->hasMany('App\Models\User');
}
}

8 changes: 7 additions & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class User extends Authenticatable implements MustVerifyEmail
'points',
'role',
'track_id',
'ip'
'ip',
'team_id'

];

Expand Down Expand Up @@ -78,5 +79,10 @@ public function username() {
return "full_name";
}

public function team()
{
return $this->belongsTo('App\Models\Team');
}


}
25 changes: 25 additions & 0 deletions database/factories/TeamFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\Hash;

/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Challenge>
*/
class TeamFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition()
{
return [
'name' => fake()->name(),
'token' => fake()->text(14),
];
}
}
Loading
Loading