Current state
This commit is contained in:
114
app/Http/Controllers/ManageController.php
Normal file
114
app/Http/Controllers/ManageController.php
Normal file
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Artwork;
|
||||
use App\Models\ArtworkCategory;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class ManageController extends Controller
|
||||
{
|
||||
public function index(Request $request)
|
||||
{
|
||||
$userId = $request->user()->id;
|
||||
$perPage = 50;
|
||||
|
||||
// Use legacy connection query builder and join category name to avoid Eloquent model issues
|
||||
$query = DB::connection('legacy')->table('artworks as a')
|
||||
->leftJoin('artworks_categories as c', 'a.category', '=', 'c.category_id')
|
||||
->where('a.user_id', $userId)
|
||||
->select('a.*', 'c.category_name')
|
||||
->orderByDesc('a.datum')
|
||||
->orderByDesc('a.id');
|
||||
|
||||
$artworks = $query->paginate($perPage);
|
||||
|
||||
return view('manage.index', [
|
||||
'artworks' => $artworks,
|
||||
'page_title' => 'Artwork Manager',
|
||||
]);
|
||||
}
|
||||
|
||||
public function edit(Request $request, $id)
|
||||
{
|
||||
$userId = $request->user()->id;
|
||||
$artwork = DB::connection('legacy')->table('artworks')->where('id', (int)$id)->where('user_id', $userId)->first();
|
||||
if (! $artwork) {
|
||||
abort(404);
|
||||
}
|
||||
|
||||
$categories = DB::connection('legacy')->table('artworks_categories')->where('section_id', 0)->orderBy('category_id')->get();
|
||||
|
||||
return view('manage.edit', [
|
||||
'artwork' => $artwork,
|
||||
'categories' => $categories,
|
||||
'page_title' => 'Edit Artwork: ' . ($artwork->name ?? ''),
|
||||
]);
|
||||
}
|
||||
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
$userId = $request->user()->id;
|
||||
$existing = DB::connection('legacy')->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',
|
||||
]);
|
||||
$update = [
|
||||
'name' => $data['name'],
|
||||
'category' => $data['section'] ?? $existing->category,
|
||||
'description' => $data['description'] ?? $existing->description,
|
||||
'updated' => now(),
|
||||
];
|
||||
|
||||
// handle artwork image upload (replacing picture)
|
||||
if ($request->hasFile('artwork')) {
|
||||
$file = $request->file('artwork');
|
||||
$path = $file->store('public/uploads/artworks');
|
||||
$filename = basename($path);
|
||||
$update['picture'] = $filename;
|
||||
}
|
||||
|
||||
// handle attachment upload (zip, etc.)
|
||||
if ($request->hasFile('attachment')) {
|
||||
$att = $request->file('attachment');
|
||||
$attPath = $att->store('public/uploads/attachments');
|
||||
$update['fname'] = basename($attPath);
|
||||
}
|
||||
|
||||
DB::connection('legacy')->table('artworks')->where('id', (int)$id)->where('user_id', $userId)->update($update);
|
||||
|
||||
return redirect()->route('manage')->with('status', 'Artwork was successfully updated.');
|
||||
}
|
||||
|
||||
public function destroy(Request $request, $id)
|
||||
{
|
||||
$userId = $request->user()->id;
|
||||
$artwork = DB::connection('legacy')->table('artworks')->where('id', (int)$id)->where('user_id', $userId)->first();
|
||||
if (! $artwork) {
|
||||
abort(404);
|
||||
}
|
||||
|
||||
// delete files if present (stored in new storage location)
|
||||
if (!empty($artwork->fname)) {
|
||||
Storage::delete('public/uploads/attachments/' . $artwork->fname);
|
||||
}
|
||||
if (!empty($artwork->picture)) {
|
||||
Storage::delete('public/uploads/artworks/' . $artwork->picture);
|
||||
}
|
||||
|
||||
DB::connection('legacy')->table('artworks')->where('id', (int)$id)->where('user_id', $userId)->delete();
|
||||
|
||||
return redirect()->route('manage')->with('status', 'Artwork deleted.');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user