����JFIF��x�x����'
| Server IP : 78.140.185.180  /  Your IP : 216.73.216.51 Web Server : LiteSpeed System : Linux cpanel13.v.fozzy.com 4.18.0-513.11.1.lve.el8.x86_64 #1 SMP Thu Jan 18 16:21:02 UTC 2024 x86_64 User : builderbox ( 1072) PHP Version : 7.3.33 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : OFF | Pkexec : OFF Directory : /proc/thread-self/root/home/builderbox/././././././www/common/Comments/ | 
| Upload File : | 
<?php
namespace Common\Comments;
use Common\Core\BaseController;
use Common\Database\Datasource\MysqlDataSource;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Symfony\Component\HttpFoundation\Response;
class CommentController extends BaseController
{
    /**
     * @var Comment
     */
    private $comment;
    /**
     * @var Request
     */
    private $request;
    public function __construct(Comment $comment, Request $request)
    {
        $this->comment = $comment;
        $this->request = $request;
    }
    public function index(): Response
    {
        $userId = $this->request->get("userId");
        $this->authorize("index", [Comment::class, $userId]);
        $dataSource = new MysqlDataSource(
            $this->comment->with(["user", "commentable"]),
            $this->request->all(),
        );
        $pagination = $dataSource->paginate();
        $pagination->transform(function (Comment $comment) {
            if ($comment->commentable) {
                $normalized = $comment->commentable->toNormalizedArray();
                $comment->unsetRelation("commentable");
                $comment->setAttribute("commentable", $normalized);
            }
            return $comment;
        });
        return $this->success(["pagination" => $pagination]);
    }
    public function show(Comment $comment): Response
    {
        $this->authorize("show", $comment);
        return $this->success(["comment" => $comment]);
    }
    public function store(CrupdateCommentRequest $request): Response
    {
        $this->authorize("store", Comment::class);
        $comment = app(CrupdateComment::class)->execute($request->all());
        return $this->success(["comment" => $comment]);
    }
    public function update(
        Comment $comment,
        CrupdateCommentRequest $request
    ): Response {
        $this->authorize("store", $comment);
        $comment = app(CrupdateComment::class)->execute(
            $request->all(),
            $comment,
        );
        return $this->success(["comment" => $comment]);
    }
    public function destroy(string $ids): Response
    {
        $commentIds = explode(",", $ids);
        $this->authorize("destroy", [Comment::class, $commentIds]);
        $allDeleted = [];
        $allMarkedAsDeleted = [];
        $this->comment
            ->whereIn("id", $commentIds)
            ->chunkById(100, function (Collection $comments) use (
                &$allDeleted,
                &$allMarkedAsDeleted
            ) {
                $toMarkAsDeleted = [];
                $toDelete = [];
                foreach ($comments as $comment) {
                    if ($comment->allChildren()->count() > 1) {
                        $toMarkAsDeleted[] = $comment->id;
                    } else {
                        $toDelete[] = $comment->id;
                    }
                }
                if (!empty($toMarkAsDeleted)) {
                    $this->comment
                        ->whereIn("id", $toMarkAsDeleted)
                        ->update(["deleted" => true]);
                }
                if (!empty($toDelete)) {
                    $this->comment->whereIn("id", $toDelete)->delete();
                }
                $allDeleted = array_merge($allDeleted, $toDelete);
                $allMarkedAsDeleted = array_merge(
                    $allMarkedAsDeleted,
                    $toMarkAsDeleted,
                );
            });
        return $this->success([
            "allDeleted" => $allDeleted,
            "allMarkedAsDeleted" => $allMarkedAsDeleted,
        ]);
    }
    public function restore()
    {
        $this->authorize("update", Comment::class);
        $commentIds = $this->request->get("commentIds");
        $this->comment
            ->whereIn("id", $commentIds)
            ->update(["deleted" => false]);
        return $this->success();
    }
}