Files
sccs_laravel/app/Traits/AdCommentHelper.php
2025-11-04 16:23:40 +05:00

161 lines
5.5 KiB
PHP

<?php
namespace App\Traits;
use App\Models\AdComment;
use App\Models\AdCommentReply;
use App\Models\AdHug;
use App\Models\AdsManagement;
use App\Models\HiddenAd;
use App\Models\ReportedAd;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
trait AdCommentHelper
{
use AdHelper, ApiResponseHelper;
public function aadAdComment(Request $request, AdsManagement $ad)
{
try {
$user = Auth::user();
$comment = new AdComment();
$comment->commenting_user_id = $user->id;
$comment->comment = $request->comment;
$ad->adComments()->save($comment);
return $this->apiResponse(true, 'Comment added successfully');
} catch (\Throwable $exception) {
return $this->apiResponse(false, $exception->getMessage());
}
}
public function updateAdComment(Request $request, AdComment $adComment)
{
try {
$ad = AdsManagement::findOrFail($adComment->ad_id);
$ad->adComments()->where('id', $adComment->id)->update([
'comment' => $request->comment
]);
return $this->apiResponse(true, 'Comment updated successfully');
} catch (\Exception $e) {
return $this->apiResponse(false, $e->getMessage());
}
}
public function deleteAdComment(AdComment $adComment)
{
try {
if($adComment->adCommentReplies()){
$adComment->adCommentReplies->delete();
}
$adComment->delete();
return $this->apiResponse(true, 'Comment deleted successfully');
} catch (\Throwable $exception) {
return $this->apiResponse(false, $exception->getMessage());
}
}
public function addReplyToAdComment(Request $request, AdComment $adComment)
{
try {
$adCommentReply = new AdCommentReply();
$adCommentReply->replying_user_id = auth()->id();
$adCommentReply->reply = $request->reply;
$adComment->adCommentReplies()->save($adCommentReply);
return $this->apiResponse(true, 'Reply added successfully');
} catch (\Throwable $exception) {
return $this->apiResponse(false, $exception->getMessage());
}
}
public function updateReplyToAdComment(Request $request, AdCommentReply $adCommentReply)
{
try {
$adComment = AdComment::findOrFail($adCommentReply->ad_comment_id);
$adComment->adCommentReplies()->where('id', $adCommentReply->id)->update([
'reply' => $request->reply
]);
return $this->apiResponse(true, 'Reply updated successfully');
} catch (\Throwable $exception) {
return $this->apiResponse(false, $exception->getMessage());
}
}
public function deleteReplyToAdComment(AdCommentReply $adCommentReply)
{
try {
$adCommentReply->delete();
return $this->apiResponse(true, 'Reply removed successfully');
} catch (\Throwable $exception) {
return $this->apiResponse(false, $exception->getMessage());
}
}
public function aadAdHug(Request $request, AdsManagement $ad)
{
try {
$user = Auth::user();
if ($request->status == 'true') {
$checkHug = AdHug::where(['hugging_user_id' => $user->id, 'ad_id' => $ad->id])->first();
if (!$checkHug) {
$hug = new AdHug();
$hug->hugging_user_id = $user->id;
$ad->adHugs()->save($hug);
return $this->apiResponse(true, 'Ad hugged successfully');
}
return $this->apiResponse(true, 'Ad already hugged');
} elseif ($request->status == 'false') {
$checkHug = AdHug::where(['hugging_user_id' => $user->id, 'ad_id' => $ad->id])->first();
if(!$checkHug){
return $this->apiResponse(true, 'Hug does not exist');
}
$checkHug->delete();
return $this->apiResponse(true, 'Hug removed successfully');
}
} catch (\Throwable $exception) {
return $this->apiResponse(false, $exception->getMessage());
}
}
public function reportAd(Request $request, AdsManagement $ad)
{
try {
if ($this->checkAdReportRequest($ad)) {
return $this->apiResponse(false, 'Your Request was already completed');
}
$user = Auth::user();
ReportedAd::create([
'ad_id' => $ad->id,
'reporting_user_id' => $user->id,
'reason' => $request->reason
]);
return $this->apiResponse(true, 'Request submitted successfully');
} catch (\Throwable $exception) {
return $this->apiResponse(false, $exception->getMessage());
}
}
public function hideAd(AdsManagement $ad)
{
try {
if ($this->checkAdHideRequest($ad)) {
return $this->apiResponse(false, 'Your Request was already completed');
}
$user = Auth::user();
HiddenAd::create([
'ad_id' => $ad->id,
'hide_on' => $user->id,
]);
return $this->apiResponse(true, 'Request submitted successfully');
} catch (\Throwable $exception) {
return $this->apiResponse(false, $exception->getMessage());
}
}
}