Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#6 - CRUD for providers #139

Merged
merged 52 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
a56d226
add Link provider
AleksandraKozubal Aug 11, 2023
23ccfd6
add BinBin provider
AleksandraKozubal Aug 14, 2023
345c12d
Merge branch 'main' into #6-CRUD-for-providers
AleksandraKozubal Aug 21, 2023
88bf19e
Merge branch 'main' into #6-CRUD-for-providers
AleksandraKozubal Aug 21, 2023
5b2c69e
show providers name, url, color
AleksandraKozubal Aug 22, 2023
4f74f8a
show provider logo
AleksandraKozubal Aug 22, 2023
dd6aa3e
nicer looking logo
AleksandraKozubal Aug 22, 2023
75dd50d
delete zinc/black borders around logos
AleksandraKozubal Aug 22, 2023
1b08a7d
Merge branch 'main' into #6-CRUD-for-providers
AleksandraKozubal Aug 22, 2023
c869e8d
Merge branch 'main' into #6-CRUD-for-providers
AleksandraKozubal Aug 22, 2023
a2a80e6
delete unused elements
AleksandraKozubal Aug 22, 2023
5bee4e6
fix edit and delete provider and a few minor changes
AleksandraKozubal Aug 23, 2023
b461229
upload image
AleksandraKozubal Aug 24, 2023
b82b1d7
fix sorting in cities and providers
vojcc Aug 24, 2023
8e03f6d
Merge remote-tracking branch 'origin/#6-CRUD-for-providers' into #6-C…
vojcc Aug 24, 2023
e93b964
another fix
vojcc Aug 24, 2023
fa35e58
Merge branch 'main' into #6-CRUD-for-providers
AleksandraKozubal Aug 25, 2023
0fdf813
fix color formatting
AleksandraKozubal Aug 25, 2023
60badcd
add and validate image
AleksandraKozubal Aug 28, 2023
f20893b
fix validate image
AleksandraKozubal Aug 29, 2023
2442fc2
fix validate image
AleksandraKozubal Aug 29, 2023
69de5df
Merge branch 'main' into #6-CRUD-for-providers
AleksandraKozubal Aug 29, 2023
66d5579
delete image when provider deleted
AleksandraKozubal Aug 29, 2023
027f3f3
update image
AleksandraKozubal Aug 29, 2023
d62fd3c
name formatting
AleksandraKozubal Aug 29, 2023
0c96bd7
refactor img upload
AleksandraKozubal Aug 29, 2023
6496d20
img upload fix
AleksandraKozubal Aug 29, 2023
8a851a6
lang, upload photo
AleksandraKozubal Aug 30, 2023
5fda1f1
upload photo fix
AleksandraKozubal Aug 31, 2023
2828943
resize existing providers' logos
AleksandraKozubal Aug 31, 2023
04488f8
add veo
AleksandraKozubal Aug 31, 2023
312f04d
add unknown.png when image is missing
AleksandraKozubal Aug 31, 2023
60df3f5
move ImageController, delete zinc frames around logos
AleksandraKozubal Aug 31, 2023
b7a7c99
update image fix, lang
AleksandraKozubal Sep 1, 2023
ae5c683
small changes
AleksandraKozubal Sep 1, 2023
3251a4e
fix conflicts
AleksandraKozubal Nov 9, 2023
51b51f9
Neuron logo
AleksandraKozubal Nov 10, 2023
5002bd8
Merge branch 'main' into #6-CRUD-for-providers
AleksandraKozubal Jan 6, 2024
8b1d015
Merge branch 'main' into #6-CRUD-for-providers
AleksandraKozubal Jan 11, 2024
0b2b209
delete create and edit option
AleksandraKozubal Jan 11, 2024
4e9173a
fix Search and logos
AleksandraKozubal Jan 12, 2024
c0007c6
fix lint
AleksandraKozubal Jan 12, 2024
b50810e
fix lint
AleksandraKozubal Jan 12, 2024
da9550b
remove hover, add delete translation
AleksandraKozubal Jan 12, 2024
419198e
Merge branch 'main' into #6-CRUD-for-providers
AleksandraKozubal Feb 1, 2024
f2be964
remove statistics, remove hover on url and disabled buttons
AleksandraKozubal Feb 1, 2024
ad5711e
lint
AleksandraKozubal Feb 1, 2024
3487ca6
change typing and import
AleksandraKozubal Feb 1, 2024
7b2282c
fix
AleksandraKozubal Feb 1, 2024
4b9dee1
lint
AleksandraKozubal Feb 1, 2024
ddad187
add const
AleksandraKozubal Feb 1, 2024
858308c
cr fixes
AleksandraKozubal Mar 7, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions app/Http/Controllers/Admin/ProviderController.php
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();
}
}
35 changes: 35 additions & 0 deletions app/Http/Requests/ProviderRequest.php
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);
}
}
1 change: 0 additions & 1 deletion app/Importers/BinBinDataImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public function transform(): void
$existingCityProviders[] = $provider;
}
}

$this->deleteMissingProviders(self::getProviderName(), $existingCityProviders);
}
}
16 changes: 14 additions & 2 deletions app/Models/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace App\Models;

use App\QueryBuilders\SortQuery;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

/**
Expand All @@ -16,9 +18,19 @@ class Provider extends Model
public $incrementing = false;
protected $primaryKey = "name";
protected $keyType = "string";
protected $fillable = [
"name",
"url",
"color",
];

public function cityProvider()
public static function query(): Builder
{
return $this->belongsTo(CityProvider::class);
return parent::query();
}
kamilpiech97 marked this conversation as resolved.
Show resolved Hide resolved

public function newEloquentBuilder($query): SortQuery
{
return new SortQuery($query);
}
}
Loading
Loading