Upload beautify
This commit is contained in:
@@ -2,8 +2,9 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Artwork;
|
||||
use App\Models\ArtworkCategory;
|
||||
use App\Http\Requests\Manage\ManageArtworkEditRequest;
|
||||
use App\Http\Requests\Manage\ManageArtworkUpdateRequest;
|
||||
use App\Http\Requests\Manage\ManageArtworkDestroyRequest;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
@@ -38,13 +39,9 @@ class ManageController extends Controller
|
||||
]);
|
||||
}
|
||||
|
||||
public function edit(Request $request, $id)
|
||||
public function edit(ManageArtworkEditRequest $request, $id)
|
||||
{
|
||||
$userId = $request->user()->id;
|
||||
$artwork = DB::table('artworks')->where('id', (int)$id)->where('user_id', $userId)->first();
|
||||
if (! $artwork) {
|
||||
abort(404);
|
||||
}
|
||||
$artwork = $request->artwork();
|
||||
|
||||
// If artworks no longer have a single `category` column, fetch pivot selection
|
||||
$selectedCategory = DB::table('artwork_category')->where('artwork_id', (int)$id)->value('category_id');
|
||||
@@ -63,22 +60,10 @@ class ManageController extends Controller
|
||||
]);
|
||||
}
|
||||
|
||||
public function update(Request $request, $id)
|
||||
public function update(ManageArtworkUpdateRequest $request, $id)
|
||||
{
|
||||
$userId = $request->user()->id;
|
||||
$existing = DB::table('artworks')->where('id', (int)$id)->where('user_id', $userId)->first();
|
||||
|
||||
if (! $existing) {
|
||||
abort(404);
|
||||
}
|
||||
|
||||
$data = $request->validate([
|
||||
'name' => 'required|string|max:255',
|
||||
'section' => 'nullable|integer',
|
||||
'description' => 'nullable|string',
|
||||
'artwork' => 'nullable|file|image',
|
||||
'attachment' => 'nullable|file',
|
||||
]);
|
||||
$existing = $request->artwork();
|
||||
$data = $request->validated();
|
||||
$update = [
|
||||
'name' => $data['name'],
|
||||
'description' => $data['description'] ?? $existing->description,
|
||||
@@ -100,7 +85,7 @@ class ManageController extends Controller
|
||||
$update['fname'] = basename($attPath);
|
||||
}
|
||||
|
||||
DB::table('artworks')->where('id', (int)$id)->where('user_id', $userId)->update($update);
|
||||
DB::table('artworks')->where('id', (int)$id)->update($update);
|
||||
|
||||
// Update pivot: set single category selection for this artwork
|
||||
if (isset($data['section'])) {
|
||||
@@ -114,13 +99,9 @@ class ManageController extends Controller
|
||||
return redirect()->route('manage')->with('status', 'Artwork was successfully updated.');
|
||||
}
|
||||
|
||||
public function destroy(Request $request, $id)
|
||||
public function destroy(ManageArtworkDestroyRequest $request, $id)
|
||||
{
|
||||
$userId = $request->user()->id;
|
||||
$artwork = DB::table('artworks')->where('id', (int)$id)->where('user_id', $userId)->first();
|
||||
if (! $artwork) {
|
||||
abort(404);
|
||||
}
|
||||
$artwork = $request->artwork();
|
||||
|
||||
// delete files if present (stored in new storage location)
|
||||
if (!empty($artwork->fname)) {
|
||||
@@ -130,7 +111,7 @@ class ManageController extends Controller
|
||||
Storage::delete('public/uploads/artworks/' . $artwork->picture);
|
||||
}
|
||||
|
||||
DB::table('artworks')->where('id', (int)$id)->where('user_id', $userId)->delete();
|
||||
DB::table('artworks')->where('id', (int)$id)->delete();
|
||||
|
||||
return redirect()->route('manage')->with('status', 'Artwork deleted.');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user