planner/plan/command/show.go

96 lines
1.8 KiB
Go
Raw Permalink Normal View History

2024-10-29 07:22:04 +01:00
package command
2024-12-29 09:32:49 +01:00
import (
"errors"
"fmt"
"strconv"
"go-mod.ewintr.nl/planner/item"
"go-mod.ewintr.nl/planner/plan/format"
"go-mod.ewintr.nl/planner/plan/storage"
2025-01-13 09:13:48 +01:00
"go-mod.ewintr.nl/planner/sync/client"
2024-12-29 09:32:49 +01:00
)
type ShowArgs struct {
localID int
}
func NewShowArgs() ShowArgs {
return ShowArgs{}
}
func (sa ShowArgs) Parse(main []string, fields map[string]string) (Command, error) {
if len(main) != 1 {
return nil, ErrWrongCommand
}
lid, err := strconv.Atoi(main[0])
if err != nil {
return nil, ErrWrongCommand
}
return &Show{
args: ShowArgs{
localID: lid,
},
}, nil
}
type Show struct {
args ShowArgs
}
2025-01-13 09:13:48 +01:00
func (s Show) 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()
id, err := repos.LocalID(tx).FindOne(s.args.localID)
2024-12-29 09:32:49 +01:00
switch {
case errors.Is(err, storage.ErrNotFound):
return nil, fmt.Errorf("could not find local id")
case err != nil:
return nil, err
}
2025-01-13 09:13:48 +01:00
tsk, err := repos.Task(tx).FindOne(id)
2024-12-29 09:32:49 +01:00
if err != nil {
return nil, fmt.Errorf("could not find task")
}
2025-01-13 09:13:48 +01:00
if err := tx.Commit(); err != nil {
return nil, fmt.Errorf("could not show task: %v", err)
}
2024-12-29 09:32:49 +01:00
return ShowResult{
LocalID: s.args.localID,
Task: tsk,
}, nil
}
type ShowResult struct {
LocalID int
Task item.Task
}
func (sr ShowResult) Render() string {
var recurStr string
if sr.Task.Recurrer != nil {
recurStr = sr.Task.Recurrer.String()
}
data := [][]string{
{"title", sr.Task.Title},
{"local id", fmt.Sprintf("%d", sr.LocalID)},
2024-12-31 08:37:00 +01:00
{"project", sr.Task.Project},
2024-12-29 09:32:49 +01:00
{"date", sr.Task.Date.String()},
{"time", sr.Task.Time.String()},
{"duration", sr.Task.Duration.String()},
{"recur", recurStr},
// {"id", s.Task.ID},
}
return fmt.Sprintf("\n%s\n", format.Table(data))
}