feat: Added StayType management with CRUD operations and DataTables integration
This commit is contained in:
@@ -0,0 +1,153 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\CostCalculator\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Str;
|
||||
use Modules\CostCalculator\Models\StayType;
|
||||
use Modules\CostCalculator\Services\StayTypeService;
|
||||
use Yajra\DataTables\Facades\DataTables;
|
||||
|
||||
class StayTypeController extends Controller
|
||||
{
|
||||
protected $stayTypeService;
|
||||
|
||||
public function __construct(StayTypeService $stayTypeService)
|
||||
{
|
||||
$this->stayTypeService = $stayTypeService;
|
||||
}
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index(?int $id = null)
|
||||
{
|
||||
$isEditing = !is_null($id);
|
||||
$stay = $isEditing ? $this->stayTypeService->getStayTypeById($id) : null;
|
||||
|
||||
if (request()->ajax()) {
|
||||
$model = StayType::query()->orderBy('order');
|
||||
|
||||
return DataTables::eloquent($model)
|
||||
->addIndexColumn()
|
||||
->setRowClass('tableRow')
|
||||
->editColumn('status', function (StayType $stay) {
|
||||
$status = $stay->status ? 'Published' : 'Draft';
|
||||
$color = $stay->status ? 'text-success' : 'text-danger';
|
||||
return "<p class='{$color}'>{$status}</p>";
|
||||
})
|
||||
->addColumn('action', 'costcalculator::staytype.datatable.action')
|
||||
->rawColumns(['action', 'status'])
|
||||
->toJson();
|
||||
}
|
||||
|
||||
return view('costcalculator::staytype.index', [
|
||||
'stay' => $stay,
|
||||
'editable' => $isEditing ? true : false,
|
||||
'title' => $isEditing ? 'Edit Stay Type' : 'Add Stay Type',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$isEditing = $request->has('id');
|
||||
|
||||
$request->merge([
|
||||
'slug' => Str::slug($request->title),
|
||||
]);
|
||||
|
||||
if ($isEditing) {
|
||||
$validated = $request->validate([
|
||||
'title' => ['required', 'string', 'max:255'],
|
||||
'slug' => ['required', 'string'],
|
||||
]);
|
||||
|
||||
$stay = $this->stayTypeService->updateStayType($request->id, partnerData: $validated);
|
||||
flash()->success("stay type for {$stay->title} has been updated.");
|
||||
return to_route('stay.index');
|
||||
}
|
||||
|
||||
$maxOrder = StayType::max('order');
|
||||
$order = $maxOrder ? ++$maxOrder : 1;
|
||||
|
||||
$request->mergeIfMissing([
|
||||
'order' => $order,
|
||||
]);
|
||||
|
||||
$validated = $request->validate([
|
||||
'title' => ['required', 'string'],
|
||||
'slug' => ['required', 'string'],
|
||||
'order' => ['integer'],
|
||||
]);
|
||||
|
||||
$stay = $this->stayTypeService->storeStayType($validated);
|
||||
flash()->success("Stay for {$stay->title} has been created.");
|
||||
return to_route('stay.index');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the specified resource.
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*/
|
||||
public function edit($id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy($id)
|
||||
{
|
||||
$partner = $this->stayTypeService->deleteStayType($id);
|
||||
return response()->json(['status' => 200, 'message' => "Stay Type has been deleted."], 200);
|
||||
}
|
||||
|
||||
public function reorder(Request $request)
|
||||
{
|
||||
$stays = $this->stayTypeService->getAllCategories();
|
||||
|
||||
foreach ($stays as $partner) {
|
||||
foreach ($request->order as $order) {
|
||||
if ($order['id'] == $partner->id) {
|
||||
$partner->update(['order' => $order['position']]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return response(['status' => true, 'message' => 'Reordered successfully'], 200);
|
||||
}
|
||||
|
||||
public function toggle($id)
|
||||
{
|
||||
$partner = StayType::findOrFail($id);
|
||||
$partner->update(['status' => !$partner->status]);
|
||||
return response(['status' => 200, 'message' => 'Toggled successfully'], 200);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user