Bicycle/App/Controller/GoalsController.php
2026-08-15 10:39:22 +03:00

71 lines
2.0 KiB
PHP

<?php
/**
* @package Bicycle
* @author Egor Isaev
* @description GoalsController.php
* @copyright (c) 15/08/2026
*/
namespace App\Controller;
use App\Repositories\AccountRepository;
use App\Repositories\GoalRepository;
use App\Repositories\UserRepository;
use Services\Auth;
use System\Classes\Controller;
use System\Classes\HTTP\HTTPException;
use System\Classes\MyException;
use System\Classes\Request;
/**
* Удаление цели (goals) — сама цель не отдельная страница, создаётся/правится прямо в модалке
* счёта (см. App\Controller\AccountsController, поля goal_target_summa/goal_deadline). Тут только
* «Удалить» из ⋮-меню карточки цели на дашборде — счёт при этом остаётся, удаляется только цель.
*/
class GoalsController extends Controller
{
protected bool $_auth_protection = true;
/**
* @return string JSON
* @throws HTTPException|MyException
*/
public function deleteAction(): string
{
$request = Request::$current;
$user = $this->currentUser();
$goal_repo = new GoalRepository();
$goal = $goal_repo->get((int)$request->param(0));
if (!$goal) {
throw HTTPException::factory(404);
}
$account = (new AccountRepository())->get((int)$goal->account_id);
if (!$account || (int)$account->user_id !== (int)$user->id) {
throw HTTPException::factory(404);
}
$goal_repo->delete($goal->id);
return $this->json(['success' => true]);
}
/**
* @return object
* @throws HTTPException|MyException
*/
protected function currentUser(): object
{
$auth_user = Auth::instance()->getUser();
$user = (new UserRepository())->getByLogin($auth_user['login']);
if (!$user) {
throw HTTPException::factory(404);
}
return $user;
}
}