Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into LP-103-Login-Test-Case
Browse files Browse the repository at this point in the history
# Conflicts:
#	tests/TestCase.php
  • Loading branch information
hafijul233 committed Oct 14, 2023
2 parents de09eb2 + 34e9e8a commit b558ec2
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 0 deletions.
2 changes: 2 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,7 @@

// Route::apiResource('teams', \Fintech\Auth\Http\Controllers\TeamController::class);
// Route::post('teams/{team}/restore', [\Fintech\Auth\Http\Controllers\TeamController::class, 'restore'])->name('teams.restore');

Route::apiResource('settings', \Fintech\Auth\Http\Controllers\SettingController::class)->only(['index', 'store']);
});
});
111 changes: 111 additions & 0 deletions src/Http/Controllers/SettingController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

namespace Fintech\Auth\Http\Controllers;

use Fintech\Auth\Http\Requests\StoreSettingRequest;
use Fintech\Auth\Http\Resources\SettingResource;
use Fintech\Core\Facades\Core;
use Fintech\Core\Supports\Utility;
use Fintech\Core\Traits\ApiResponseTrait;
use Illuminate\Http\JsonResponse;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Auth;

/**
* Class SettingController
* @package Fintech\Auth\Http\Controllers
*
* @lrd:start
* This class handle system setting related to individual user
* @lrd:end
*
*/
class SettingController extends Controller
{
use ApiResponseTrait;

/**
* @lrd:start
* Return a listing of the configurations in key and value format.
* *`configuration`* value depends on number of package configured to system
* @lrd:end
*
* @return SettingResource|JsonResponse
*/
public function index(): SettingResource|JsonResponse
{
try {

$configurations = (Auth::check())
? Core::setting()->list(['user_id' => auth()->id()])
: collect([]);

$settings = [];

$configurations->each(function ($setting) use (&$settings) {
$settings[$setting->package][$setting->key] = Utility::typeCast($setting->value, $setting->type);
});

return new SettingResource($settings);

} catch (\Exception $exception) {

return $this->failed($exception->getMessage());
}
}

/**
* @LRDparam package string|required|in:dashboard,other
* @lrd:start
* Update a specified user settings using configuration
* @lrd:end
*
* @param StoreSettingRequest $request
* @return JsonResponse
*/
public function store(StoreSettingRequest $request): JsonResponse
{
try {

$configuration = $request->input('package', 'dashboard');

$inputs = $request->except('package');

foreach ($inputs as $key => $value) {
Core::setting()->setValue($configuration, $key, $value, null, auth()->id());
}

return $this->updated(__('core::messages.setting.saved', ['package' => config("fintech.core.packages.{$configuration}", 'System')]));

} catch (\Exception $exception) {

return $this->failed($exception->getMessage());
}
}

/**
* @lrd:start
* Soft delete a specified setting resource using id.
* @lrd:end
*
* @param string $configuration
* @return JsonResponse
*/
public function destroy(string $configuration)
{
try {

$settings = Core::setting()->list(['package' => $configuration]);

foreach ($settings as $setting) {
Core::setting()->destroy($setting->id);
}

return $this->deleted(__('core::messages.setting.deleted', ['model' => 'Setting']));

} catch (\Exception $exception) {

return $this->failed($exception->getMessage());
}
}
}
28 changes: 28 additions & 0 deletions src/Http/Requests/StoreSettingRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Fintech\Auth\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class StoreSettingRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array|string>
*/
public function rules(): array
{
return [
'package' => ['required', 'string'],
];
}
}
36 changes: 36 additions & 0 deletions src/Http/Resources/SettingResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Fintech\Auth\Http\Resources;

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class SettingResource extends JsonResource
{
/**
* Transform the resource collection into an array.
*
* @param Request $request
* @return array
*/
public function toArray(Request $request)
{
return $this->resource;
}

/**
* Get additional data that should be returned with the resource array.
*
* @param Request $request
* @return array<string, mixed>
*/
public function with(Request $request): array
{
return [
'options' => [
'package' => ['dashboard' => 'Dashboard', 'other' => 'Other']
],
'query' => $request->all(),
];
}
}

0 comments on commit b558ec2

Please sign in to comment.