73 lines
1.8 KiB
Go
73 lines
1.8 KiB
Go
package turso
|
|
|
|
import (
|
|
"database/sql"
|
|
"log"
|
|
|
|
"trankilou.fr/lassistanoque/backend/internal/adapter/database/orm"
|
|
"trankilou.fr/lassistanoque/backend/internal/domain"
|
|
)
|
|
|
|
type TursoToolRepository struct {
|
|
db *sql.DB
|
|
ToolTable orm.Table[domain.Tool]
|
|
}
|
|
|
|
func NewTursoToolRepository(db *sql.DB) *TursoToolRepository {
|
|
dialect := orm.NewSqliteDialect()
|
|
ToolTable, err := orm.NewTable[domain.Tool](db, dialect, "tools")
|
|
if err != nil {
|
|
log.Fatalf("error creating lasebuche Tool table")
|
|
}
|
|
|
|
return &TursoToolRepository{
|
|
db: db,
|
|
ToolTable: ToolTable,
|
|
}
|
|
}
|
|
|
|
func (r *TursoToolRepository) ListTools(userID string, teamID string) ([]*domain.Tool, error) {
|
|
return r.ToolTable.Select(
|
|
orm.WithWhere(
|
|
"team_id=$1 and team_id in (select team_id from user_teams where user_id=$2)",
|
|
teamID,
|
|
userID,
|
|
),
|
|
)
|
|
}
|
|
|
|
func (r *TursoToolRepository) GetToolByName(userID string, teamID string, name string) (*domain.Tool, error) {
|
|
return r.ToolTable.SelectOne(
|
|
"name=$1 and team_id=$2 and team_id in (select team_id from user_teams where user_id=$3)",
|
|
name,
|
|
teamID,
|
|
userID,
|
|
)
|
|
}
|
|
|
|
func (r *TursoToolRepository) GetToolByID(userID string, teamID string, id string) (*domain.Tool, error) {
|
|
return r.ToolTable.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 *TursoToolRepository) CreateTool(userID string, Tool *domain.Tool) (*domain.Tool, error) {
|
|
return r.ToolTable.Insert(Tool)
|
|
}
|
|
|
|
func (r *TursoToolRepository) UpdateTool(userID string, Tool *domain.Tool) (*domain.Tool, error) {
|
|
return r.ToolTable.Update(Tool)
|
|
}
|
|
|
|
func (r *TursoToolRepository) DeleteTool(userID string, teamID string, name string) error {
|
|
return r.ToolTable.DeleteWhere(
|
|
"name=$1 and team_id=$2 and team_id in (select team_id from user_teams where user_id=$3)",
|
|
name,
|
|
teamID,
|
|
userID,
|
|
)
|
|
}
|