planner/item/task.go

112 lines
2.0 KiB
Go
Raw Permalink Normal View History

2024-09-20 07:09:30 +02:00
package item
import (
"encoding/json"
"fmt"
"time"
2024-12-19 12:06:03 +01:00
"github.com/google/go-cmp/cmp"
2024-09-20 07:09:30 +02:00
)
2024-12-24 08:00:23 +01:00
type TaskBody struct {
2024-09-26 07:21:48 +02:00
Title string `json:"title"`
2024-12-31 08:37:00 +01:00
Project string `json:"project"`
2024-12-19 12:06:03 +01:00
Time Time `json:"time"`
2024-09-26 07:21:48 +02:00
Duration time.Duration `json:"duration"`
2024-09-20 07:09:30 +02:00
}
2024-12-24 08:00:23 +01:00
func (e TaskBody) MarshalJSON() ([]byte, error) {
type Alias TaskBody
2024-09-20 07:09:30 +02:00
return json.Marshal(&struct {
2024-09-26 07:21:48 +02:00
Duration string `json:"duration"`
2024-09-20 07:09:30 +02:00
*Alias
}{
2024-09-26 07:21:48 +02:00
Duration: e.Duration.String(),
Alias: (*Alias)(&e),
2024-09-20 07:09:30 +02:00
})
}
2024-12-24 08:00:23 +01:00
func (e *TaskBody) UnmarshalJSON(data []byte) error {
type Alias TaskBody
2024-09-26 07:21:48 +02:00
aux := &struct {
Duration string `json:"duration"`
*Alias
}{
Alias: (*Alias)(e),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
var err error
if e.Duration, err = time.ParseDuration(aux.Duration); err != nil {
return err
}
return nil
}
2024-12-24 08:00:23 +01:00
type Task struct {
2024-12-19 12:06:03 +01:00
ID string `json:"id"`
Date Date `json:"date"`
Recurrer Recurrer `json:"recurrer"`
RecurNext Date `json:"recurNext"`
2024-12-24 08:00:23 +01:00
TaskBody
2024-09-20 07:09:30 +02:00
}
2024-12-24 08:00:23 +01:00
func NewTask(i Item) (Task, error) {
if i.Kind != KindTask {
return Task{}, fmt.Errorf("item is not an task")
2024-09-20 07:09:30 +02:00
}
2024-12-24 08:00:23 +01:00
var t Task
if err := json.Unmarshal([]byte(i.Body), &t); err != nil {
return Task{}, fmt.Errorf("could not unmarshal item body: %v", err)
2024-09-20 07:09:30 +02:00
}
2024-12-24 08:00:23 +01:00
t.ID = i.ID
t.Date = i.Date
t.Recurrer = i.Recurrer
t.RecurNext = i.RecurNext
2024-09-20 07:09:30 +02:00
2024-12-24 08:00:23 +01:00
return t, nil
2024-09-20 07:09:30 +02:00
}
2024-12-24 08:00:23 +01:00
func (t Task) Item() (Item, error) {
body, err := json.Marshal(t.TaskBody)
2024-09-20 07:09:30 +02:00
if err != nil {
2024-12-24 08:00:23 +01:00
return Item{}, fmt.Errorf("could not marshal task body to json")
2024-09-20 07:09:30 +02:00
}
return Item{
2024-12-24 08:00:23 +01:00
ID: t.ID,
Kind: KindTask,
Date: t.Date,
Recurrer: t.Recurrer,
RecurNext: t.RecurNext,
2024-12-01 10:22:47 +01:00
Body: string(body),
2024-09-20 07:09:30 +02:00
}, nil
}
2024-10-29 07:22:04 +01:00
2024-12-24 08:00:23 +01:00
func (t Task) Valid() bool {
if t.Title == "" {
2024-10-29 07:22:04 +01:00
return false
}
return true
}
2024-12-19 12:06:03 +01:00
2024-12-24 08:00:23 +01:00
func TaskDiff(a, b Task) string {
2024-12-19 12:06:03 +01:00
aJSON, _ := json.Marshal(a)
bJSON, _ := json.Marshal(b)
return cmp.Diff(string(aJSON), string(bJSON))
}
2024-12-24 08:00:23 +01:00
func TaskDiffs(a, b []Task) string {
2024-12-19 12:06:03 +01:00
aJSON, _ := json.Marshal(a)
bJSON, _ := json.Marshal(b)
return cmp.Diff(string(aJSON), string(bJSON))
}