99 lines
2.6 KiB
PHP
99 lines
2.6 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())->exists();
|
|
if ($alreadyReported) {
|
|
return response()->json(['message' => 'You had already reported this deal'], 200);
|
|
}
|
|
|
|
try {
|
|
DB::transaction(function () use ($data, $deal) {
|
|
Report::unguard();
|
|
Report::create($data)->deals()->attach($deal);
|
|
Report::reguard();
|
|
});
|
|
|
|
return response()->json(['message' => 'Report submitted. Thank you for keeping DealHub safe'], 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.');
|
|
}
|
|
}
|