Последняя версия с сервера прошлого разработчика
This commit is contained in:
40
app/Domain/Images/Action/CreateImageAction.php
Executable file
40
app/Domain/Images/Action/CreateImageAction.php
Executable file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Images\Action;
|
||||
|
||||
use DB;
|
||||
use App\Domain\Feeds\Models\Feed;
|
||||
use App\Domain\Feeds\ToFeedAction;
|
||||
use App\Domain\Images\DataTransferObjects\ImageData;
|
||||
|
||||
class CreateImageAction implements ToFeedAction
|
||||
{
|
||||
public function __invoke(ImageData $imageData)
|
||||
{
|
||||
DB::beginTransaction();
|
||||
|
||||
$imageFeed = Feed::create([
|
||||
'title' => $imageData->title,
|
||||
'body' => $imageData->body,
|
||||
'price' => $imageData->price,
|
||||
'is_paid' => $imageData->is_paid,
|
||||
'user_id' => $imageData->user->id,
|
||||
'is_ads' => false,
|
||||
'type' => 'images',
|
||||
]);
|
||||
|
||||
foreach ($imageData->photos as $photo) {
|
||||
$imageFeed->addMedia($photo)->toMediaCollection('common');
|
||||
}
|
||||
|
||||
if($imageData->is_loaded_photos_paid){
|
||||
foreach ($imageData->photos_paid as $photo) {
|
||||
$imageFeed->addMedia($photo)->toMediaCollection('paid');
|
||||
}
|
||||
}
|
||||
|
||||
DB::commit();
|
||||
|
||||
return $imageFeed;
|
||||
}
|
||||
}
|
||||
68
app/Domain/Images/Action/UpdateImageAction.php
Executable file
68
app/Domain/Images/Action/UpdateImageAction.php
Executable file
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Images\Action;
|
||||
|
||||
use DB;
|
||||
use App\Domain\Feeds\Models\Feed;
|
||||
use App\Domain\Feeds\ToFeedAction;
|
||||
use App\Domain\Feeds\Enums\StatusEnum;
|
||||
use App\Domain\Images\DataTransferObjects\ImageData;
|
||||
use Spatie\MediaLibrary\MediaCollections\Models\Media;
|
||||
|
||||
class UpdateImageAction implements ToFeedAction
|
||||
{
|
||||
|
||||
public $imageFeed;
|
||||
|
||||
public function __construct(Feed $imageFeed)
|
||||
{
|
||||
$this->imageFeed = $imageFeed;
|
||||
}
|
||||
|
||||
public function __invoke(ImageData $imageData)
|
||||
{
|
||||
|
||||
$status = $this->imageFeed->status;
|
||||
if($status === StatusEnum::BANNED()){
|
||||
$status = StatusEnum::EDITABLE();
|
||||
}
|
||||
if($status === StatusEnum::APPROVED()){
|
||||
$status = StatusEnum::EDITABLE();
|
||||
}
|
||||
|
||||
DB::beginTransaction();
|
||||
|
||||
$this->imageFeed->fill([
|
||||
'title' => $imageData->title,
|
||||
'body' => $imageData->body,
|
||||
'price' => $imageData->price,
|
||||
'is_paid' => $imageData->is_paid,
|
||||
'status' => $status,
|
||||
'is_ads' => false,
|
||||
])->save();
|
||||
|
||||
if($imageData->is_loaded_photos){
|
||||
foreach ($imageData->photos as $photo) {
|
||||
$this->imageFeed->addMedia($photo)->toMediaCollection('common');
|
||||
}
|
||||
}
|
||||
|
||||
if($imageData->is_loaded_photos_paid){
|
||||
foreach ($imageData->photos_paid as $photo) {
|
||||
$this->imageFeed->addMedia($photo)->toMediaCollection('paid');
|
||||
}
|
||||
}
|
||||
|
||||
if(count($imageData->removedItems)){
|
||||
foreach ($imageData->removedItems as $removedItem) {
|
||||
if($media = Media::find($removedItem)){
|
||||
$media->delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
DB::commit();
|
||||
|
||||
return $this->imageFeed->refresh();
|
||||
}
|
||||
}
|
||||
41
app/Domain/Images/DataTransferObjects/ImageData.php
Executable file
41
app/Domain/Images/DataTransferObjects/ImageData.php
Executable file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Images\DataTransferObjects;
|
||||
|
||||
use App\Http\Requests\ImageFormRequest;
|
||||
use Spatie\DataTransferObject\DataTransferObject;
|
||||
|
||||
class ImageData extends DataTransferObject
|
||||
{
|
||||
public $title;
|
||||
public $body;
|
||||
public $user;
|
||||
public $photos;
|
||||
public $price;
|
||||
public $tags;
|
||||
public $is_paid;
|
||||
public $is_loaded_photos_paid;
|
||||
public $is_loaded_photos;
|
||||
public $photos_paid;
|
||||
public $removedItems;
|
||||
|
||||
public static function fromRequest(ImageFormRequest $request)
|
||||
{
|
||||
return new self([
|
||||
'title' => $request->input('title'),
|
||||
'body' => $request->input('body'),
|
||||
'price' => $request->input('price'),
|
||||
'is_paid' => $request->input('is_paid'),
|
||||
'user' => auth()->user(),
|
||||
'tags' => $request->input('tags') ?? [],
|
||||
|
||||
'photos' => $request->file('photos'),
|
||||
'is_loaded_photos' => $request->hasFile('photos'),
|
||||
|
||||
'photos_paid' => $request->file('photos_paid'),
|
||||
'is_loaded_photos_paid' => $request->hasFile('photos_paid'),
|
||||
|
||||
'removedItems' => $request->input('removedItems') ?? [],
|
||||
]);
|
||||
}
|
||||
}
|
||||
32
app/Domain/Images/Models/Image.php
Executable file
32
app/Domain/Images/Models/Image.php
Executable file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domain\Images\Models;
|
||||
|
||||
use App\Domain\Complaints\Models\Complaint;
|
||||
use App\Domain\Feeds\Models\Feed;
|
||||
use App\Models\Model;
|
||||
use App\Models\User;
|
||||
use Spatie\MediaLibrary\HasMedia;
|
||||
use Spatie\MediaLibrary\InteractsWithMedia;
|
||||
|
||||
class Image extends Model implements HasMedia
|
||||
{
|
||||
use InteractsWithMedia;
|
||||
|
||||
public function feed()
|
||||
{
|
||||
return $this->morphOne(Feed::class, 'feedable');
|
||||
}
|
||||
|
||||
public function complaints()
|
||||
{
|
||||
return $this->morphMany(Complaint::class, 'complaintable');
|
||||
}
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user