-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into #90-send-e-mail-when-provider-is-added-torem…
…oved-from-favorite-city
- Loading branch information
Showing
60 changed files
with
1,421 additions
and
510 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Http\Controllers\Admin; | ||
|
||
use App\Http\Controllers\Controller; | ||
use App\Http\Requests\ProviderRequest; | ||
use App\Http\Resources\ProviderResource; | ||
use App\Models\Provider; | ||
use Illuminate\Http\Response as InertiaResponse; | ||
use Illuminate\Support\Facades\File; | ||
use Illuminate\Support\Facades\Storage; | ||
use Inertia\Inertia; | ||
use Inertia\Response; | ||
|
||
class ProviderController extends Controller | ||
{ | ||
public const ITEMS_PER_PAGE = 15; | ||
|
||
public function index(): Response | ||
{ | ||
$providers = Provider::query() | ||
->search("name") | ||
->orderByName() | ||
->orderByTimeRange() | ||
->paginate(self::ITEMS_PER_PAGE) | ||
->withQueryString(); | ||
|
||
return Inertia::render("Providers/Index", [ | ||
"providers" => ProviderResource::collection($providers), | ||
]); | ||
} | ||
|
||
public function store(ProviderRequest $request): void | ||
{ | ||
Provider::query()->create($request->validated()); | ||
|
||
$fileName = $this->getFilename($request); | ||
$fileContents = $request->file("file")->get(); | ||
|
||
Storage::disk("public")->put("providers/" . $fileName, $fileContents); | ||
} | ||
|
||
public function update(ProviderRequest $request, Provider $provider): void | ||
{ | ||
$provider->update($request->validated()); | ||
|
||
$imageName = $this->getFilename($request); | ||
$storageImagePath = storage_path("app/public/providers/" . $imageName); | ||
$resourceImagePath = resource_path("providers/" . $imageName); | ||
$imageContents = $request->file("file")->get(); | ||
|
||
if (file_exists($resourceImagePath)) { | ||
file_put_contents($resourceImagePath, $imageContents); | ||
Storage::put($storageImagePath, file_get_contents($imageContents)); | ||
} else { | ||
Storage::put($storageImagePath, file_get_contents($imageContents)); | ||
} | ||
} | ||
|
||
public function destroy(Provider $provider): void | ||
{ | ||
$provider->delete(); | ||
$imagePath = storage_path("app/public/providers/" . strtolower($provider["name"]) . ".png"); | ||
File::delete($imagePath); | ||
} | ||
|
||
public function showLogo(string $filename): InertiaResponse | ||
{ | ||
$imagePath = storage_path("app/public/providers/" . $filename); | ||
|
||
if (!file_exists($imagePath)) { | ||
$imagePath = storage_path("app/public/providers/unknown.png"); | ||
} | ||
|
||
return response(file_get_contents($imagePath), 200, ["Content-Type" => "image/png"]); | ||
} | ||
|
||
public function getFilename(ProviderRequest $request): string | ||
{ | ||
return strtolower($request["name"]) . "." . $request->file("file")->getClientOriginalExtension(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Http\Requests; | ||
|
||
use Illuminate\Foundation\Http\FormRequest; | ||
use Illuminate\Validation\Rule; | ||
use Illuminate\Validation\Rules\File; | ||
use Illuminate\Validation\Rules\Unique; | ||
|
||
class ProviderRequest extends FormRequest | ||
{ | ||
public function rules(): array | ||
{ | ||
return [ | ||
"name" => ["required", "string", "regex:/^[A-Z\s]/", "max:100", $this->uniqueRuleForProvider("name")], | ||
"color" => ["required", "string", "size:7"], | ||
"file" => [ | ||
"required", | ||
"mimes:png", | ||
File::image() | ||
->max(config("app.provider_logo_size")) | ||
->dimensions(Rule::dimensions()->width(config("app.provider_logo_width"))->height(config("app.provider_logo_height"))), | ||
], | ||
]; | ||
} | ||
|
||
protected function uniqueRuleForProvider(string $column): Unique | ||
{ | ||
$currentProviderId = $this->route(param: "provider"); | ||
|
||
return Rule::unique(table: "providers", column: $column)->ignore($currentProviderId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.