Skip to content

Commit

Permalink
feat: fetch a role in an organisation endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
sparkybug committed Aug 15, 2024
2 parents 05402a2 + fe881bf commit 44f812f
Show file tree
Hide file tree
Showing 32 changed files with 1,729 additions and 874 deletions.
Empty file added -
Empty file.
3 changes: 2 additions & 1 deletion app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Console;

use App\Jobs\SendEmailRequestsJob;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

Expand All @@ -12,7 +13,7 @@ class Kernel extends ConsoleKernel
*/
protected function schedule(Schedule $schedule): void
{
// $schedule->command('inspire')->hourly();
$schedule->job(new SendEmailRequestsJob())->everyMinute();
}

/**
Expand Down
39 changes: 39 additions & 0 deletions app/Http/Controllers/Api/V1/Admin/AdminDashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,48 @@
use App\Models\User;
use App\Models\Product;
use Illuminate\Http\Response;
use Illuminate\Http\Request;

class AdminDashboardController extends Controller
{
public function getUsers(Request $request)
{
// Get the 'status' and 'is_disabled' query parameters
$status = $request->query('status'); // For filtering by active or inactive status
$isDisabled = $request->query('is_disabled'); // For filtering by disabled status
$createdAtFrom = $request->query('created_at_from'); // Start date for filtering
$createdAtTo = $request->query('created_at_to'); // End date for filtering

// Build the query
$query = User::select('id', 'name', 'email', 'is_active', 'created_at',)
->orderBy('created_at', 'desc');

// Apply filters if provided
if ($status !== null) {
if ($status === 'true') {
$query->where('status', 'true');
} elseif ($status === 'false') {
$query->where('status', 'false');
}
}

if ($isDisabled !== null) {
$isDisabled = filter_var($isDisabled, FILTER_VALIDATE_BOOLEAN); // Convert to boolean
$query->where('is_disabled', $isDisabled);
}

if ($createdAtFrom) {
$query->where('created_at', '>=', $createdAtFrom);
}

if ($createdAtTo) {
$query->where('created_at', '<=', $createdAtTo);
}
// Paginate results
$users = $query->paginate(15);

return response()->json($users);
}
public function getStatistics()
{
$currentMonth = now()->startOfMonth();
Expand Down
212 changes: 108 additions & 104 deletions app/Http/Controllers/Api/V1/Admin/FaqController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,142 +3,146 @@
namespace App\Http\Controllers\Api\V1\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\CreateFaqRequest;
use App\Http\Requests\UpdateFaqRequest;
use App\Models\Faq;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Validator;

use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
use Exception;

class FaqController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
public function store(Request $request)
{
$faqs = Faq::where('status', 1)->get()->map(function ($faq) {
return [
'id' => $faq->id,
'question' => $faq->question,
'answer' => $faq->answer,
'category' => $faq->category,
'createdBy' => "ADMIN",
'createdAt' => $faq->created_at,
'updatedAt' => $faq->updated_at,
];
});
try {
$validatedData = $request->validate([
'question' => 'required|string|max:255',
'answer' => 'required|string',
'category' => 'required|string',
]);

$faq = Faq::create([
'id' => Str::uuid(),
'question' => $validatedData['question'],
'answer' => $validatedData['answer'],
'category' => $validatedData['category'],
]);

return response()->json([
'status_code' => 201,
'message' => 'FAQ created successfully',
'data' => $faq
], 201);

return response()->json([
'status_code' => 200,
'message' => "Faq fetched successfully",
'data' => $faqs
], Response::HTTP_OK);
} catch (ValidationException $e) {
return response()->json([
'status_code' => 422,
'message' => 'Validation failed',
'data' => $e->errors()
], 422);

} catch (Exception $e) {
return response()->json([
'status_code' => 500,
'message' => 'An error occurred while creating the FAQ',
'data' => []
], 500);
}
}

/**
* Store a newly created resource in storage.
*/
public function store(CreateFaqRequest $request)

public function index()
{
try {
$data = $request->validated();

$faq = Faq::create($data);

return response()->json([
'status_code' => Response::HTTP_CREATED,
'message' => "The FAQ has been successfully created.",
'data' => [
$faqs = Faq::all()->map(function ($faq) {
return [
'id' => $faq->id,
'created_at' => $faq->created_at->toIso8601String(),
'updated_at' => $faq->updated_at->toIso8601String(),
'question' => $faq->question,
'answer' => $faq->answer,
'category' => $faq->category,
'createdBy' => "",
'createdAt' => $faq->created_at,
'updatedAt' => $faq->updated_at,
]
], Response::HTTP_CREATED);
} catch (\Exception $e) {
];
});

return response()->json([
'message' => 'Internal server error',
'status' => Response::HTTP_INTERNAL_SERVER_ERROR
], Response::HTTP_INTERNAL_SERVER_ERROR);
'status_code' => 200,
'message' => 'Faq fetched successfully',
'data' => $faqs
], 200);

} catch (Exception $e) {
return response()->json([
'status_code' => 500,
'message' => 'An error occurred while fetching FAQs',
'data' => []
], 500);
}
}

/**
* Display the specified resource.
*/
public function show(Faq $faq)
{
return response()->json([
'status_code' => 200,
'message' => "Faq returned successfully",
'data' => $faq
], 200);
}

/**
* Update the specified resource in storage.
*/
public function update(UpdateFaqRequest $request, Faq $faq)

public function update(Request $request, $id)
{
if (auth()->user()->role !== 'admin') {
return response()->json([
'status_code' => Response::HTTP_FORBIDDEN,
'message' => 'Only admin users can update a faq',
], Response::HTTP_FORBIDDEN);
}

try {
$data = $request->validated();

$faq->update($data);

$validatedData = $request->validate([
'question' => 'required|string|max:255',
'answer' => 'required|string',
'category' => 'required|string',
]);

$faq = Faq::findOrFail($id);

$faq->update([
'question' => $validatedData['question'],
'answer' => $validatedData['answer'],
'category' => $validatedData['category'],
]);

return response()->json([
'status_code' => Response::HTTP_CREATED,
'message' => "The FAQ has been updated created.",
'status_code' => 200,
'message' => 'FAQ updated successfully',
'data' => [
'id' => $faq->id,
'question' => $faq->question,
'answer' => $faq->answer,
'category' => $faq->category,
'createdBy' => "",
'createdAt' => $faq->created_at,
'updatedAt' => $faq->updated_at,
'id' => $faq->id,
'created_at' => $faq->created_at->toIso8601String(),
'updated_at' => $faq->updated_at->toIso8601String(),
]
], Response::HTTP_CREATED);
} catch (\Exception $e) {
], 200);

} catch (ValidationException $e) {
return response()->json([
'message' => 'Internal server error',
'status' => Response::HTTP_INTERNAL_SERVER_ERROR
], Response::HTTP_INTERNAL_SERVER_ERROR);
'status_code' => 422,
'message' => 'Validation failed',
'data' => $e->errors()
], 422);

} catch (Exception $e) {
return response()->json([
'status_code' => 500,
'message' => 'An error occurred while updating the FAQ',
'data' => []
], 500);
}
}

/**
* Remove the specified resource from storage.
*/
public function destroy($faq)
public function destroy($id)
{
$faq = Faq::find($faq);

if (!$faq) {
try {
$faq = Faq::findOrFail($id);
$faq->delete();

return response()->json([
'status_code' => 200,
'message' => 'FAQ successfully deleted'
], 200);

} catch (Exception $e) {
return response()->json([
'code' => 400,
'description' => 'Bad Request.',
'links' => []
], 400);
'status_code' => 500,
'message' => 'An error occurred while deleting the FAQ',
'data' => []
], 500);
}

$faq->delete();

return response()->json([
'code' => 200,
'description' => 'The FAQ has been successfully deleted.',
'links' => []
], 200);
}

}
6 changes: 0 additions & 6 deletions app/Http/Controllers/Api/V1/Admin/SendEmailController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,4 @@ public function createEmailRequest(Request $request)

return response()->json(['message' => 'Email request is queued.']);
}
public function triggerEmailSending(Request $request)
{
SendEmailRequestsJob::dispatch();

return response()->json(['message' => 'Email requests are being processed.']);
}
}
Loading

0 comments on commit 44f812f

Please sign in to comment.