feat: image feature added
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
<?php
|
||||
|
||||
// app/Http/Controllers/ImageController.php
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Image;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class ImageController extends Controller
|
||||
{
|
||||
public function store(Request $request)
|
||||
{
|
||||
$data = $request->validate([
|
||||
'image' => 'required|image|max:2048',
|
||||
'title' => 'nullable|string|max:255',
|
||||
'description' => 'nullable|string|max:500',
|
||||
'type' => 'nullable|in:public,private',
|
||||
]);
|
||||
|
||||
$path = $request->file('image')->store('images', 'public');
|
||||
|
||||
$image = Image::create([
|
||||
'user_id' => auth()->id(),
|
||||
'path' => $path,
|
||||
'title' => $data['title'] ?? null,
|
||||
'description' => $data['description'] ?? null,
|
||||
'type' => $data['type'] ?? 'private', // default private
|
||||
]);
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Image uploaded successfully',
|
||||
'image' => $image,
|
||||
'url' => asset('storage/' . $path),
|
||||
]);
|
||||
}
|
||||
|
||||
public function publicImages()
|
||||
{
|
||||
$images = Image::where('type', 'public')->get();
|
||||
|
||||
return response()->json($images->map(function ($image) {
|
||||
$image->url = asset('storage/' . $image->path);
|
||||
return $image;
|
||||
}));
|
||||
}
|
||||
|
||||
public function userImages()
|
||||
{
|
||||
$images = Image::where('user_id', auth()->id())
|
||||
->where('type', 'private')
|
||||
->get();
|
||||
|
||||
return response()->json($images->map(function ($image) {
|
||||
$image->url = asset('storage/' . $image->path);
|
||||
return $image;
|
||||
}));
|
||||
}
|
||||
public function allImages()
|
||||
{
|
||||
$userId = auth()->id();
|
||||
|
||||
$images = Image::where('type', 'public')
|
||||
->orWhere(function ($query) use ($userId) {
|
||||
$query->where('type', 'private')
|
||||
->where('user_id', $userId);
|
||||
})
|
||||
->get();
|
||||
|
||||
return response()->json($images->map(function ($image) {
|
||||
$image->url = asset('storage/' . $image->path);
|
||||
return $image;
|
||||
}));
|
||||
}
|
||||
// ✅ Update image
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
$image = Image::where('id', $id)
|
||||
->where('user_id', auth()->id()) // only owner can update
|
||||
->firstOrFail();
|
||||
|
||||
$data = $request->validate([
|
||||
'title' => 'nullable|string|max:255',
|
||||
'description' => 'nullable|string|max:500',
|
||||
'type' => 'nullable|in:public,private',
|
||||
'image' => 'nullable|image|max:2048',
|
||||
]);
|
||||
|
||||
if ($request->hasFile('image')) {
|
||||
// delete old image
|
||||
Storage::disk('public')->delete($image->path);
|
||||
|
||||
// store new one
|
||||
$path = $request->file('image')->store('images', 'public');
|
||||
$image->path = $path;
|
||||
}
|
||||
|
||||
$image->title = $data['title'] ?? $image->title;
|
||||
$image->description = $data['description'] ?? $image->description;
|
||||
$image->type = $data['type'] ?? $image->type;
|
||||
$image->save();
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Image updated successfully',
|
||||
'image' => $image,
|
||||
'url' => asset('storage/' . $image->path),
|
||||
]);
|
||||
}
|
||||
|
||||
// ✅ Delete image
|
||||
public function destroy($id)
|
||||
{
|
||||
$image = Image::where('id', $id)
|
||||
->where('user_id', auth()->id()) // only owner can delete
|
||||
->firstOrFail();
|
||||
|
||||
Storage::disk('public')->delete($image->path);
|
||||
$image->delete();
|
||||
|
||||
return response()->json(['message' => 'Image deleted successfully']);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Image extends Model
|
||||
{
|
||||
protected $fillable = ['user_id', 'path', 'title', 'description','type',];
|
||||
|
||||
public function user() {
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user