generated from Flycro/laravel-nuxt
feat: Multiple Systems
This commit is contained in:
130
app/Http/Controllers/BookRecommendationController.php
Normal file
130
app/Http/Controllers/BookRecommendationController.php
Normal file
@@ -0,0 +1,130 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\BookRecommendation;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class BookRecommendationController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$relations = [];
|
||||
if (request()->has('with')) {
|
||||
$relations = explode(',', request()->with);
|
||||
}
|
||||
return BookRecommendation::with($relations)->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'book_name' => 'required|string|max:255',
|
||||
'author' => 'required|string|max:255',
|
||||
'description' => 'nullable|string',
|
||||
'isbn' => 'required|string|unique:book_recommendations,isbn',
|
||||
'pages' => 'required|integer',
|
||||
'status' => 'in:PENDING,COMPLETED,REJECTED,ACTIVE',
|
||||
'cover_image' => 'nullable|image|mimes:jpeg,png,jpg,gif|max:2048',
|
||||
]);
|
||||
|
||||
$data = $request->all();
|
||||
|
||||
if ($request->hasFile('cover_image')) {
|
||||
$imagePath = $request->file('cover_image')->store('cover_images', 'public');
|
||||
$data['cover_image'] = $imagePath;
|
||||
}
|
||||
|
||||
$bookRecommendation = BookRecommendation::create([...$request->all(), 'recommended_by' => auth()->id()]);
|
||||
return response()->json($bookRecommendation, 201);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*/
|
||||
public function show(string $id)
|
||||
{
|
||||
$relations = [];
|
||||
if (request()->has('with')) {
|
||||
$relations = explode(',', request()->with);
|
||||
}
|
||||
|
||||
$bookRecommendation = BookRecommendation::with($relations)->findOrFail($id);
|
||||
return response()->json($bookRecommendation);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*/
|
||||
public function edit(string $id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, string $id)
|
||||
{
|
||||
$bookRecommendation = BookRecommendation::findOrFail($id);
|
||||
$request->validate([
|
||||
'book_name' => 'string|max:255',
|
||||
'author' => 'string|max:255',
|
||||
'description' => 'nullable|string',
|
||||
'isbn' => 'string|unique:book_recommendations,isbn,'.$bookRecommendation->id,
|
||||
'pages' => 'integer',
|
||||
'recommended_by' => 'exists:users,id',
|
||||
'status' => 'in:PENDING,COMPLETED,REJECTED,ACTIVE',
|
||||
'cover_image' => 'nullable|image|mimes:jpeg,png,jpg,gif|max:2048',
|
||||
]);
|
||||
|
||||
|
||||
if ($bookRecommendation->recommended_by !== auth()->id() && !(auth()->user()->hasRole('admin')) ) {
|
||||
return response()->json(['message' => 'Keine Berechtigung.'], 403);
|
||||
}
|
||||
|
||||
$data = $request->all();
|
||||
|
||||
if ($request->hasFile('cover_image')) {
|
||||
// Delete old image if exists
|
||||
if ($bookRecommendation->cover_image) {
|
||||
Storage::delete($bookRecommendation->cover_image);
|
||||
}
|
||||
|
||||
$imagePath = $request->file('cover_image')->store('cover_images', 'public');
|
||||
$data['cover_image'] = $imagePath;
|
||||
}
|
||||
|
||||
$bookRecommendation->update($data);
|
||||
|
||||
return response()->json($bookRecommendation);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy(string $id)
|
||||
{
|
||||
$bookRecommendation = BookRecommendation::findOrFail($id);
|
||||
if ($bookRecommendation->recommended_by !== auth()->id() && !(auth()->user()->hasRole('admin')) ) {
|
||||
return response()->json(['message' => 'Keine Berechtigung.'], 403);
|
||||
}
|
||||
$bookRecommendation->delete();
|
||||
return response()->json(null, 204);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user