-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
640 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace App\DTO; | ||
|
||
use App\Rules\PhotosBelongToUser; | ||
use Spatie\LaravelData\Data; | ||
|
||
class BulkDeletePhotoItems extends Data | ||
{ | ||
/** | ||
* @param int[] $photo_ids | ||
* @param int[] $item_ids | ||
* @param int[] $tag_ids | ||
*/ | ||
public function __construct( | ||
public array $photo_ids, | ||
public array $item_ids = [], | ||
public array $tag_ids = [], | ||
) {} | ||
|
||
/** | ||
* @return array<string, mixed> | ||
*/ | ||
public static function rules(): array | ||
{ | ||
return [ | ||
'photo_ids' => [ | ||
'required', | ||
'array', | ||
new PhotosBelongToUser, | ||
], | ||
'photo_ids.*' => ['required', 'exists:photos,id'], | ||
'item_ids' => ['array'], | ||
'item_ids.*' => ['required', 'exists:items,id'], | ||
'tag_ids' => ['array'], | ||
'tag_ids.*' => ['required', 'exists:tags,id'], | ||
]; | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public static function messages(): array | ||
{ | ||
return [ | ||
'photo_ids.required' => 'You must select at least one photo.', | ||
'photo_ids.*.required' => 'You must select at least one photo.', | ||
'photo_ids.*.exists' => 'The selected photo #:position does not exist.', | ||
]; | ||
} | ||
} |
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,32 @@ | ||
<?php | ||
|
||
namespace App\Rules; | ||
|
||
use App\Models\Photo; | ||
use Closure; | ||
use Illuminate\Contracts\Validation\ValidationRule; | ||
use Illuminate\Translation\PotentiallyTranslatedString; | ||
|
||
class PhotosBelongToUser implements ValidationRule | ||
{ | ||
/** | ||
* Run the validation rule. | ||
* | ||
* @param Closure(string, ?string=): PotentiallyTranslatedString $fail | ||
*/ | ||
public function validate(string $attribute, mixed $value, Closure $fail): void | ||
{ | ||
if (! is_array($value)) { | ||
return; | ||
} | ||
|
||
$photosBelongsToOthers = Photo::query() | ||
->whereIn('id', $value) | ||
->where('user_id', '!=', auth()->id()) | ||
->exists(); | ||
|
||
if ($photosBelongsToOthers) { | ||
$fail('You are not the owner of the photos.'); | ||
} | ||
} | ||
} |
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.