2024-10-07 09:34:17 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-10-29 07:22:04 +01:00
|
|
|
"strconv"
|
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) {
|
2024-12-31 08:47:39 +01:00
|
|
|
if len(main) < 2 || (main[0] != "delete" && main[0] != "done") {
|
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
|
|
|
|
2024-10-29 07:22:04 +01:00
|
|
|
localID, err := strconv.Atoi(main[1])
|
|
|
|
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-04 11:03:57 +01:00
|
|
|
func (del Delete) Do(deps Dependencies) (CommandResult, error) {
|
2024-10-07 09:34:17 +02:00
|
|
|
var id string
|
2024-12-27 11:20:32 +01:00
|
|
|
idMap, err := deps.LocalIDRepo.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
|
|
|
}
|
|
|
|
|
2024-12-29 09:32:49 +01:00
|
|
|
tsk, err := deps.TaskRepo.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
|
2024-12-27 11:20:32 +01:00
|
|
|
if err := deps.SyncRepo.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
|
|
|
|
2024-12-27 11:20:32 +01:00
|
|
|
if err := deps.LocalIDRepo.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
|
|
|
}
|
|
|
|
|
2024-12-27 11:20:32 +01:00
|
|
|
if err := deps.TaskRepo.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 {
|
|
|
|
return "task deleted"
|
2024-10-07 09:34:17 +02:00
|
|
|
}
|