86 lines
2.2 KiB
Go
86 lines
2.2 KiB
Go
package turso
|
|
|
|
import (
|
|
"database/sql"
|
|
"log"
|
|
|
|
"trankilou.fr/lassistanoque/backend/internal/adapter/database/orm"
|
|
"trankilou.fr/lassistanoque/backend/internal/domain"
|
|
)
|
|
|
|
type TursoTaskRepository struct {
|
|
db *sql.DB
|
|
taskTable orm.Table[domain.Task]
|
|
}
|
|
|
|
func NewTursoTaskRepository(db *sql.DB) *TursoTaskRepository {
|
|
dialect := orm.NewSqliteDialect()
|
|
taskTable, err := orm.NewTable[domain.Task](db, dialect, "tasks")
|
|
if err != nil {
|
|
log.Fatalf("error creating task table")
|
|
}
|
|
|
|
return &TursoTaskRepository{
|
|
db: db,
|
|
taskTable: taskTable,
|
|
}
|
|
}
|
|
|
|
func (r *TursoTaskRepository) ListTasks(userID string, teamID string) ([]*domain.Task, error) {
|
|
return r.taskTable.Select(
|
|
orm.WithWhere(
|
|
"team_id=$1 and team_id in (select team_id from user_teams where user_id=$2)",
|
|
teamID,
|
|
userID,
|
|
),
|
|
orm.WithOrder("_date_created desc"),
|
|
)
|
|
}
|
|
|
|
func (r *TursoTaskRepository) ListActiveTasks() ([]*domain.Task, error) {
|
|
return r.taskTable.Select(
|
|
orm.WithWhere("status=$1", domain.TaskStatusActive),
|
|
)
|
|
}
|
|
|
|
func (r *TursoTaskRepository) GetTaskByID(userID string, teamID string, id string) (*domain.Task, error) {
|
|
return r.taskTable.SelectOne(
|
|
"id=$1 and team_id=$2 and team_id in (select team_id from user_teams where user_id=$3)",
|
|
id,
|
|
teamID,
|
|
userID,
|
|
)
|
|
}
|
|
|
|
func (r *TursoTaskRepository) CreateTask(task *domain.Task) (*domain.Task, error) {
|
|
return r.taskTable.Insert(task)
|
|
}
|
|
|
|
func (r *TursoTaskRepository) UpdateTask(task *domain.Task) (*domain.Task, error) {
|
|
return r.taskTable.Update(task)
|
|
}
|
|
|
|
func (r *TursoTaskRepository) DeleteTask(userID string, teamID string, id string) error {
|
|
return r.taskTable.DeleteWhere(
|
|
"id=$1 and team_id=$2 and team_id in (select team_id from user_teams where user_id=$3)",
|
|
id,
|
|
teamID,
|
|
userID,
|
|
)
|
|
}
|
|
|
|
// FirstUserOfTeam retourne le premier utilisateur d'une équipe. Les tâches
|
|
// planifiées ne portent pas d'utilisateur : c'est lui qui porte la conversation
|
|
// générée lors de l'exécution.
|
|
func (r *TursoTaskRepository) FirstUserOfTeam(teamID string) (string, error) {
|
|
var userID string
|
|
err := r.db.QueryRow(
|
|
"select user_id from user_teams where team_id=$1 order by _date_created asc limit 1",
|
|
teamID,
|
|
).Scan(&userID)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return userID, nil
|
|
}
|