Files
kandev/apps/backend/internal/task/repository/sqlite/board.go
T

231 lines
6.8 KiB
Go

package sqlite
import (
"context"
"database/sql"
"fmt"
"time"
"github.com/google/uuid"
"github.com/kandev/kandev/internal/task/models"
v1 "github.com/kandev/kandev/pkg/api/v1"
)
// AddTaskToBoard adds a task to a board with placement
func (r *Repository) AddTaskToBoard(ctx context.Context, taskID, boardID, columnID string, position int) error {
_, err := r.db.ExecContext(ctx, `
UPDATE tasks SET board_id = ?, column_id = ?, position = ?, updated_at = ? WHERE id = ?
`, boardID, columnID, position, time.Now().UTC(), taskID)
return err
}
// RemoveTaskFromBoard removes a task from a board
func (r *Repository) RemoveTaskFromBoard(ctx context.Context, taskID, boardID string) error {
_, err := r.db.ExecContext(ctx, `
UPDATE tasks SET board_id = '', column_id = '', position = 0, updated_at = ? WHERE id = ? AND board_id = ?
`, time.Now().UTC(), taskID, boardID)
return err
}
// Board operations
// CreateBoard creates a new board
func (r *Repository) CreateBoard(ctx context.Context, board *models.Board) error {
if board.ID == "" {
board.ID = uuid.New().String()
}
now := time.Now().UTC()
board.CreatedAt = now
board.UpdatedAt = now
_, err := r.db.ExecContext(ctx, `
INSERT INTO boards (id, workspace_id, name, description, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?)
`, board.ID, board.WorkspaceID, board.Name, board.Description, board.CreatedAt, board.UpdatedAt)
return err
}
// GetBoard retrieves a board by ID
func (r *Repository) GetBoard(ctx context.Context, id string) (*models.Board, error) {
board := &models.Board{}
err := r.db.QueryRowContext(ctx, `
SELECT id, workspace_id, name, description, created_at, updated_at
FROM boards WHERE id = ?
`, id).Scan(&board.ID, &board.WorkspaceID, &board.Name, &board.Description, &board.CreatedAt, &board.UpdatedAt)
if err == sql.ErrNoRows {
return nil, fmt.Errorf("board not found: %s", id)
}
return board, err
}
// UpdateBoard updates an existing board
func (r *Repository) UpdateBoard(ctx context.Context, board *models.Board) error {
board.UpdatedAt = time.Now().UTC()
result, err := r.db.ExecContext(ctx, `
UPDATE boards SET name = ?, description = ?, updated_at = ? WHERE id = ?
`, board.Name, board.Description, board.UpdatedAt, board.ID)
if err != nil {
return err
}
rows, _ := result.RowsAffected()
if rows == 0 {
return fmt.Errorf("board not found: %s", board.ID)
}
return nil
}
// DeleteBoard deletes a board by ID
func (r *Repository) DeleteBoard(ctx context.Context, id string) error {
result, err := r.db.ExecContext(ctx, `DELETE FROM boards WHERE id = ?`, id)
if err != nil {
return err
}
rows, _ := result.RowsAffected()
if rows == 0 {
return fmt.Errorf("board not found: %s", id)
}
return nil
}
// ListBoards returns all boards
func (r *Repository) ListBoards(ctx context.Context, workspaceID string) ([]*models.Board, error) {
query := `
SELECT id, workspace_id, name, description, created_at, updated_at FROM boards
`
var args []interface{}
if workspaceID != "" {
query += " WHERE workspace_id = ?"
args = append(args, workspaceID)
}
query += " ORDER BY created_at DESC"
rows, err := r.db.QueryContext(ctx, query, args...)
if err != nil {
return nil, err
}
defer func() { _ = rows.Close() }()
var result []*models.Board
for rows.Next() {
board := &models.Board{}
err := rows.Scan(&board.ID, &board.WorkspaceID, &board.Name, &board.Description, &board.CreatedAt, &board.UpdatedAt)
if err != nil {
return nil, err
}
result = append(result, board)
}
return result, rows.Err()
}
// Column operations
// CreateColumn creates a new column
func (r *Repository) CreateColumn(ctx context.Context, column *models.Column) error {
if column.ID == "" {
column.ID = uuid.New().String()
}
now := time.Now().UTC()
column.CreatedAt = now
column.UpdatedAt = now
_, err := r.db.ExecContext(ctx, `
INSERT INTO columns (id, board_id, name, position, state, color, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
`, column.ID, column.BoardID, column.Name, column.Position, column.State, column.Color, column.CreatedAt, column.UpdatedAt)
return err
}
// GetColumn retrieves a column by ID
func (r *Repository) GetColumn(ctx context.Context, id string) (*models.Column, error) {
column := &models.Column{}
err := r.db.QueryRowContext(ctx, `
SELECT id, board_id, name, position, state, color, created_at, updated_at
FROM columns WHERE id = ?
`, id).Scan(&column.ID, &column.BoardID, &column.Name, &column.Position, &column.State, &column.Color, &column.CreatedAt, &column.UpdatedAt)
if err == sql.ErrNoRows {
return nil, fmt.Errorf("column not found: %s", id)
}
return column, err
}
// GetColumnByState retrieves a column by board ID and state
func (r *Repository) GetColumnByState(ctx context.Context, boardID string, state v1.TaskState) (*models.Column, error) {
column := &models.Column{}
err := r.db.QueryRowContext(ctx, `
SELECT id, board_id, name, position, state, color, created_at, updated_at
FROM columns WHERE board_id = ? AND state = ?
`, boardID, state).Scan(&column.ID, &column.BoardID, &column.Name, &column.Position, &column.State, &column.Color, &column.CreatedAt, &column.UpdatedAt)
if err == sql.ErrNoRows {
return nil, fmt.Errorf("column not found for board %s with state %s", boardID, state)
}
return column, err
}
// UpdateColumn updates an existing column
func (r *Repository) UpdateColumn(ctx context.Context, column *models.Column) error {
column.UpdatedAt = time.Now().UTC()
result, err := r.db.ExecContext(ctx, `
UPDATE columns SET name = ?, position = ?, state = ?, color = ?, updated_at = ? WHERE id = ?
`, column.Name, column.Position, column.State, column.Color, column.UpdatedAt, column.ID)
if err != nil {
return err
}
rows, _ := result.RowsAffected()
if rows == 0 {
return fmt.Errorf("column not found: %s", column.ID)
}
return nil
}
// DeleteColumn deletes a column by ID
func (r *Repository) DeleteColumn(ctx context.Context, id string) error {
result, err := r.db.ExecContext(ctx, `DELETE FROM columns WHERE id = ?`, id)
if err != nil {
return err
}
rows, _ := result.RowsAffected()
if rows == 0 {
return fmt.Errorf("column not found: %s", id)
}
return nil
}
// ListColumns returns all columns for a board
func (r *Repository) ListColumns(ctx context.Context, boardID string) ([]*models.Column, error) {
rows, err := r.db.QueryContext(ctx, `
SELECT id, board_id, name, position, state, color, created_at, updated_at
FROM columns WHERE board_id = ? ORDER BY position
`, boardID)
if err != nil {
return nil, err
}
defer func() { _ = rows.Close() }()
var result []*models.Column
for rows.Next() {
column := &models.Column{}
err := rows.Scan(&column.ID, &column.BoardID, &column.Name, &column.Position, &column.State, &column.Color, &column.CreatedAt, &column.UpdatedAt)
if err != nil {
return nil, err
}
result = append(result, column)
}
return result, rows.Err()
}