kiwimix/pkg/calendarops/merge.go

50 lines
1.4 KiB
Go

package calendarops
import (
basetypes "code.apps.glenux.net/glenux/kiwimix/pkg/basetypes"
)
// MergeCalendarOperation fusionne les événements qui se chevauchent dans un calendrier.
type MergeCalendarOperation struct {
}
// Ensure FilterCalendarOperation implements CalendarOperation
var _ CalendarOperation = (*MergeCalendarOperation)(nil)
// NewMergeCalendarOperation crée une nouvelle instance de MergeCalendarOperation.
func NewMergeCalendarOperation() *MergeCalendarOperation {
return &MergeCalendarOperation{}
}
// Execute réalise l'opération de fusion.
func (op *MergeCalendarOperation) Execute(cal ...*basetypes.Calendar) (*basetypes.Calendar, error) {
// Guard: exit if no calendar provided
if len(cal) == 0 {
return nil, nil
}
mergedEvents := cal[0].GetEvents()
/*
events := cal.GetEvents()
sort.Slice(cal.Event, func(i, j int) bool {
return cal.Event[i].Start.Before(cal.Event[j].Start)
})
mergedEvents := []basetypes.Event{cal.Event[0]}
for _, event := range cal.Event[1:] {
lastEvent := &mergedEvents[len(mergedEvents)-1]
if event.Start.Before(lastEvent.End) || event.Start.Equal(lastEvent.End) {
if event.End.After(lastEvent.End) {
lastEvent.End = event.End
}
} else {
mergedEvents = append(mergedEvents, event)
}
}
*/
mergedCalendar := &basetypes.Calendar{}
mergedCalendar.AddEvents(mergedEvents)
return mergedCalendar, nil
}