127 lines
3.2 KiB
PHP
127 lines
3.2 KiB
PHP
<?php
|
||
|
||
namespace App\Modules\Tasks\Services;
|
||
|
||
use App\Modules\Tasks\Models\TaskBoardModel;
|
||
use App\Modules\Tasks\Models\TaskColumnModel;
|
||
|
||
class TaskBoardService
|
||
{
|
||
protected TaskBoardModel $boardModel;
|
||
protected TaskColumnModel $columnModel;
|
||
|
||
public function __construct()
|
||
{
|
||
$this->boardModel = new TaskBoardModel();
|
||
$this->columnModel = new TaskColumnModel();
|
||
}
|
||
|
||
/**
|
||
* Создать новую доску с дефолтными колонками
|
||
*/
|
||
public function createBoard(array $data, int $userId): int
|
||
{
|
||
$boardId = $this->boardModel->insert($data);
|
||
|
||
if ($boardId) {
|
||
// Создаём дефолтные колонки
|
||
$this->columnModel->createDefaultColumns($boardId);
|
||
}
|
||
|
||
return $boardId;
|
||
}
|
||
|
||
/**
|
||
* Обновить доску
|
||
*/
|
||
public function updateBoard(int $boardId, array $data, int $organizationId): bool
|
||
{
|
||
$board = $this->boardModel->getBoard($boardId, $organizationId);
|
||
if (!$board) {
|
||
return false;
|
||
}
|
||
|
||
return $this->boardModel->update($boardId, $data);
|
||
}
|
||
|
||
/**
|
||
* Удалить доску
|
||
*/
|
||
public function deleteBoard(int $boardId, int $organizationId): bool
|
||
{
|
||
$board = $this->boardModel->getBoard($boardId, $organizationId);
|
||
if (!$board) {
|
||
return false;
|
||
}
|
||
|
||
return $this->boardModel->delete($boardId);
|
||
}
|
||
|
||
/**
|
||
* Получить доску с колонками
|
||
*/
|
||
public function getBoardWithColumns(int $boardId, int $organizationId): ?array
|
||
{
|
||
$board = $this->boardModel->getBoard($boardId, $organizationId);
|
||
if (!$board) {
|
||
return null;
|
||
}
|
||
|
||
$board['columns'] = $this->columnModel->getColumnsByBoard($boardId);
|
||
|
||
return $board;
|
||
}
|
||
|
||
/**
|
||
* Получить все доски организации
|
||
*/
|
||
public function getOrganizationBoards(int $organizationId): array
|
||
{
|
||
return $this->boardModel->getBoardsByOrganization($organizationId);
|
||
}
|
||
|
||
/**
|
||
* Создать колонку
|
||
*/
|
||
public function createColumn(int $boardId, array $data): int
|
||
{
|
||
$data['board_id'] = $boardId;
|
||
$data['order_index'] = $this->columnModel->getNextOrderIndex($boardId);
|
||
|
||
return $this->columnModel->insert($data);
|
||
}
|
||
|
||
/**
|
||
* Обновить колонку
|
||
*/
|
||
public function updateColumn(int $columnId, array $data): bool
|
||
{
|
||
return $this->columnModel->update($columnId, $data);
|
||
}
|
||
|
||
/**
|
||
* Удалить колонку
|
||
*/
|
||
public function deleteColumn(int $columnId, int $boardId): bool
|
||
{
|
||
$column = $this->columnModel->find($columnId);
|
||
if (!$column || $column['board_id'] !== $boardId) {
|
||
return false;
|
||
}
|
||
|
||
return $this->columnModel->delete($columnId);
|
||
}
|
||
|
||
/**
|
||
* Изменить порядок колонок
|
||
*/
|
||
public function reorderColumns(array $columnOrders): bool
|
||
{
|
||
foreach ($columnOrders as $index => $columnId) {
|
||
$this->columnModel->update($columnId, ['order_index' => $index]);
|
||
}
|
||
|
||
return true;
|
||
}
|
||
}
|