-
Notifications
You must be signed in to change notification settings - Fork 0
/
recurring.go
157 lines (135 loc) · 3.92 KB
/
recurring.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package recurring
import (
"encoding/json"
"log"
"net/http"
"time"
"github.com/teambition/rrule-go"
)
type RecurringInput struct {
// Frequency of the recurring event. Possible values are daily, weekly, monthly, yearly
Frequency string `json:"frequency"`
Until time.Time `json:"until,omitempty"`
// Count number of occurrences to be generated
Count int `json:"count,omitempty"`
WeekDays []string `json:"weekDays,omitempty"`
// Interval between each frequency iteration. For example, if the interval is 2, the frequency of monthly will be every 2 months
Interval int `json:"interval,omitempty"`
Month int `json:"month,omitempty"`
Pos int `json:"pos,omitempty"`
Day int `json:"day,omitempty"`
}
func (ri RecurringInput) MarshalJSON() ([]byte, error) {
type Alias RecurringInput
return json.Marshal(&struct {
Until string `json:"until,omitempty"`
*Alias
}{
Until: ri.Until.Format("2006-01-02"),
Alias: (*Alias)(&ri),
})
}
// RuleGenerator generates a RRule based on RecurringInput
func RuleSetGenerator(input RecurringInput) (rrule.Set, error) {
set := rrule.Set{}
ruleOption := rrule.ROption{}
// Frequency of the recurring event
switch input.Frequency {
case "daily":
ruleOption.Freq = rrule.DAILY
case "weekly":
ruleOption.Freq = rrule.WEEKLY
case "monthly":
ruleOption.Freq = rrule.MONTHLY
case "yearly":
ruleOption.Freq = rrule.YEARLY
default:
// todo: handle error properly
log.Println("Invalid frequency:" + input.Frequency)
}
// Count of the recurring event
if input.Count > 0 {
ruleOption.Count = input.Count
}
// Interval between each frequency iteration
if input.Interval > 0 {
ruleOption.Interval = input.Interval
}
// Until date of the recurring event
if !input.Until.IsZero() {
ruleOption.Until = input.Until
}
// Weekdays of the recurring event
if len(input.WeekDays) > 0 {
ruleOption.Byweekday = make([]rrule.Weekday, len(input.WeekDays))
for i, day := range input.WeekDays {
switch day {
case "MO":
ruleOption.Byweekday[i] = rrule.MO
case "TU":
ruleOption.Byweekday[i] = rrule.TU
case "WE":
ruleOption.Byweekday[i] = rrule.WE
case "TH":
ruleOption.Byweekday[i] = rrule.TH
case "FR":
ruleOption.Byweekday[i] = rrule.FR
case "SA":
ruleOption.Byweekday[i] = rrule.SA
case "SU":
ruleOption.Byweekday[i] = rrule.SU
default:
log.Println("Invalid weekday: " + day)
}
}
}
// Month of the recurring event
if input.Month > 0 {
ruleOption.Bymonth = []int{input.Month}
}
// Position of the recurring event
if input.Pos > 0 {
ruleOption.Bysetpos = []int{input.Pos}
}
// Day of the recurring event
if input.Day > 0 {
ruleOption.Bymonthday = []int{input.Day}
}
r, err := rrule.NewRRule(ruleOption)
if err != nil {
log.Println("Error creating RRule: " + err.Error())
}
set.RRule(r)
return set, nil
}
type RecurringOutput struct {
RRule string `json:"rRule"`
Occurrences []time.Time `json:"occurrences"`
Recurring RecurringInput `json:"recurring"`
}
func RecurringJSON(w http.ResponseWriter, r *http.Request) {
// Check if the request method is POST
if r.Method != http.MethodPost {
http.Error(w, "Only POST requests are allowed", http.StatusMethodNotAllowed)
return
}
// Decode the JSON request body
var input RecurringInput
if err := json.NewDecoder(r.Body).Decode(&input); err != nil {
http.Error(w, "Invalid JSON input", http.StatusBadRequest)
return
}
// Process the JSON input (you can add your logic here)
b, _ := json.Marshal(input)
log.Printf("Received JSON: %s\n", string(b))
// Create a new RRule Builder
set, _ := RuleSetGenerator(input)
recurringOutput := RecurringOutput{
RRule: set.String(),
Occurrences: set.All(),
Recurring: input}
// Send a response (optional)
//response := map[string]string{"message": "JSON received successfully"}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(&recurringOutput)
}