Рефакторинг, отказ от $u->testAction('',2) для разрыва зависимости с User::class. Добавлен зачаточный генератор запросов.

This commit is contained in:
2024-05-14 17:24:54 +03:00
parent 7b8ffdfd6a
commit bc136cc030
11 changed files with 802 additions and 742 deletions

View File

@@ -3,10 +3,13 @@
namespace Model;
use Core\Db;
use Exception;
use Helper\QueryBuilder;
use User\UserIp;
class ActionModel
{
private const TABLE_NAME = 'actions';
private int $uid;
public function __construct(int $uid)
@@ -39,6 +42,32 @@ class ActionModel
return Db::getRows('select * from actions');
}
public static function deleteById(int $id): void
{
Db::sql('delete from actions where id = ?', [$id]);
}
public static function testCount(array $filters, int $limit = 0): int
{
$query = new QueryBuilder(self::TABLE_NAME, 'count(id)');
try {
$stmt = $query->select($filters, $limit);
return Db::getValue($stmt['sql'], $stmt['binds']);
} catch (Exception $e) {
echo $e->getMessage();
trigger_error(__METHOD__ . ': ' . $e->getMessage(), E_USER_ERROR);
}
}
public static function testCountCustom(string $filter, array $binds, int $limit = 0): int
{
$sql = "select count(id) from actions where $filter";
if ($limit > 0) {
$sql .= ' limit ' . $limit;
}
return (int)Db::getValue($sql, $binds);
}
public function getByVals(string $vals)
{
return Db::getRow('select * from actions where uid = ? and vals = ?', [$this->uid, $vals]);
@@ -65,28 +94,30 @@ class ActionModel
Db::sql('delete from actions where uid = ? and vals = ?', [$this->uid, $vals]);
}
public static function deleteById(int $id): void
public function getDailyQuest(): array
{
Db::sql('delete from actions where id = ?', [$id]);
$filter = [
"uid = $this->uid",
"vars = 'day_quest'",
];
return self::getAction($filter);
}
/*protected function testAction($filter, $tp): array
public static function getAction(array $filters, int $limit = 1): array
{
if ($tp == 1) {
$query = 'select * from actions where ' . $filter;
} elseif ($tp == 2) {
$query = 'select count(*) from actions where ' . $filter;
} else {
return [];
$query = new QueryBuilder(self::TABLE_NAME);
try {
$stmt = $query->select($filters, $limit);
$result = Db::getRows($stmt['sql'], $stmt['binds']);
if (count($result) === 1 || $limit === 1) {
return $result[0];
}
return $result;
} catch (Exception $e) {
echo $e->getMessage();
trigger_error(__METHOD__ . ': ' . $e->getMessage(), E_USER_ERROR);
}
$arr = mysql_fetch_array(mysql_query($query));
return !empty($arr) ? $arr : [];
}*/
public function getDailyQuest(): array|false
{
return Db::getRow("select * from actions where uid = ? and vars = 'day_quest' limit 1", [$this->uid]);
}
public function getFinishedDailyQuestTasks(int $timeout): array