ldc-crm/app/Http/Controllers/Bo/BrandController.php

156 lines
4.8 KiB
PHP
Raw Normal View History

2020-12-01 17:02:10 +01:00
<?php
2023-10-06 15:00:42 +02:00
namespace App\Http\Controllers\Bo;
2020-12-01 17:02:10 +01:00
use App\Http\Controllers\Controller;
2023-10-06 15:00:42 +02:00
use App\Http\Requests\Bo\Brands\StoreBrandRequest;
use App\Http\Requests\Bo\Brands\UpdateBrandRequest;
2025-08-04 14:50:03 +02:00
use App\Http\Requests\Bo\PaginateModelsRequest;
2020-12-01 17:02:10 +01:00
use App\Models\Brand;
2023-06-23 13:29:03 +02:00
use Illuminate\Database\Eloquent\Builder;
2023-06-13 09:11:18 +02:00
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
2025-08-04 14:50:03 +02:00
use Uwil\LaravelCommon\Helpers\CollectionHelper;
2020-12-01 17:02:10 +01:00
class BrandController extends Controller
{
/**
* Create the controller instance.
*/
public function __construct()
{
$this->authorizeResource(Brand::class);
}
2023-06-13 09:11:18 +02:00
/**
* Display a listing of the resource.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Contracts\View\View
2023-06-13 09:11:18 +02:00
*/
public function index(Request $request): \Illuminate\Contracts\View\View
2023-06-13 09:11:18 +02:00
{
2023-06-15 13:18:48 +02:00
/** @var \Illuminate\Database\Eloquent\Builder $query */
$query = Brand::query();
2023-06-13 09:11:18 +02:00
if ($request->search) {
$this->searchQuery(
2023-06-15 13:18:48 +02:00
$query,
2023-06-13 09:11:18 +02:00
$request->search,
null,
'name',
);
}
2023-06-15 13:18:48 +02:00
$this->sortQuery($query);
2023-06-13 09:11:18 +02:00
$searchFields = trans('Nom');
2023-06-23 13:29:03 +02:00
$query->withCount(['customerInfos', 'customerInfos as customer_infos_optin_count' => function (Builder $q) {
$q->where('opt_in', true);
}]);
2023-10-06 15:00:42 +02:00
$brandModels = $this->paginate($query);
2023-06-13 09:11:18 +02:00
2023-10-06 15:00:42 +02:00
return view('bo.pages.brands.index', compact('brandModels', 'searchFields'));
2020-12-01 17:02:10 +01:00
}
2023-06-13 09:11:18 +02:00
/**
* Show the form for creating a new resource.
*
* @param \App\Models\Brand $brands
* @return \Illuminate\Contracts\View\View
2023-06-13 09:11:18 +02:00
*/
public function create(Brand $brands): \Illuminate\Contracts\View\View
2023-06-13 09:11:18 +02:00
{
2023-10-06 15:00:42 +02:00
return view('bo.pages.brands.create', [
2023-06-13 09:11:18 +02:00
'brandModel' => $brands
]);
}
Version 3.0.0 This removes api authentification with token in favor of signature method. Refactorize code and clean up. It add more unit test plus code coverage in test/converage. - Added config.api.timestampTimeout - Added Helper::signRequest This calculates the request signature - Added more unit tests and extend code coverage. - Added rate limiter for web routes - Added RemoveSpaces Sanitizer on api requests - Added stress test suite - Changed AuthServiceProvider api-token to api-hmac - Changed CustomerSeeder to have at least of customer activated (for unit tests) - Changed used transation for saveing multiple models - Changed, in ServiceController generateApiToken to generateServicePassword - Changed, timestamp has to be send in request with a DateTime format, it also has to a UTC value - Composer added brianium/paratest - Composer update - Fixed case where removing single optin could be the wrong one (not for the good service) - Fixed customer delete - Fixed customer would not be resolved by brand. - Fixed Really check that request is json checking body content, preventing sending malformed requests - Remove ApiError::needAuth - Removed api_token - Removed ApiController::authenticate - Removed config.api.tokenTimeout - Removed Header check or usage because of HAProxy doing weird stuff - Removed lock files from gitignore - Removed memcached dependency - Removed unused questions resources api routes - Removed workspace file Migrations - Added unique index on services.bearer_token - Changed customer_id on customer_answer is nullable - Changed services.bearer_pwd length to 312 (the size of the token) - Dropped services.api_token and service.api_token_date
2021-12-19 15:57:24 +01:00
/**
2023-06-13 09:11:18 +02:00
* Store a newly created resource in storage.
Version 3.0.0 This removes api authentification with token in favor of signature method. Refactorize code and clean up. It add more unit test plus code coverage in test/converage. - Added config.api.timestampTimeout - Added Helper::signRequest This calculates the request signature - Added more unit tests and extend code coverage. - Added rate limiter for web routes - Added RemoveSpaces Sanitizer on api requests - Added stress test suite - Changed AuthServiceProvider api-token to api-hmac - Changed CustomerSeeder to have at least of customer activated (for unit tests) - Changed used transation for saveing multiple models - Changed, in ServiceController generateApiToken to generateServicePassword - Changed, timestamp has to be send in request with a DateTime format, it also has to a UTC value - Composer added brianium/paratest - Composer update - Fixed case where removing single optin could be the wrong one (not for the good service) - Fixed customer delete - Fixed customer would not be resolved by brand. - Fixed Really check that request is json checking body content, preventing sending malformed requests - Remove ApiError::needAuth - Removed api_token - Removed ApiController::authenticate - Removed config.api.tokenTimeout - Removed Header check or usage because of HAProxy doing weird stuff - Removed lock files from gitignore - Removed memcached dependency - Removed unused questions resources api routes - Removed workspace file Migrations - Added unique index on services.bearer_token - Changed customer_id on customer_answer is nullable - Changed services.bearer_pwd length to 312 (the size of the token) - Dropped services.api_token and service.api_token_date
2021-12-19 15:57:24 +01:00
*
2023-10-06 15:00:42 +02:00
* @param \App\Http\Requests\Brands\Bo\StoreBrandRequest $request
* @return \Illuminate\Http\RedirectResponse
Version 3.0.0 This removes api authentification with token in favor of signature method. Refactorize code and clean up. It add more unit test plus code coverage in test/converage. - Added config.api.timestampTimeout - Added Helper::signRequest This calculates the request signature - Added more unit tests and extend code coverage. - Added rate limiter for web routes - Added RemoveSpaces Sanitizer on api requests - Added stress test suite - Changed AuthServiceProvider api-token to api-hmac - Changed CustomerSeeder to have at least of customer activated (for unit tests) - Changed used transation for saveing multiple models - Changed, in ServiceController generateApiToken to generateServicePassword - Changed, timestamp has to be send in request with a DateTime format, it also has to a UTC value - Composer added brianium/paratest - Composer update - Fixed case where removing single optin could be the wrong one (not for the good service) - Fixed customer delete - Fixed customer would not be resolved by brand. - Fixed Really check that request is json checking body content, preventing sending malformed requests - Remove ApiError::needAuth - Removed api_token - Removed ApiController::authenticate - Removed config.api.tokenTimeout - Removed Header check or usage because of HAProxy doing weird stuff - Removed lock files from gitignore - Removed memcached dependency - Removed unused questions resources api routes - Removed workspace file Migrations - Added unique index on services.bearer_token - Changed customer_id on customer_answer is nullable - Changed services.bearer_pwd length to 312 (the size of the token) - Dropped services.api_token and service.api_token_date
2021-12-19 15:57:24 +01:00
*/
public function store(StoreBrandRequest $request): \Illuminate\Http\RedirectResponse
2023-06-13 09:11:18 +02:00
{
return DB::transaction(function () use ($request) {
$brand = new Brand();
$brand->fill($request->validated());
if ($brand->saveOrFail()) {
return redirect()
2023-10-06 15:00:42 +02:00
->route('bo.brands.edit', $brand)
2023-06-13 09:11:18 +02:00
->with('success', trans(
2023-06-15 12:24:40 +02:00
':model a été créé.',
2023-06-13 09:11:18 +02:00
['model' => Str::singular(trans('models.classes.brands'))]
));
}
return redirect()
->back()
->with('error', trans(
2023-06-15 12:24:40 +02:00
':model n\'a pas été créé.',
2023-06-13 09:11:18 +02:00
['model' => Str::singular(trans('models.classes.brands'))]
));
});
}
Version 3.0.0 This removes api authentification with token in favor of signature method. Refactorize code and clean up. It add more unit test plus code coverage in test/converage. - Added config.api.timestampTimeout - Added Helper::signRequest This calculates the request signature - Added more unit tests and extend code coverage. - Added rate limiter for web routes - Added RemoveSpaces Sanitizer on api requests - Added stress test suite - Changed AuthServiceProvider api-token to api-hmac - Changed CustomerSeeder to have at least of customer activated (for unit tests) - Changed used transation for saveing multiple models - Changed, in ServiceController generateApiToken to generateServicePassword - Changed, timestamp has to be send in request with a DateTime format, it also has to a UTC value - Composer added brianium/paratest - Composer update - Fixed case where removing single optin could be the wrong one (not for the good service) - Fixed customer delete - Fixed customer would not be resolved by brand. - Fixed Really check that request is json checking body content, preventing sending malformed requests - Remove ApiError::needAuth - Removed api_token - Removed ApiController::authenticate - Removed config.api.tokenTimeout - Removed Header check or usage because of HAProxy doing weird stuff - Removed lock files from gitignore - Removed memcached dependency - Removed unused questions resources api routes - Removed workspace file Migrations - Added unique index on services.bearer_token - Changed customer_id on customer_answer is nullable - Changed services.bearer_pwd length to 312 (the size of the token) - Dropped services.api_token and service.api_token_date
2021-12-19 15:57:24 +01:00
/**
2023-06-13 09:11:18 +02:00
* Show the form for editing the specified resource.
Version 3.0.0 This removes api authentification with token in favor of signature method. Refactorize code and clean up. It add more unit test plus code coverage in test/converage. - Added config.api.timestampTimeout - Added Helper::signRequest This calculates the request signature - Added more unit tests and extend code coverage. - Added rate limiter for web routes - Added RemoveSpaces Sanitizer on api requests - Added stress test suite - Changed AuthServiceProvider api-token to api-hmac - Changed CustomerSeeder to have at least of customer activated (for unit tests) - Changed used transation for saveing multiple models - Changed, in ServiceController generateApiToken to generateServicePassword - Changed, timestamp has to be send in request with a DateTime format, it also has to a UTC value - Composer added brianium/paratest - Composer update - Fixed case where removing single optin could be the wrong one (not for the good service) - Fixed customer delete - Fixed customer would not be resolved by brand. - Fixed Really check that request is json checking body content, preventing sending malformed requests - Remove ApiError::needAuth - Removed api_token - Removed ApiController::authenticate - Removed config.api.tokenTimeout - Removed Header check or usage because of HAProxy doing weird stuff - Removed lock files from gitignore - Removed memcached dependency - Removed unused questions resources api routes - Removed workspace file Migrations - Added unique index on services.bearer_token - Changed customer_id on customer_answer is nullable - Changed services.bearer_pwd length to 312 (the size of the token) - Dropped services.api_token and service.api_token_date
2021-12-19 15:57:24 +01:00
*
2023-06-13 09:11:18 +02:00
* @param \App\Models\Brand $brands
* @return \Illuminate\Contracts\View\View
Version 3.0.0 This removes api authentification with token in favor of signature method. Refactorize code and clean up. It add more unit test plus code coverage in test/converage. - Added config.api.timestampTimeout - Added Helper::signRequest This calculates the request signature - Added more unit tests and extend code coverage. - Added rate limiter for web routes - Added RemoveSpaces Sanitizer on api requests - Added stress test suite - Changed AuthServiceProvider api-token to api-hmac - Changed CustomerSeeder to have at least of customer activated (for unit tests) - Changed used transation for saveing multiple models - Changed, in ServiceController generateApiToken to generateServicePassword - Changed, timestamp has to be send in request with a DateTime format, it also has to a UTC value - Composer added brianium/paratest - Composer update - Fixed case where removing single optin could be the wrong one (not for the good service) - Fixed customer delete - Fixed customer would not be resolved by brand. - Fixed Really check that request is json checking body content, preventing sending malformed requests - Remove ApiError::needAuth - Removed api_token - Removed ApiController::authenticate - Removed config.api.tokenTimeout - Removed Header check or usage because of HAProxy doing weird stuff - Removed lock files from gitignore - Removed memcached dependency - Removed unused questions resources api routes - Removed workspace file Migrations - Added unique index on services.bearer_token - Changed customer_id on customer_answer is nullable - Changed services.bearer_pwd length to 312 (the size of the token) - Dropped services.api_token and service.api_token_date
2021-12-19 15:57:24 +01:00
*/
public function edit(Brand $brands): \Illuminate\Contracts\View\View
{
2023-10-06 15:00:42 +02:00
return view('bo.pages.brands.edit', [
2023-06-13 09:11:18 +02:00
'brandModel' => $brands
]);
}
/**
* Update a spacific resource in storage.
*
2023-10-06 15:00:42 +02:00
* @param \App\Http\Requests\Brands\Bo\UpdateBrandRequest $request
* @param \App\Models\Brand $brand
* @return \Illuminate\Http\RedirectResponse
2023-06-13 09:11:18 +02:00
*/
public function update(UpdateBrandRequest $request, Brand $brand): \Illuminate\Http\RedirectResponse
2023-06-13 09:11:18 +02:00
{
2023-06-15 12:24:40 +02:00
return DB::transaction(function () use ($request, $brand) {
2023-06-13 09:11:18 +02:00
$brand->fill($request->validated());
if ($brand->saveOrFail()) {
return redirect()
2023-10-06 15:00:42 +02:00
->route('bo.brands.edit', $brand)
2023-06-13 09:11:18 +02:00
->with('success', trans(
2023-06-15 12:24:40 +02:00
':model a été mis à jour.',
2023-06-13 09:11:18 +02:00
['model' => Str::singular(trans('models.classes.brands'))]
));
}
return redirect()
->back()
->with('error', trans(
2023-06-15 12:24:40 +02:00
':model n\'a pas été mis à jour.',
2023-06-13 09:11:18 +02:00
['model' => Str::singular(trans('models.classes.brands'))]
));
});
2020-12-01 17:02:10 +01:00
}
2025-08-04 14:50:03 +02:00
/**
* Get a paginate resource.
*
* @param \App\Http\Requests\Bo\PaginateModelsRequest $request
* @return \Illuminate\Http\JsonResponse
*/
public function jsonPaginateModels(PaginateModelsRequest $request): \Illuminate\Http\JsonResponse
{
$models = CollectionHelper::paginateManually(
Brand::getUserAssociatedBrands(),
$this->jsonItemPerPagination,
2025-08-28 11:07:31 +02:00
(int) $request->validated('page'),
2025-08-04 14:50:03 +02:00
);
return \response()->json($models);
}
2020-12-01 17:02:10 +01:00
}