first commit
This commit is contained in:
135
app/Http/Controllers/CategoryNotesController.php
Normal file
135
app/Http/Controllers/CategoryNotesController.php
Normal file
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Category;
|
||||
use App\Models\CategoryNotes;
|
||||
use App\Models\Course;
|
||||
use App\Models\Transaction;
|
||||
use App\Traits\ImageHelper;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class CategoryNotesController extends Controller
|
||||
{
|
||||
use ImageHelper;
|
||||
|
||||
public function create(Category $category)
|
||||
{
|
||||
|
||||
$pageConfigs = [
|
||||
'pageHeader' => true,
|
||||
'showMenu' => true
|
||||
];
|
||||
|
||||
$breadcrumbs = [['link' => '/admin/category/' . $category->id . '/edit', 'name' => 'Notes'], ['name' => 'Add']];
|
||||
|
||||
return view('content.categories.notes.create', [
|
||||
'pageConfigs' => $pageConfigs,
|
||||
'breadcrumbs' => $breadcrumbs,
|
||||
'category' => $category
|
||||
]);
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'category_id' => ['required'],
|
||||
'title' => ['required'],
|
||||
'pdf' => ['required'],
|
||||
]);
|
||||
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
$notes = new CategoryNotes();
|
||||
$notes->category_id = $request->category_id;
|
||||
$notes->title = $request->title;
|
||||
if ($request->hasFile('pdf')) {
|
||||
$pdfPath = $this->uploadPDF($request->pdf, 'pdf');
|
||||
$notes->pdf = $pdfPath;
|
||||
}
|
||||
$notes->save();
|
||||
DB::commit();
|
||||
return redirect()->route('category.edit', $request->category_id)->with([
|
||||
'success' => "Notes created successfully"
|
||||
]);
|
||||
} catch (\Throwable $exception) {
|
||||
DB::rollBack();
|
||||
return redirect()->back()->with([
|
||||
'error' => $exception->getMessage()
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
public function downloadPDF(CategoryNotes $notes)
|
||||
{
|
||||
$path = public_path('storage'.$notes->pdf);
|
||||
return response()->download($path);
|
||||
}
|
||||
|
||||
|
||||
public function edit(CategoryNotes $notes)
|
||||
{
|
||||
|
||||
$pageConfigs = [
|
||||
'pageHeader' => true,
|
||||
'showMenu' => true
|
||||
];
|
||||
|
||||
$breadcrumbs = [['link' => '/admin/category/' . $notes->category_id . '/edit', 'name' => 'Notes'], ['name' => 'Edit']];
|
||||
// $breadcrumbs = [['link' => '/admin/category', 'name' => 'Course'], ['name' => 'Edit']];
|
||||
|
||||
return view('content.categories.notes.edit', [
|
||||
'pageConfigs' => $pageConfigs,
|
||||
'breadcrumbs' => $breadcrumbs,
|
||||
'note' => $notes,
|
||||
]);
|
||||
}
|
||||
|
||||
public function update(Request $request, CategoryNotes $notes)
|
||||
{
|
||||
$request->validate([
|
||||
'title' => ['required'],
|
||||
'pdf' => ['nullable'],
|
||||
]);
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
$notes->title = $request->title;
|
||||
if ($request->hasFile('pdf')) {
|
||||
$this->deleteFile($notes->pdf);
|
||||
$pdfPath = $this->uploadPDF($request->pdf, 'pdf');
|
||||
$notes->pdf = $pdfPath;
|
||||
}
|
||||
|
||||
$notes->update();
|
||||
DB::commit();
|
||||
return redirect()->route('category.edit', $notes->category_id)->with([
|
||||
'success' => "Notes updated successfully"
|
||||
]);
|
||||
|
||||
} catch (\Throwable $exception) {
|
||||
DB::rollBack();
|
||||
return redirect()->back()->with([
|
||||
'error' => $exception->getMessage()
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
public function destroy(CategoryNotes $notes)
|
||||
{
|
||||
try {
|
||||
DB::beginTransaction();
|
||||
$this->deleteFile($notes->pdf);
|
||||
$notes->delete();
|
||||
DB::commit();
|
||||
return redirect()->back()->with([
|
||||
'success' => "Notes deleted successfully"
|
||||
]);
|
||||
} catch (\Throwable $exception) {
|
||||
DB::rollBack();
|
||||
return redirect()->back()->with([
|
||||
'error' => $exception->getMessage()
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user