planner/plan/command/show.go

84 lines
1.5 KiB
Go
Raw 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"
2024-12-30 09:37:53 +01:00
"go-mod.ewintr.nl/planner/item"
"go-mod.ewintr.nl/planner/plan/format"
2024-12-29 09:32:49 +01:00
"go-mod.ewintr.nl/planner/plan/storage"
)
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
}
2024-12-30 09:37:53 +01:00
func (s *Show) Do(deps Dependencies) (CommandResult, error) {
2024-12-29 09:32:49 +01:00
id, err := deps.LocalIDRepo.FindOne(s.args.localID)
switch {
case errors.Is(err, storage.ErrNotFound):
2024-12-29 10:16:03 +01:00
return nil, fmt.Errorf("could not find local id")
2024-12-29 09:32:49 +01:00
case err != nil:
2024-12-29 10:16:03 +01:00
return nil, err
2024-12-29 09:32:49 +01:00
}
2024-12-29 11:31:33 +01:00
tsk, err := deps.TaskRepo.FindOne(id)
2024-12-29 09:32:49 +01:00
if err != nil {
2024-12-29 10:16:03 +01:00
return nil, fmt.Errorf("could not find task")
2024-12-29 09:32:49 +01:00
}
2024-12-30 09:37:53 +01:00
return ShowResult{
LocalID: s.args.localID,
Task: tsk,
}, nil
}
type ShowResult struct {
LocalID int
Task item.Task
}
func (sr ShowResult) Render() string {
2024-12-29 09:32:49 +01:00
var recurStr string
2024-12-30 09:37:53 +01:00
if sr.Task.Recurrer != nil {
recurStr = sr.Task.Recurrer.String()
2024-12-29 09:32:49 +01:00
}
data := [][]string{
2024-12-30 09:37:53 +01:00
{"title", sr.Task.Title},
{"local id", fmt.Sprintf("%d", sr.LocalID)},
{"date", sr.Task.Date.String()},
{"time", sr.Task.Time.String()},
{"duration", sr.Task.Duration.String()},
2024-12-29 09:32:49 +01:00
{"recur", recurStr},
2024-12-30 09:37:53 +01:00
// {"id", s.Task.ID},
2024-12-29 09:32:49 +01:00
}
2024-12-30 09:37:53 +01:00
return fmt.Sprintf("\n%s\n", format.Table(data))
2024-12-29 09:32:49 +01:00
}