generated from Flycro/laravel-nuxt
147 lines
4.4 KiB
PHP
147 lines
4.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\BookRecommendation;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Artisan;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class BookRecommendationController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index()
|
|
{
|
|
$relations = [];
|
|
$bookRecommendations = BookRecommendation::query();
|
|
if (request()->has('with')) {
|
|
$relations = explode(',', request()->with);
|
|
$bookRecommendations->with($relations);
|
|
}
|
|
if(request()->has('status')) {
|
|
$bookRecommendations->where('status', request()->status);
|
|
}
|
|
|
|
return $bookRecommendations->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([...$data, '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);
|
|
}
|
|
if ($bookRecommendation->cover_image) {
|
|
Storage::delete($bookRecommendation->cover_image);
|
|
}
|
|
$bookRecommendation->delete();
|
|
return response()->json(null, 204);
|
|
}
|
|
|
|
public function fetchCover(Request $request)
|
|
{
|
|
Artisan::call('book:open-library-fetch-cover-art');
|
|
return response()->json(Artisan::output());
|
|
}
|
|
}
|