Tasks Module Stage 2: Subtasks
- Create task_subtasks table migration - Create TaskSubtaskModel with CRUD operations - Add subtask API methods: addSubtask, toggleSubtask, deleteSubtask - Update TaskService to include subtasks in getTask() - Add Routes for subtasks API - Update show.twig with subtasks UI and JavaScript
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Tasks\Models;
|
||||
|
||||
use CodeIgniter\Model;
|
||||
use App\Models\Traits\TenantScopedModel;
|
||||
|
||||
class TaskSubtaskModel extends Model
|
||||
{
|
||||
use TenantScopedModel;
|
||||
|
||||
protected $table = 'task_subtasks';
|
||||
protected $primaryKey = 'id';
|
||||
protected $useAutoIncrement = true;
|
||||
protected $returnType = 'array';
|
||||
protected $useSoftDeletes = false;
|
||||
protected $tenantField = 'task.organization_id';
|
||||
protected $allowedFields = [
|
||||
'task_id',
|
||||
'title',
|
||||
'is_completed',
|
||||
'order_index',
|
||||
];
|
||||
protected $useTimestamps = true;
|
||||
protected $createdField = 'created_at';
|
||||
protected $updatedField = 'updated_at';
|
||||
|
||||
/**
|
||||
* Получить подзадачи задачи
|
||||
*/
|
||||
public function getByTask(int $taskId): array
|
||||
{
|
||||
return $this->where('task_id', $taskId)
|
||||
->orderBy('order_index', 'ASC')
|
||||
->findAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* Получить следующий порядковый номер для подзадачи
|
||||
*/
|
||||
public function getNextOrder(int $taskId): int
|
||||
{
|
||||
$max = $this->selectMax('order_index')
|
||||
->where('task_id', $taskId)
|
||||
->first();
|
||||
|
||||
return ($max['order_index'] ?? 0) + 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Переключить статус подзадачи
|
||||
*/
|
||||
public function toggle(int $subtaskId): bool
|
||||
{
|
||||
$subtask = $this->find($subtaskId);
|
||||
if (!$subtask) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->update($subtaskId, [
|
||||
'is_completed' => !$subtask['is_completed'],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Получить количество незавершённых подзадач
|
||||
*/
|
||||
public function getIncompleteCount(int $taskId): int
|
||||
{
|
||||
return $this->where('task_id', $taskId)
|
||||
->where('is_completed', false)
|
||||
->countAllResults();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user