-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapplication.go
165 lines (146 loc) · 5.95 KB
/
application.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
158
159
160
161
162
163
164
165
package application
import (
"github.com/carbocation/interpose"
gorilla_mux "github.com/gorilla/mux"
"github.com/gorilla/sessions"
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
"github.com/sendgrid/sendgrid-go"
"github.com/sendgrid/sendgrid-go/helpers/mail"
"github.com/spf13/viper"
"net/http"
"os"
"time"
"github.com/jpatrickpark/server1/handlers"
"github.com/jpatrickpark/server1/middlewares"
"github.com/jpatrickpark/server1/models"
)
func ReadableStatus(newStatus int) string {
switch newStatus {
case models.FULL: // = 0
return "is full"
case models.OPEN: // = 1
return "is open"
case models.WAITLIST: // = 2
return "has open waitlist"
case models.NONEXISTENT: // = 3
return "does not exist"
case models.DELETED: // = 4
return "is successfully deleted"
case models.ENTRYEXISTS: // = 5
return "is already recorded"
case models.NOTDELETED: // = 6
return "is not deleted"
case models.NEWONLY_FULL: // = 7
return "is only available for new students"
case models.NEWONLY_WAITLIST: // = 8
return "has an open waitlist for current students"
default:
return "encountered an unknown error"
}
}
func SendCourseOpenEmail(courseCode, quarter, email string, newStatus int) {
stringStatus := ReadableStatus(newStatus)
from := mail.NewEmail("My UCI Class Is Full", "myuciclassisfull@gmail.com")
to := mail.NewEmail(email, email)
title := "Your course " + courseCode + " " + stringStatus + "!"
content := "<p>Your course " + courseCode + " for " + handlers.ReadableQuarter(quarter) + " quarter " + stringStatus + "!</p><p>Go ahead and enroll in now on <a href='https://www.reg.uci.edu'>Webreg</a>!</p>"
newContent := mail.NewContent("text/html", content)
message := mail.NewV3MailInit(from, title, to, newContent)
message.AddCategories("CourseAlert")
request := sendgrid.GetRequest(os.Getenv("SENDGRID_API_KEY"), "/v3/mail/send", "https://api.sendgrid.com")
request.Method = "POST"
request.Body = mail.GetRequestBody(message)
// I should think more about what to do when the email fails to send.
sendgrid.API(request)
}
func SendToAccordingUsers(db *sqlx.DB, courseId int64, courseCode, quarter string, newStatus int) {
if newStatus != models.OPEN && newStatus != models.WAITLIST && newStatus != models.NEWONLY_WAITLIST {
return
}
pair := models.NewUserCoursePair(db)
userStruct := models.NewUser(db)
pairs, err1 := pair.GetPairsByCourseId(nil, courseId)
if err1 == nil {
for _, item := range *pairs {
user, err2 := userStruct.GetById(nil, item.UserID)
if err2 == nil {
SendCourseOpenEmail(courseCode, quarter, user.Email, newStatus)
}
}
}
}
func My_uci_class_is_full(db *sqlx.DB) {
for {
course := models.NewCourse(db)
courses, err := course.AllCourses(nil)
if err == nil {
now := time.Now()
//now = time.Date(2016, time.March, 10, 23, 0, 0, 0, time.UTC)
possibleQuarters := handlers.PossibleQuarters(now)
for _, item := range courses {
if handlers.Contains(possibleQuarters, item.Quarter) {
newStatus := handlers.CourseStatus(item.Quarter, item.CourseCode)
if item.Status != newStatus {
course.UpdateCourse(nil, item.ID, newStatus)
if (item.Status == models.FULL || item.Status == models.NEWONLY_FULL) && (newStatus == models.OPEN || newStatus == models.WAITLIST || newStatus == models.NEWONLY_WAITLIST) {
go SendToAccordingUsers(db, item.ID, item.CourseCode, item.Quarter, newStatus)
}
}
}
}
}
time.Sleep(time.Minute)
}
}
// New is the constructor for Application struct.
func New(config *viper.Viper) (*Application, error) {
dsn := config.Get("dsn").(string)
db, err := sqlx.Connect("postgres", dsn)
if err != nil {
return nil, err
}
cookieStoreSecret := config.Get("cookie_secret").(string)
app := &Application{}
app.config = config
app.dsn = dsn
app.db = db
app.sessionStore = sessions.NewCookieStore([]byte(cookieStoreSecret))
return app, err
}
// Application is the application object that runs HTTP server.
type Application struct {
config *viper.Viper
dsn string
db *sqlx.DB
sessionStore sessions.Store
}
func (app *Application) MiddlewareStruct() (*interpose.Middleware, error) {
middle := interpose.New()
middle.Use(middlewares.SetDB(app.db))
middle.Use(middlewares.SetSessionStore(app.sessionStore))
middle.UseHandler(app.mux())
return middle, nil
}
func (app *Application) mux() *gorilla_mux.Router {
MustLogin := middlewares.MustLogin
router := gorilla_mux.NewRouter()
router.Handle("/search-golang", MustLogin(http.HandlerFunc(handlers.GetHome))).Methods("GET")
router.Handle("/my-uci-class-is-full", MustLogin(http.HandlerFunc(handlers.GetUciClass))).Methods("GET")
router.Handle("/whiteboard", MustLogin(http.HandlerFunc(handlers.GetWhiteboardHome))).Methods("GET")
router.HandleFunc("/signup", handlers.GetSignup).Methods("GET")
router.HandleFunc("/signup", handlers.PostSignup).Methods("POST")
router.HandleFunc("/login", handlers.GetLogin).Methods("GET")
router.HandleFunc("/login", handlers.PostLogin).Methods("POST")
router.HandleFunc("/logout", handlers.GetLogout).Methods("GET")
router.HandleFunc("/search-golang/intersectRepo", handlers.PostIntersectRepo).Methods("Post")
router.HandleFunc("/search-golang/intersectHuman", handlers.PostIntersectHuman).Methods("Post")
router.HandleFunc("/search-golang/search", handlers.GetSearch).Methods("GET")
router.Handle("/my-uci-class-is-full/term/{quarter}", MustLogin(http.HandlerFunc(handlers.PutTerm))).Methods("PUT")
router.Handle("/my-uci-class-is-full/term/{quarter}/{courseCode}", MustLogin(http.HandlerFunc(handlers.DeleteTerm))).Methods("DELETE")
router.Handle("/my-uci-class-is-full/term/{quarter}", MustLogin(http.HandlerFunc(handlers.GetTerm))).Methods("GET")
router.Handle("/users/{id:[0-9]+}", MustLogin(http.HandlerFunc(handlers.PostPutDeleteUsersID))).Methods("POST", "PUT", "DELETE")
// Path of static files must be last!
router.PathPrefix("/").Handler(http.FileServer(http.Dir("static")))
return router
}