feat: BookRecommendation Functionality

This commit is contained in:
2024-03-20 18:07:30 +01:00
parent 096698a8e1
commit 5ab2c46335
3 changed files with 120 additions and 4 deletions

View File

@@ -14,10 +14,16 @@ class BookRecommendationController extends Controller
public function index()
{
$relations = [];
$bookRecommendations = BookRecommendation::query();
if (request()->has('with')) {
$relations = explode(',', request()->with);
$bookRecommendations->with($relations);
}
return BookRecommendation::with($relations)->get();
if(request()->has('status')) {
$bookRecommendations->where('status', request()->status);
}
return $bookRecommendations->get();
}
/**
@@ -50,7 +56,7 @@ class BookRecommendationController extends Controller
$data['cover_image'] = $imagePath;
}
$bookRecommendation = BookRecommendation::create([...$request->all(), 'recommended_by' => auth()->id()]);
$bookRecommendation = BookRecommendation::create([...$data, 'recommended_by' => auth()->id()]);
return response()->json($bookRecommendation, 201);
}
@@ -124,6 +130,9 @@ class BookRecommendationController extends Controller
if ($bookRecommendation->recommended_by !== auth()->id() && !(auth()->user()->hasRole('admin')) ) {
return response()->json(['message' => 'Keine Berechtigung.'], 403);
}
if ($bookRecommendation->cover_image) {
Storage::delete($bookRecommendation->cover_image);
}
$bookRecommendation->delete();
return response()->json(null, 204);
}