-
Notifications
You must be signed in to change notification settings - Fork 0
/
coursesense.go
91 lines (74 loc) · 2.09 KB
/
coursesense.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
package coursesense
import (
"context"
"errors"
"fmt"
)
// Domain types are defined in this file
type Course struct {
Department string `json:"department"`
Code int `json:"code"`
}
func (c Course) Valid() error {
if c.Department == "" {
return errors.New("Department cannot be empty")
}
if c.Code <= 999 && c.Code >= 9999 {
return errors.New("Course code must be 4 digits")
}
return nil
}
type Section struct {
Course Course `json:"course"`
Code string `json:"code"`
Term string `json:"term"`
}
func (s Section) Valid() error {
if s.Code == "" {
return errors.New("Section code cannot be empty")
}
if s.Term == "" {
return errors.New("Term cannot be empty")
}
return s.Course.Valid()
}
func (s Section) String() string {
return fmt.Sprintf("%s*%d*%s*%s", s.Course.Department, s.Course.Code, s.Code, s.Term)
}
// Service that gets information on course sections
type SectionService interface {
Exists(context.Context, Section) (bool, error)
GetAvailableSeats(context.Context, Section) (uint, error)
}
// A user registered for notifications on a Section
type Watcher struct {
Email string `json:"email"`
Phone string `json:"phone"`
}
func (w Watcher) Valid() error {
if w.Email == "" && w.Phone == "" {
return errors.New("At least one contact method needs to be present")
}
return nil
}
func (w Watcher) String() string {
return fmt.Sprintf("%s:%s", w.Email, w.Phone)
}
// Service that persists watched sections
type Repository interface {
AddWatcher(context.Context, Section, Watcher) error
GetWatchedSections(context.Context) ([]Section, error)
GetWatchers(context.Context, Section) ([]Watcher, error)
// This function removes a section and its watchers. It will also remove the associated course if no other sections reference it
Cleanup(context.Context, Section) error
}
// A type that sends can send notifications to Watchers
type Notifier interface {
Notify(context.Context, Section, ...Watcher) error
}
type TriggerService interface {
Trigger(context.Context) error
}
type RegistrationService interface {
Register(context.Context, Section, Watcher) error
}