2024-10-07 09:34:17 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2025-01-05 12:25:55 +01:00
|
|
|
"slices"
|
2024-10-29 07:22:04 +01:00
|
|
|
"strconv"
|
2025-01-13 09:13:48 +01:00
|
|
|
|
|
|
|
"go-mod.ewintr.nl/planner/sync/client"
|
2024-10-07 09:34:17 +02:00
|
|
|
)
|
|
|
|
|
2024-12-27 11:20:32 +01:00
|
|
|
type DeleteArgs struct {
|
|
|
|
LocalID int
|
2024-10-07 09:34:17 +02:00
|
|
|
}
|
|
|
|
|
2024-12-27 11:20:32 +01:00
|
|
|
func NewDeleteArgs() DeleteArgs {
|
|
|
|
return DeleteArgs{}
|
2024-10-07 09:34:17 +02:00
|
|
|
}
|
|
|
|
|
2024-12-27 11:20:32 +01:00
|
|
|
func (da DeleteArgs) Parse(main []string, flags map[string]string) (Command, error) {
|
2025-01-05 12:27:59 +01:00
|
|
|
if len(main) != 2 {
|
|
|
|
return nil, ErrWrongCommand
|
|
|
|
}
|
|
|
|
aliases := []string{"d", "delete", "done"}
|
|
|
|
var localIDStr string
|
|
|
|
switch {
|
|
|
|
case slices.Contains(aliases, main[0]):
|
|
|
|
localIDStr = main[1]
|
|
|
|
case slices.Contains(aliases, main[1]):
|
|
|
|
localIDStr = main[0]
|
|
|
|
default:
|
2024-12-27 11:20:32 +01:00
|
|
|
return nil, ErrWrongCommand
|
2024-10-29 07:22:04 +01:00
|
|
|
}
|
2024-12-27 11:20:32 +01:00
|
|
|
|
2025-01-05 12:27:59 +01:00
|
|
|
localID, err := strconv.Atoi(localIDStr)
|
2024-10-29 07:22:04 +01:00
|
|
|
if err != nil {
|
2024-12-27 11:20:32 +01:00
|
|
|
return nil, fmt.Errorf("not a local id: %v", main[1])
|
2024-10-29 07:22:04 +01:00
|
|
|
}
|
|
|
|
|
2024-12-27 11:20:32 +01:00
|
|
|
return &Delete{
|
2024-12-29 09:32:49 +01:00
|
|
|
Args: DeleteArgs{
|
2024-12-27 11:20:32 +01:00
|
|
|
LocalID: localID,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type Delete struct {
|
2024-12-29 09:32:49 +01:00
|
|
|
Args DeleteArgs
|
2024-10-29 07:22:04 +01:00
|
|
|
}
|
|
|
|
|
2025-01-13 09:13:48 +01:00
|
|
|
func (del Delete) Do(repos Repositories, _ client.Client) (CommandResult, error) {
|
|
|
|
tx, err := repos.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not start transaction: %v", err)
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
2024-10-07 09:34:17 +02:00
|
|
|
var id string
|
2025-01-13 09:13:48 +01:00
|
|
|
idMap, err := repos.LocalID(tx).FindAll()
|
2024-10-07 09:34:17 +02:00
|
|
|
if err != nil {
|
2024-12-29 09:32:49 +01:00
|
|
|
return nil, fmt.Errorf("could not get local ids: %v", err)
|
2024-10-07 09:34:17 +02:00
|
|
|
}
|
2024-12-24 08:00:23 +01:00
|
|
|
for tskID, lid := range idMap {
|
2024-12-29 09:32:49 +01:00
|
|
|
if del.Args.LocalID == lid {
|
2024-12-24 08:00:23 +01:00
|
|
|
id = tskID
|
2024-10-07 09:34:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if id == "" {
|
2024-12-29 09:32:49 +01:00
|
|
|
return nil, fmt.Errorf("could not find local id")
|
2024-10-07 09:34:17 +02:00
|
|
|
}
|
|
|
|
|
2025-01-13 09:13:48 +01:00
|
|
|
tsk, err := repos.Task(tx).FindOne(id)
|
2024-10-07 11:11:18 +02:00
|
|
|
if err != nil {
|
2024-12-29 09:32:49 +01:00
|
|
|
return nil, fmt.Errorf("could not get task: %v", err)
|
2024-10-07 11:11:18 +02:00
|
|
|
}
|
|
|
|
|
2024-12-24 08:00:23 +01:00
|
|
|
it, err := tsk.Item()
|
2024-10-07 11:11:18 +02:00
|
|
|
if err != nil {
|
2024-12-29 09:32:49 +01:00
|
|
|
return nil, fmt.Errorf("could not convert task to sync item: %v", err)
|
2024-10-07 11:11:18 +02:00
|
|
|
}
|
2024-10-29 07:22:04 +01:00
|
|
|
it.Deleted = true
|
2025-01-13 09:13:48 +01:00
|
|
|
if err := repos.Sync(tx).Store(it); err != nil {
|
2024-12-29 09:32:49 +01:00
|
|
|
return nil, fmt.Errorf("could not store sync item: %v", err)
|
2024-10-07 11:11:18 +02:00
|
|
|
}
|
2024-10-29 07:22:04 +01:00
|
|
|
|
2025-01-13 09:13:48 +01:00
|
|
|
if err := repos.LocalID(tx).Delete(id); err != nil {
|
2024-12-29 09:32:49 +01:00
|
|
|
return nil, fmt.Errorf("could not delete local id: %v", err)
|
2024-10-29 07:22:04 +01:00
|
|
|
}
|
|
|
|
|
2025-01-13 09:13:48 +01:00
|
|
|
if err := repos.Task(tx).Delete(id); err != nil {
|
2024-12-29 09:32:49 +01:00
|
|
|
return nil, fmt.Errorf("could not delete task: %v", err)
|
2024-10-29 07:22:04 +01:00
|
|
|
}
|
|
|
|
|
2024-12-31 08:47:39 +01:00
|
|
|
return DeleteResult{}, nil
|
2024-12-29 09:32:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type DeleteResult struct{}
|
|
|
|
|
|
|
|
func (dr DeleteResult) Render() string {
|
2025-01-06 11:26:45 +01:00
|
|
|
return "task removed"
|
2024-10-07 09:34:17 +02:00
|
|
|
}
|