Files
ewaste-backend/app/Http/Controllers/ListingController.php
2025-11-05 14:47:19 -08:00

79 lines
1.7 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Http\Requests\StoreListingRequest;
use App\Http\Requests\UpdateListingRequest;
use App\Models\Listing;
class ListingController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
return Listing::with('poster')->get()->toJson();
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*/
public function store(StoreListingRequest $request)
{
$attrs = $request->safe()->merge([
'user_id' => $request->user()->id
]);
return Listing::create($attrs->all())->toJson();
}
/**
* Display the specified resource.
*/
public function show(Listing $listing)
{
return $listing->with('poster')->get()->toJson();
}
/**
* Show the form for editing the specified resource.
*/
public function edit(Listing $listing)
{
//
}
/**
* Update the specified resource in storage.
*/
public function update(UpdateListingRequest $request, Listing $listing)
{
return $listing->update($request->validated());
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Listing $listing)
{
if (request()->user()->can('delete', $listing)) {
$listing->delete();
return response()->json([
'deleted_at' => $listing->deleted_at,
], 200);
} else {
return response()->json([
'error' => 'You are not authorized to delete this listing.',
], 403);
}
}
}