-
Notifications
You must be signed in to change notification settings - Fork 0
#6 - listing image uploads backend #45
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
693bba1
5 - Report Listing
DawBaz15 f9f4403
assing role on user register
KacperWalenga d6405ce
add images to auctions
KacperWalenga 431f486
apply suggestions from copilot c:
KacperWalenga 20a3b45
Merge remote-tracking branch 'origin/main' into #6-Listing-Image-Uplo…
KacperWalenga File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,18 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Otoszroto\Actions\Auction; | ||
|
|
||
| use Illuminate\Http\UploadedFile; | ||
| use Otoszroto\Models\Auction; | ||
|
|
||
| class AddImageToAuctionAction | ||
| { | ||
| public function execute(Auction $auction, UploadedFile $uploadedFile): bool | ||
| { | ||
| $stored = $uploadedFile->storeAs("", $auction->id . ".png", "auctionImage"); | ||
|
|
||
| return $stored !== false; | ||
| } | ||
| } |
This file contains hidden or 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,22 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Otoszroto\Actions\Auction; | ||
|
|
||
| use Otoszroto\Models\Auction; | ||
| use Otoszroto\Models\Report; | ||
| use Otoszroto\Models\User; | ||
|
|
||
| class CreateReportAction | ||
| { | ||
| public function execute(User $user, Auction $auction, array $reportData): Report | ||
| { | ||
| $report = new Report($reportData); | ||
| $report->reporter_id = $user->id; | ||
| $report->auction_id = $auction->id; | ||
| $report->save(); | ||
|
|
||
| return $report; | ||
| } | ||
| } |
This file contains hidden or 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,21 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Otoszroto\Actions\Auction; | ||
|
|
||
| use Illuminate\Support\Facades\Storage; | ||
|
|
||
| class GetAuctionImageAction | ||
| { | ||
| public function execute(int $auctionId): ?string | ||
| { | ||
| $filename = $auctionId . ".png"; | ||
|
|
||
| if (Storage::disk("auctionImage")->exists($filename)) { | ||
| return Storage::disk("auctionImage")->get($filename); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } |
This file contains hidden or 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,21 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Otoszroto\Actions\Auction; | ||
|
|
||
| use Illuminate\Support\Facades\Storage; | ||
|
|
||
| class GetDefaultAuctionImageAction | ||
| { | ||
| public function execute(): ?string | ||
| { | ||
| $filename = "default.png"; | ||
|
|
||
| if (Storage::disk("auctionImage")->exists($filename)) { | ||
| return Storage::disk("auctionImage")->get($filename); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } |
This file contains hidden or 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,39 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Otoszroto\Helpers; | ||
|
|
||
| use Identicon\Identicon; | ||
| use Illuminate\Support\Facades\Storage; | ||
|
|
||
| class IdenticonHelper | ||
| { | ||
| private Identicon $identicon; | ||
|
|
||
| public function __construct() | ||
| { | ||
| $this->identicon = new Identicon(); | ||
| } | ||
|
|
||
| public static function url(string|int $name): string | ||
| { | ||
| return url("/auctions/{$name}/image"); | ||
| } | ||
|
|
||
| public function create(string|int $filename, string $data): string | ||
| { | ||
| $image = $this->identicon->getImageData($data, 300); | ||
|
|
||
| return $this->save($filename, $image); | ||
| } | ||
|
|
||
| private function save(string|int $name, string $imageData): string | ||
| { | ||
| $path = $name . ".png"; | ||
|
|
||
| Storage::disk("avatars")->put($path, $imageData); | ||
|
|
||
| return Storage::disk("avatars")->url($path); | ||
| } | ||
KacperWalenga marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -9,9 +9,12 @@ | |
| use Illuminate\Http\Request; | ||
| use Inertia\Inertia; | ||
| use Inertia\Response; | ||
| use Otoszroto\Actions\Auction\AddImageToAuctionAction; | ||
| use Otoszroto\Actions\Auction\CancelAuctionAction; | ||
| use Otoszroto\Actions\Auction\CreateAuctionAction; | ||
| use Otoszroto\Actions\Auction\FinishAuctionAction; | ||
| use Otoszroto\Actions\Auction\GetAuctionImageAction; | ||
| use Otoszroto\Actions\Auction\GetDefaultAuctionImageAction; | ||
| use Otoszroto\Actions\Auction\UpdateAuctionAction; | ||
| use Otoszroto\Enums\AuctionState; | ||
| use Otoszroto\Helpers\SortHelper; | ||
|
|
@@ -41,11 +44,17 @@ public function edit(Auction $auction): Response | |
| return Inertia::render("Auction/EditAuction", ["auction" => $auction]); | ||
| } | ||
|
|
||
| public function store(CreateAuctionRequest $request, CreateAuctionAction $createAuctionAction): RedirectResponse | ||
| public function store(CreateAuctionRequest $request, CreateAuctionAction $createAuctionAction, AddImageToAuctionAction $addImageToAuctionAction): RedirectResponse | ||
| { | ||
| $user = $request->user(); | ||
| $validated = $request->validated(); | ||
| $createAuctionAction->execute($user, $validated); | ||
| $auction = $createAuctionAction->execute($user, $validated); | ||
|
||
|
|
||
| $photo = $validated["photo"]; | ||
|
|
||
| if ($photo) { | ||
| $addImageToAuctionAction->execute($auction, $photo); | ||
| } | ||
|
|
||
KacperWalenga marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return redirect()->route("auctions.create")->with(["message" => "Aukcja została utworzona."]); | ||
| } | ||
|
|
@@ -110,6 +119,16 @@ public function index(SortHelper $sorter, Request $request): Response | |
| ]); | ||
| } | ||
|
|
||
| public function getImage(int $id, GetAuctionImageAction $getAuctionImageAction, GetDefaultAuctionImageAction $getDefaultAuctionImageAction): ?\Illuminate\Http\Response | ||
KacperWalenga marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| $image = $getAuctionImageAction->execute($id); | ||
| $default = $getDefaultAuctionImageAction->execute(); | ||
|
|
||
| return response($image ?? $default) | ||
KacperWalenga marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ->header("Content-Type", "image/png") | ||
| ->header("Cache-Control", "max-age=31536000, public"); | ||
| } | ||
|
|
||
| private function filterCategory(Builder $query, Request $request): Builder | ||
| { | ||
| $category_id = $request->query("category", null); | ||
|
|
||
This file contains hidden or 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,56 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Otoszroto\Http\Controllers\Auction; | ||
|
|
||
| use Illuminate\Http\RedirectResponse; | ||
| use Inertia\Inertia; | ||
| use Inertia\Response; | ||
| use Otoszroto\Actions\Auction\CreateReportAction; | ||
| use Otoszroto\Helpers\SortHelper; | ||
| use Otoszroto\Http\Controllers\Controller; | ||
| use Otoszroto\Http\Requests\Auction\CreateReportRequest; | ||
| use Otoszroto\Http\Resources\ReportResource; | ||
| use Otoszroto\Models\Auction; | ||
| use Otoszroto\Models\Report; | ||
|
|
||
| class ReportController extends Controller | ||
| { | ||
| public function create(Auction $auction): Response | ||
| { | ||
| return Inertia::render("Auction/ReportAuction", ["auction" => $auction]); | ||
| } | ||
|
|
||
| public function store(CreateReportRequest $request, CreateReportAction $createReportAction, Auction $auction): RedirectResponse | ||
| { | ||
| $user = $request->user(); | ||
| $validated = $request->validated(); | ||
| $createReportAction->execute($user, $auction, $validated); | ||
|
|
||
| return redirect()->route("auctions.show", ["auction" => $auction])->with(["message" => "Aukcja została zgłoszona."]); | ||
KacperWalenga marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| public function index(SortHelper $sorter): Response | ||
| { | ||
| $reports = Report::query()->with(["reporter", "auction"])->where("resolved_at", "=", null); | ||
|
|
||
| $perPage = (int)request()->query("per_page", 10); | ||
|
|
||
| $query = $sorter->sort($reports, ["id", "created_at"], []); | ||
| $query = $sorter->search($query, "id"); | ||
|
|
||
| return Inertia::render("Auction/Reports", [ | ||
| "reports" => ReportResource::collection($query->paginate($perPage)), | ||
| ]); | ||
| } | ||
|
|
||
| public function show(Report $report): Response | ||
| { | ||
| $report->load(["reporter", "auction"]); | ||
|
|
||
| return Inertia::render("Auction/ShowReport", [ | ||
| "report" => new ReportResource($report), | ||
| ]); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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,26 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Otoszroto\Http\Requests\Auction; | ||
|
|
||
| use Illuminate\Contracts\Validation\ValidationRule; | ||
| use Illuminate\Foundation\Http\FormRequest; | ||
|
|
||
| class CreateReportRequest extends FormRequest | ||
| { | ||
| public function authorize(): bool | ||
| { | ||
| return auth()->check(); | ||
| } | ||
|
|
||
| /** | ||
| * @return array<string, ValidationRule|array<mixed>|string> | ||
| */ | ||
| public function rules(): array | ||
| { | ||
| return [ | ||
| "reason" => ["sometimes", "string", "max:255"], | ||
| ]; | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,27 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Otoszroto\Http\Resources; | ||
|
|
||
| use Illuminate\Http\Request; | ||
| use Illuminate\Http\Resources\Json\JsonResource; | ||
|
|
||
| class ReportResource extends JsonResource | ||
| { | ||
| /** | ||
| * @return array<string, mixed> | ||
| */ | ||
| public function toArray(Request $request): array | ||
| { | ||
| return [ | ||
| "id" => $this->id, | ||
| "reporter" => UserResource::make($this->whenLoaded("reporter")), | ||
| "auction" => AuctionResource::make($this->whenLoaded("auction")), | ||
| "reason" => $this->reason, | ||
| "resolvedAt" => optional($this->resolved_at)?->toDateTimeString(), | ||
| "createdAt" => $this->created_at, | ||
| "updatedAt" => $this->updated_at, | ||
| ]; | ||
| } | ||
| } |
This file contains hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The disk configuration references a disk named "auctionImage" in the filesystem config, but the IdenticonHelper class incorrectly uses the disk name "avatars". This will cause a runtime error when trying to save identicon images.