diff --git a/gateway/services/notifications.go b/gateway/services/notifications.go index 710e803a..5dbee3de 100644 --- a/gateway/services/notifications.go +++ b/gateway/services/notifications.go @@ -5,14 +5,27 @@ import ( "github.com/HackIllinois/api/gateway/middleware" "github.com/HackIllinois/api/gateway/models" + "net/http" + "github.com/arbor-dev/arbor" "github.com/justinas/alice" - "net/http" ) const NotificationsFormat string = "JSON" var NotificationsRoutes = arbor.RouteCollection{ + arbor.Route{ + "GetUserSubscriptions", + "GET", + "/notifications/subscriptions/", + alice.New(middleware.IdentificationMiddleware, middleware.AuthMiddleware([]models.Role{models.UserRole})).ThenFunc(GetUserSubscriptions).ServeHTTP, + }, + arbor.Route{ + "GetUserRegisteredDevices", + "GET", + "/notifications/devices/", + alice.New(middleware.IdentificationMiddleware, middleware.AuthMiddleware([]models.Role{models.UserRole})).ThenFunc(GetUserRegisteredDevices).ServeHTTP, + }, arbor.Route{ "GetAllTopics", "GET", @@ -81,6 +94,14 @@ var NotificationsRoutes = arbor.RouteCollection{ }, } +func GetUserSubscriptions(w http.ResponseWriter, r *http.Request) { + arbor.GET(w, config.NOTIFICATIONS_SERVICE+r.URL.String(), NotificationsFormat, "", r) +} + +func GetUserRegisteredDevices(w http.ResponseWriter, r *http.Request) { + arbor.GET(w, config.NOTIFICATIONS_SERVICE+r.URL.String(), NotificationsFormat, "", r) +} + func GetAllTopics(w http.ResponseWriter, r *http.Request) { arbor.GET(w, config.NOTIFICATIONS_SERVICE+r.URL.String(), NotificationsFormat, "", r) } diff --git a/services/notifications/controller/controller.go b/services/notifications/controller/controller.go index 0fa72150..410e87e8 100644 --- a/services/notifications/controller/controller.go +++ b/services/notifications/controller/controller.go @@ -2,13 +2,14 @@ package controller import ( "encoding/json" + "net/http" + "time" + "github.com/HackIllinois/api/common/errors" "github.com/HackIllinois/api/common/utils" "github.com/HackIllinois/api/services/notifications/models" "github.com/HackIllinois/api/services/notifications/service" "github.com/gorilla/mux" - "net/http" - "time" ) func SetupController(route *mux.Route) { @@ -27,6 +28,44 @@ func SetupController(route *mux.Route) { router.HandleFunc("/order/{id}/", GetNotificationOrder).Methods("GET") } +/* + Returns all topics a user is subscribed to +*/ +func GetUserSubscriptions(w http.ResponseWriter, r *http.Request) { + id := r.Header.Get("HackIllinois-Identity") + topics, err := service.GetSubscriptions(id) + + if err != nil { + errors.WriteError(w, r, errors.DatabaseError(err.Error(), "Could not retrieve user's subscriptions.")) + return + } + + topic_list := models.TopicList{ + Topics: topics, + } + + json.NewEncoder(w).Encode(topic_list) +} + +/* + Returns devices registered of a specific user +*/ +func GetUserRegisteredDevices(w http.ResponseWriter, r *http.Request) { + id := r.Header.Get("HackIllinois-Identity") + devices, err := service.GetUserDevices(id) + + if err != nil { + errors.WriteError(w, r, errors.DatabaseError(err.Error(), "Could not retrieve user's registered devices.")) + return + } + + device_list := models.DeviceList{ + Devices: devices, + } + + json.NewEncoder(w).Encode(device_list) +} + /* Returns all topics that notifications can be published to */