121 lines
3.4 KiB
PHP
121 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Broker;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\StoreBrokerDeal;
|
|
use App\Models\Deal;
|
|
use App\Models\DealCategory;
|
|
use App\Services\FileService;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Str;
|
|
|
|
class BrokerDealController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index()
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*/
|
|
public function create()
|
|
{
|
|
return view('dashboards.broker.deals.create')
|
|
->with('categories', DealCategory::all('id', 'name'));
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(StoreBrokerDeal $request, FileService $fileService)
|
|
{
|
|
$data = $request->validated();
|
|
$data['slug'] = Str::slug($data['title']);
|
|
$data['user_id'] = $request->user()->id;
|
|
|
|
if ($request->hasFile('image')) {
|
|
$image = $request->file('image');
|
|
$data['image'] = $fileService->upload($image, 'images/deals',
|
|
$data['slug'].'.'.$image->extension());
|
|
}
|
|
|
|
Deal::unguard();
|
|
Deal::create($data);
|
|
Deal::reguard();
|
|
|
|
return to_route('broker.dashboard')->with('success', 'Deal has been created.');
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*/
|
|
public function edit(Deal $deal)
|
|
{
|
|
return view('dashboards.broker.deals.edit')
|
|
->with('deal', $deal)
|
|
->with('categories', DealCategory::all('id', 'name'));
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(StoreBrokerDeal $request, Deal $deal, FileService $fileService)
|
|
{
|
|
$data = $request->validated();
|
|
|
|
try {
|
|
DB::transaction(function () use ($deal, $data, $fileService, $request) {
|
|
$data['slug'] = Str::slug($data['title']);
|
|
|
|
if ($request->hasFile('image')) {
|
|
$fileService->delete($deal->image);
|
|
|
|
$image = $request->file('image');
|
|
$data['image'] = $fileService->upload($image, 'images/deals',
|
|
$data['slug'].'.'.$image->extension());
|
|
}
|
|
|
|
Deal::unguard();
|
|
$deal->update($data);
|
|
Deal::reguard();
|
|
|
|
});
|
|
} catch (\Throwable $exception) {
|
|
Log::error($exception->getMessage(), $exception->getTrace());
|
|
|
|
return to_route('broker.dashboard')->with('error', 'Something gone wrong.');
|
|
}
|
|
|
|
return to_route('broker.dashboard')->with('success', 'Deal has been updated.');
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy(Deal $deal, FileService $fileService)
|
|
{
|
|
// remove the image of deal
|
|
|
|
try {
|
|
|
|
DB::transaction(function () use ($deal, $fileService) {
|
|
$fileService->delete($deal->image);
|
|
$deal->delete();
|
|
});
|
|
|
|
} catch (\Throwable $exception) {
|
|
Log::error($exception->getMessage(), $exception->getTrace());
|
|
|
|
return to_route('broker.dashboard')->with('error', 'Something gone wrong.');
|
|
}
|
|
|
|
return to_route('broker.dashboard')->with('success', 'Deal has been deleted.');
|
|
}
|
|
}
|