planner/item/event.go

51 lines
833 B
Go
Raw Normal View History

2024-09-20 07:54:56 +02:00
package item
import (
"encoding/json"
"fmt"
"time"
)
type EventBody struct {
Title string `json:"title"`
Start time.Time `json:"start"`
End time.Time `json:"end"`
}
type Event struct {
ID string `json:"id"`
EventBody
}
func NewEvent(i Item) (Event, error) {
if i.Kind != KindEvent {
return Event{}, fmt.Errorf("item is not an event")
}
var e Event
if err := json.Unmarshal([]byte(i.Body), &e); err != nil {
return Event{}, fmt.Errorf("could not unmarshal item body: %v", err)
}
e.ID = i.ID
return e, nil
}
func (e Event) Item() (Item, error) {
body, err := json.Marshal(EventBody{
Title: e.Title,
Start: e.Start,
End: e.End,
})
if err != nil {
return Item{}, fmt.Errorf("could not marshal event to json")
}
return Item{
ID: e.ID,
Kind: KindEvent,
Body: string(body),
}, nil
}