forked from chrissnell/chickenlittle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notification_plan.go
77 lines (61 loc) · 1.7 KB
/
notification_plan.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
package main
import (
"encoding/json"
"fmt"
"time"
"github.com/twinj/uuid"
)
type (
Method uint8
)
type NotificationStep struct {
Method string `json:"method"`
NotifyEveryPeriod time.Duration `json:"notify_every_period"`
NotifyUntilPeriod time.Duration `json:"notify_until_period"`
}
type NotificationPlan struct {
ID uuid.UUID
Username string `json:"username"`
Steps []NotificationStep `json:"steps,omitempty"`
}
func (np *NotificationPlan) Marshal() ([]byte, error) {
jnp, err := json.Marshal(np)
return jnp, err
}
func (np *NotificationPlan) Unmarshal(jnp string) error {
err := json.Unmarshal([]byte(jnp), np)
return err
}
// Fetch a NotificationPlan from the DB
func (c *ChickenLittle) GetNotificationPlan(username string) (*NotificationPlan, error) {
jp, err := c.DB.Fetch("notificationplans", username)
if err != nil {
return nil, fmt.Errorf("Could not fetch notification plan from DB: plan for %v does not exist", username)
}
plan := &NotificationPlan{}
err = plan.Unmarshal(jp)
if err != nil {
return nil, fmt.Errorf("Could not unmarshal notification plan from DB. Err: %v JSON: %v", err, jp)
}
return plan, nil
}
// Store a NotificationPlan in the DB
func (c *ChickenLittle) StoreNotificationPlan(p *NotificationPlan) error {
jp, err := p.Marshal()
if err != nil {
return fmt.Errorf("Could not marshal person %+v", p)
}
err = c.DB.Store("notificationplans", p.Username, string(jp))
if err != nil {
return err
}
return nil
}
// Delete a NotificationPlan from the DB
func (c *ChickenLittle) DeleteNotificationPlan(username string) error {
err := c.DB.Delete("notificationplans", username)
if err != nil {
return err
}
return nil
}