Skip to content

Commit

Permalink
Merge pull request #1 from Bachelor-final-project/CreatingMigrationsA…
Browse files Browse the repository at this point in the history
…ndEndpoints

Creating migrations and endpoints
  • Loading branch information
Ahmed-elhelou authored Nov 28, 2024
2 parents 8d3fd25 + 3c334d9 commit b25ba69
Show file tree
Hide file tree
Showing 134 changed files with 4,510 additions and 236 deletions.
69 changes: 69 additions & 0 deletions app/Http/Controllers/AttachmentController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace App\Http\Controllers;
use App\Http\Resources\AttachmentResource;
use App\Models\Attachment;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;

class AttachmentController extends Controller
{

public static function routeName(){
return Str::snake("Attachment");
}
public function __construct(Request $request)
{
parent::__construct($request);
}
public function index(Request $request)
{
return AttachmentResource::collection(Attachment::search($request)->sort($request)->paginate((request('per_page')??request('itemsPerPage'))??15));
}
public function store(Request $request)
{
if(!$this->user->is_permitted_to('store',Attachment::class,$request))
return response()->json(['message'=>'not_permitted'],422);

$validator = Validator::make($request->all(),Attachment::createRules($this->user));
if($validator->fails()){
return response()->json(['errors'=>$validator->errors()],422);
}
$attachment = Attachment::create($validator->validated());
if ($request->translations) {
foreach ($request->translations as $translation)
$attachment->setTranslation($translation['field'], $translation['locale'], $translation['value'])->save();
}
return new AttachmentResource($attachment);
}
public function show(Request $request,Attachment $attachment)
{
if(!$this->user->is_permitted_to('view',Attachment::class,$request))
return response()->json(['message'=>'not_permitted'],422);
return new AttachmentResource($attachment);
}
public function update(Request $request, Attachment $attachment)
{
if(!$this->user->is_permitted_to('update',Attachment::class,$request))
return response()->json(['message'=>'not_permitted'],422);
$validator = Validator::make($request->all(),Attachment::updateRules($this->user));
if($validator->fails()){
return response()->json(['errors'=>$validator->errors()],422);
}
$attachment->update($validator->validated());
if ($request->translations) {
foreach ($request->translations as $translation)
$attachment->setTranslation($translation['field'], $translation['locale'], $translation['value'])->save();
}
return new AttachmentResource($attachment);
}
public function destroy(Request $request,Attachment $attachment)
{
if(!$this->user->is_permitted_to('delete',Attachment::class,$request))
return response()->json(['message'=>'not_permitted'],422);
$attachment->delete();

return new AttachmentResource($attachment);
}
}
59 changes: 59 additions & 0 deletions app/Http/Controllers/AuthController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\User;
use Illuminate\Support\Facades\Hash;

class AuthController extends Controller
{
public function register(Request $request)
{
$request->validate([
'name' => 'required|string|max:255',
'email' => 'required|email|unique:users,email',
'password' => 'required|min:6|confirmed',
]);

$user = User::create([
'name' => $request->name,
'email' => $request->email,
'password' => Hash::make($request->password),
]);

return response()->json([
'message' => 'User registered successfully!',
'user' => $user,
], 201);
}

public function login(Request $request)
{
$request->validate([
'email' => 'required|email',
'password' => 'required'
]);

if (Auth::attempt($request->only('email', 'password'))) {
$request->session()->regenerate();
return response()->json(['message' => 'Logged in successfully!'], 200);
}

return response()->json(['message' => 'Invalid credentials'], 401);
}

public function logout(Request $request)
{
Auth::guard('web')->logout();
$request->session()->invalidate();
$request->session()->regenerateToken();
return response()->json(['message' => 'Logged out successfully!'], 200);
}

public function profile()
{
return response()->json(Auth::user());
}
}
69 changes: 69 additions & 0 deletions app/Http/Controllers/BeneficiaryController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace App\Http\Controllers;
use App\Http\Resources\BeneficiaryResource;
use App\Models\Beneficiary;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;

class BeneficiaryController extends Controller
{

public static function routeName(){
return Str::snake("Beneficiary");
}
public function __construct(Request $request)
{
parent::__construct($request);
}
public function index(Request $request)
{
return BeneficiaryResource::collection(Beneficiary::search($request)->sort($request)->paginate((request('per_page')??request('itemsPerPage'))??15));
}
public function store(Request $request)
{
if(!$this->user->is_permitted_to('store',Beneficiary::class,$request))
return response()->json(['message'=>'not_permitted'],422);

$validator = Validator::make($request->all(),Beneficiary::createRules($this->user));
if($validator->fails()){
return response()->json(['errors'=>$validator->errors()],422);
}
$beneficiary = Beneficiary::create($validator->validated());
if ($request->translations) {
foreach ($request->translations as $translation)
$beneficiary->setTranslation($translation['field'], $translation['locale'], $translation['value'])->save();
}
return new BeneficiaryResource($beneficiary);
}
public function show(Request $request,Beneficiary $beneficiary)
{
if(!$this->user->is_permitted_to('view',Beneficiary::class,$request))
return response()->json(['message'=>'not_permitted'],422);
return new BeneficiaryResource($beneficiary);
}
public function update(Request $request, Beneficiary $beneficiary)
{
if(!$this->user->is_permitted_to('update',Beneficiary::class,$request))
return response()->json(['message'=>'not_permitted'],422);
$validator = Validator::make($request->all(),Beneficiary::updateRules($this->user));
if($validator->fails()){
return response()->json(['errors'=>$validator->errors()],422);
}
$beneficiary->update($validator->validated());
if ($request->translations) {
foreach ($request->translations as $translation)
$beneficiary->setTranslation($translation['field'], $translation['locale'], $translation['value'])->save();
}
return new BeneficiaryResource($beneficiary);
}
public function destroy(Request $request,Beneficiary $beneficiary)
{
if(!$this->user->is_permitted_to('delete',Beneficiary::class,$request))
return response()->json(['message'=>'not_permitted'],422);
$beneficiary->delete();

return new BeneficiaryResource($beneficiary);
}
}
9 changes: 8 additions & 1 deletion app/Http/Controllers/Controller.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;

abstract class Controller
{
//
public $user;

public function __construct(Request $request)
{
if (auth('web')->user())
$this->user = auth('web')->user();
}
}
69 changes: 69 additions & 0 deletions app/Http/Controllers/LogController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace App\Http\Controllers;
use App\Http\Resources\LogResource;
use App\Models\Log;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;

class LogController extends Controller
{

public static function routeName(){
return Str::snake("Log");
}
public function __construct(Request $request)
{
parent::__construct($request);
}
public function index(Request $request)
{
return LogResource::collection(Log::search($request)->sort($request)->paginate((request('per_page')??request('itemsPerPage'))??15));
}
public function store(Request $request)
{
if(!$this->user->is_permitted_to('store',Log::class,$request))
return response()->json(['message'=>'not_permitted'],422);

$validator = Validator::make($request->all(),Log::createRules($this->user));
if($validator->fails()){
return response()->json(['errors'=>$validator->errors()],422);
}
$log = Log::create($validator->validated());
if ($request->translations) {
foreach ($request->translations as $translation)
$log->setTranslation($translation['field'], $translation['locale'], $translation['value'])->save();
}
return new LogResource($log);
}
public function show(Request $request,Log $log)
{
if(!$this->user->is_permitted_to('view',Log::class,$request))
return response()->json(['message'=>'not_permitted'],422);
return new LogResource($log);
}
public function update(Request $request, Log $log)
{
if(!$this->user->is_permitted_to('update',Log::class,$request))
return response()->json(['message'=>'not_permitted'],422);
$validator = Validator::make($request->all(),Log::updateRules($this->user));
if($validator->fails()){
return response()->json(['errors'=>$validator->errors()],422);
}
$log->update($validator->validated());
if ($request->translations) {
foreach ($request->translations as $translation)
$log->setTranslation($translation['field'], $translation['locale'], $translation['value'])->save();
}
return new LogResource($log);
}
public function destroy(Request $request,Log $log)
{
if(!$this->user->is_permitted_to('delete',Log::class,$request))
return response()->json(['message'=>'not_permitted'],422);
$log->delete();

return new LogResource($log);
}
}
69 changes: 69 additions & 0 deletions app/Http/Controllers/PermissionController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace App\Http\Controllers;
use App\Http\Resources\PermissionResource;
use App\Models\Permission;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;

class PermissionController extends Controller
{

public static function routeName(){
return Str::snake("Permission");
}
public function __construct(Request $request)
{
parent::__construct($request);
}
public function index(Request $request)
{
return PermissionResource::collection(Permission::search($request)->sort($request)->paginate((request('per_page')??request('itemsPerPage'))??15));
}
public function store(Request $request)
{
if(!$this->user->is_permitted_to('store',Permission::class,$request))
return response()->json(['message'=>'not_permitted'],422);

$validator = Validator::make($request->all(),Permission::createRules($this->user));
if($validator->fails()){
return response()->json(['errors'=>$validator->errors()],422);
}
$permission = Permission::create($validator->validated());
if ($request->translations) {
foreach ($request->translations as $translation)
$permission->setTranslation($translation['field'], $translation['locale'], $translation['value'])->save();
}
return new PermissionResource($permission);
}
public function show(Request $request,Permission $permission)
{
if(!$this->user->is_permitted_to('view',Permission::class,$request))
return response()->json(['message'=>'not_permitted'],422);
return new PermissionResource($permission);
}
public function update(Request $request, Permission $permission)
{
if(!$this->user->is_permitted_to('update',Permission::class,$request))
return response()->json(['message'=>'not_permitted'],422);
$validator = Validator::make($request->all(),Permission::updateRules($this->user));
if($validator->fails()){
return response()->json(['errors'=>$validator->errors()],422);
}
$permission->update($validator->validated());
if ($request->translations) {
foreach ($request->translations as $translation)
$permission->setTranslation($translation['field'], $translation['locale'], $translation['value'])->save();
}
return new PermissionResource($permission);
}
public function destroy(Request $request,Permission $permission)
{
if(!$this->user->is_permitted_to('delete',Permission::class,$request))
return response()->json(['message'=>'not_permitted'],422);
$permission->delete();

return new PermissionResource($permission);
}
}
Loading

0 comments on commit b25ba69

Please sign in to comment.