coopService = $coopService; } /** * Display a listing of the resource. */ public function index(?int $id = null) { $isEditing = !is_null($id); $coop = $isEditing ? $this->coopService->getCoopById($id) : null; if (request()->ajax()) { $model = Coop::query()->orderBy('order'); return DataTables::eloquent($model) ->addIndexColumn() ->setRowClass('tableRow') ->editColumn('status', function (Coop $coop) { $status = $coop->status ? 'Published' : 'Draft'; $color = $coop->status ? 'text-success' : 'text-danger'; return "
{$status}
"; }) ->editColumn('link', function (Coop $coop) { return $coop->link ?? '-'; }) ->addColumn('action', 'coursefinder::coop.datatable.action') ->rawColumns(['image', 'action', 'status']) ->toJson(); } return view('coursefinder::coop.index', [ 'coop' => $coop, 'editable' => $isEditing ? true : false, 'title' => $isEditing ? 'Edit Coop' : 'Add Coop', ]); } /** * 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', 'unique:coops,title,' . $request->id], 'slug' => ['required', 'string'], 'link' => ['nullable'], 'image' => ['nullable', 'string'], ]); $coop = $this->coopService->updateCoop($request->id, coopData: $validated); flash()->success("Coop for {$coop->title} has been updated."); return to_route('coop.index'); } $maxOrder = Coop::max('order'); $order = $maxOrder ? ++$maxOrder : 1; $request->mergeIfMissing([ 'order' => $order, ]); $validated = $request->validate([ 'title' => ['required', 'string', 'unique:coops,title'], 'slug' => ['required', 'string'], 'link' => ['nullable'], 'image' => ['nullable', 'string'], 'order' => ['integer'], ]); $coop = $this->coopService->storeCoop($validated); flash()->success("Coop for {$coop->title} has been created."); return to_route('coop.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) { $coop = $this->coopService->deleteCoop($id); return response()->json(['status' => 200, 'message' => "Coop has been deleted."], 200); } public function reorder(Request $request) { $coops = $this->coopService->getAllCategories(); foreach ($coops as $coop) { foreach ($request->order as $order) { if ($order['id'] == $coop->id) { $coop->update(['order' => $order['position']]); } } } return response(['status' => true, 'message' => 'Reordered successfully'], 200); } public function toggle($id) { $coop = Coop::findOrFail($id); $coop->update(['status' => !$coop->status]); return response(['status' => 200, 'message' => 'Toggled successfully'], 200); } }