dealhub/app/Http/Controllers/Interaction/ReportController.php
kusowl 94ef8f360d feature (favorite and reported deals):
- add favorites and reported tabs in user profile pages
- add remove favorites
- customers can view a deal directly from profiles section and deal modal is shown in explore page
- fix formatting by pint
2026-01-23 16:14:04 +05:30

99 lines
2.5 KiB
PHP

<?php
namespace App\Http\Controllers\Interaction;
use App\Http\Controllers\Controller;
use App\Http\Requests\StoreReportRequest;
use App\Models\Deal;
use App\Models\Report;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class ReportController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
//
}
/**
* Store a newly created resource in storage.
*/
public function store(StoreReportRequest $request, Deal $deal)
{
$data = $request->validated();
$data['user_id'] = Auth::id();
// Check if the user already reported the deal
$alreadyReported = $deal->reports()->where('user_id', Auth::id())->first();
if ($alreadyReported) {
return response()->json(['message' => 'You already reported this report'], 405);
}
try {
DB::transaction(function () use ($data, $deal) {
Report::unguard();
Report::create($data)->deals()->attach($deal);
Report::reguard();
});
return response()->json(['message' => 'Report created'], 201);
} catch (\Throwable $exception) {
Log::error('Error creating report', [
'user_id' => Auth::id(),
'deal_id' => $deal->id,
'error' => $exception->getMessage(),
'trace' => $exception->getTraceAsString(),
]);
return response()->json(['message' => 'Error creating report'], 500);
}
}
/**
* Display the specified resource.
*/
public function show(Report $report)
{
//
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, Report $report)
{
//
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Report $report)
{
try {
DB::transaction(function () use ($report) {
$report->deals()->detach();
$report->delete();
});
return back()->with('success', 'Report deleted successfully.');
} catch (\Throwable $e) {
Log::error('Error deleting report', [
'user_id' => Auth::id(),
'report_id' => $report->id,
'error' => $e->getMessage(),
]);
}
return back()->with('error', 'Something went wrong.');
}
}