From 0feccaf0ad6ec0667825f6dcab21d86ac488db5b Mon Sep 17 00:00:00 2001 From: aukey2 Date: Wed, 6 May 2020 11:52:58 -0500 Subject: [PATCH 1/5] No compilation errors --- config/dev_config.json | 6 ++ config/test_config.json | 4 + container/env.template | 1 + gateway/config/config.go | 8 ++ gateway/services/event.go | 3 + gateway/services/recognition.go | 36 +++++++ gateway/services/services.go | 2 + log/access.log | 0 main.go | 2 + services/recognition/config/config.go | 41 ++++++++ services/recognition/controller/controller.go | 59 +++++++++++ services/recognition/main.go | 40 ++++++++ services/recognition/models/recognition.go | 10 ++ .../recognition/models/recognition_list.go | 5 + .../service/recognition_service.go | 99 +++++++++++++++++++ 15 files changed, 316 insertions(+) create mode 100644 gateway/services/recognition.go delete mode 100644 log/access.log create mode 100644 services/recognition/config/config.go create mode 100644 services/recognition/controller/controller.go create mode 100644 services/recognition/main.go create mode 100644 services/recognition/models/recognition.go create mode 100644 services/recognition/models/recognition_list.go create mode 100644 services/recognition/service/recognition_service.go diff --git a/config/dev_config.json b/config/dev_config.json index a0bbb7a6..914a1236 100644 --- a/config/dev_config.json +++ b/config/dev_config.json @@ -11,6 +11,7 @@ "STAT_SERVICE": "http://localhost:8011", "NOTIFICATIONS_SERVICE": "http://localhost:8012", "PROJECT_SERVICE": "http://localhost:8013", + "RECOGNITION_SERVICE": "http://localhost:8014", "GATEWAY_PORT": "8000", "AUTH_PORT": ":8002", @@ -25,6 +26,8 @@ "STAT_PORT": ":8011", "NOTIFICATIONS_PORT": ":8012", "PROJECT_PORT": ":8013", + "RECOGNITION_PORT": ":8014", + "AUTH_DB_HOST": "localhost", "USER_DB_HOST": "localhost", @@ -38,6 +41,8 @@ "STAT_DB_HOST": "localhost", "NOTIFICATIONS_DB_HOST": "localhost", "PROJECT_DB_HOST": "localhost", + "RECOGNITION_DB_HOST": "localhost", + "AUTH_DB_NAME": "auth", "USER_DB_NAME": "user", @@ -51,6 +56,7 @@ "STAT_DB_NAME": "stat", "NOTIFICATIONS_DB_NAME": "notifications", "PROJECT_DB_NAME": "project", + "RECOGNITION_DB_NAME": "recognition", "S3_REGION": "us-east-1", "S3_BUCKET": "hackillinois-upload-2019", diff --git a/config/test_config.json b/config/test_config.json index 511f7936..ca001481 100644 --- a/config/test_config.json +++ b/config/test_config.json @@ -11,6 +11,7 @@ "STAT_SERVICE": "http://localhost:8011", "NOTIFICATIONS_SERVICE": "http://localhost:8012", "PROJECT_SERVICE": "http://localhost:8013", + "RECOGNITION_SERVICE": "http://localhost:8014", "GATEWAY_PORT": "8000", "AUTH_PORT": ":8002", @@ -25,6 +26,7 @@ "STAT_PORT": ":8011", "NOTIFICATIONS_PORT": ":8012", "PROJECT_PORT": ":8013", + "RECOGNITION_PORT": ":8014", "AUTH_DB_HOST": "localhost", "USER_DB_HOST": "localhost", @@ -38,6 +40,7 @@ "STAT_DB_HOST": "localhost", "NOTIFICATIONS_DB_HOST": "localhost", "PROJECT_DB_HOST": "localhost", + "RECOGNITION_DB_HOST": "localhost", "AUTH_DB_NAME": "test-auth", "USER_DB_NAME": "test-user", @@ -51,6 +54,7 @@ "STAT_DB_NAME": "test-stat", "NOTIFICATIONS_DB_NAME": "test-notifications", "PROJECT_DB_NAME": "test-project", + "RECOGNITION_DB_NAME": "recognition", "S3_REGION": "us-east-1", "S3_BUCKET": "hackillinois-upload-2019", diff --git a/container/env.template b/container/env.template index d51ba5fd..e21cce05 100644 --- a/container/env.template +++ b/container/env.template @@ -21,6 +21,7 @@ MAIL_DB_HOST= EVENT_DB_HOST= STAT_DB_HOST= NOTIFICATIONS_DB_HOST= +RECOGNITION_DB_HOST= # Set the oauth client id and secret for your GitHub, Google, and Linkedin applications GITHUB_CLIENT_ID= diff --git a/gateway/config/config.go b/gateway/config/config.go index 50ed41fd..4cc98bf4 100644 --- a/gateway/config/config.go +++ b/gateway/config/config.go @@ -24,6 +24,8 @@ var EVENT_SERVICE string var STAT_SERVICE string var NOTIFICATIONS_SERVICE string var PROJECT_SERVICE string +var RECOGNITION_SERVICE string + func Initialize() error { @@ -111,6 +113,12 @@ func Initialize() error { return err } + RECOGNITION_SERVICE, err = cfg_loader.Get("RECOGNITION_SERVICE") + + if err != nil { + return err + } + port_str, err := cfg_loader.Get("GATEWAY_PORT") if err != nil { diff --git a/gateway/services/event.go b/gateway/services/event.go index ed7700d6..8277f299 100644 --- a/gateway/services/event.go +++ b/gateway/services/event.go @@ -1,6 +1,7 @@ package services import ( + "fmt" "net/http" "github.com/HackIllinois/api/gateway/config" @@ -88,6 +89,8 @@ var EventRoutes = arbor.RouteCollection{ } func GetEvent(w http.ResponseWriter, r *http.Request) { + fmt.Println("Yeaa, uh huh uh huh") + fmt.Println("Hello, %v", r.URL.String()) arbor.GET(w, config.EVENT_SERVICE+r.URL.String(), EventFormat, "", r) } diff --git a/gateway/services/recognition.go b/gateway/services/recognition.go new file mode 100644 index 00000000..acfbde02 --- /dev/null +++ b/gateway/services/recognition.go @@ -0,0 +1,36 @@ +package services + +import ( + "net/http" + + "github.com/HackIllinois/api/gateway/config" + "github.com/HackIllinois/api/gateway/middleware" + "github.com/HackIllinois/api/gateway/models" + "github.com/arbor-dev/arbor" + "github.com/justinas/alice" +) + +const RecognitionFormat string = "JSON" + +var RecognitionRoutes = arbor.RouteCollection{ + arbor.Route{ + "GetAllRecognitions", + "GET", + "/recognition/", + alice.New(middleware.IdentificationMiddleware).ThenFunc(GetRecognition).ServeHTTP, + }, + arbor.Route{ + "CreateRecognition", + "POST", + "/recognition/", + alice.New(middleware.AuthMiddleware([]models.Role{models.AdminRole}), middleware.IdentificationMiddleware).ThenFunc(CreateRecognition).ServeHTTP, + }, +} + +func GetRecognition(w http.ResponseWriter, r *http.Request) { + arbor.GET(w, config.RECOGNITION_SERVICE+r.URL.String(), RecognitionFormat, "", r) +} + +func CreateRecognition(w http.ResponseWriter, r *http.Request) { + arbor.POST(w, config.RECOGNITION_SERVICE+r.URL.String(), RecognitionFormat, "", r) +} diff --git a/gateway/services/services.go b/gateway/services/services.go index 36e44eea..5aac715c 100644 --- a/gateway/services/services.go +++ b/gateway/services/services.go @@ -24,6 +24,7 @@ func Initialize() error { "stat": config.STAT_SERVICE, "notifications": config.NOTIFICATIONS_SERVICE, "project": config.PROJECT_SERVICE, + "recognition": config.RECOGNITION_SERVICE, } return nil @@ -65,6 +66,7 @@ func RegisterAPIs() arbor.RouteCollection { Routes = append(Routes, StatRoutes...) Routes = append(Routes, NotificationsRoutes...) Routes = append(Routes, ProjectRoutes...) + Routes = append(Routes, RecognitionRoutes...) Routes = append(Routes, HealthRoutes...) Routes = append(Routes, ReloadRoutes...) return Routes diff --git a/log/access.log b/log/access.log deleted file mode 100644 index e69de29b..00000000 diff --git a/main.go b/main.go index 804160e4..56b754d2 100644 --- a/main.go +++ b/main.go @@ -18,6 +18,7 @@ import ( "github.com/HackIllinois/api/services/stat" "github.com/HackIllinois/api/services/upload" "github.com/HackIllinois/api/services/user" + "github.com/HackIllinois/api/services/recognition" ) var SERVICE_ENTRYPOINTS = map[string](func()){ @@ -34,6 +35,7 @@ var SERVICE_ENTRYPOINTS = map[string](func()){ "stat": stat.Entry, "notifications": notifications.Entry, "project": project.Entry, + "recognition": recognition.Entry, } func StartAll() { diff --git a/services/recognition/config/config.go b/services/recognition/config/config.go new file mode 100644 index 00000000..360710e4 --- /dev/null +++ b/services/recognition/config/config.go @@ -0,0 +1,41 @@ +package config + +import ( + "os" + + "github.com/HackIllinois/api/common/configloader" +) + +var RECOGNITION_DB_HOST string +var RECOGNITION_DB_NAME string + +var RECOGNITION_PORT string + + +func Initialize() error { + cfg_loader, err := configloader.Load(os.Getenv("HI_CONFIG")) + + if err != nil { + return err + } + + RECOGNITION_DB_HOST, err = cfg_loader.Get("RECOGNITION_DB_HOST") + + if err != nil { + return err + } + + RECOGNITION_DB_NAME, err = cfg_loader.Get("RECOGNITION_DB_NAME") + + if err != nil { + return err + } + + RECOGNITION_PORT, err = cfg_loader.Get("RECOGNITION_PORT") + + if err != nil { + return err + } + + return nil +} diff --git a/services/recognition/controller/controller.go b/services/recognition/controller/controller.go new file mode 100644 index 00000000..4d035ec1 --- /dev/null +++ b/services/recognition/controller/controller.go @@ -0,0 +1,59 @@ +package controller + +import ( + "encoding/json" + "net/http" + + "github.com/HackIllinois/api/common/errors" + "github.com/HackIllinois/api/common/utils" + "github.com/HackIllinois/api/services/recognition/models" + "github.com/HackIllinois/api/services/recognition/service" + "github.com/gorilla/mux" +) + +func SetupController(route *mux.Route) { + router := route.Subrouter() + + router.HandleFunc("/", CreateRecognition).Methods("POST") + router.HandleFunc("/", GetAllRecognitions).Methods("GET") +} + +/* + Endpoint to get all recognitions +*/ +func GetAllRecognitions(w http.ResponseWriter, r *http.Request) { + recognition_list, err := service.GetAllRecognitions() + + if err != nil { + errors.WriteError(w, r, errors.DatabaseError(err.Error(), "Could not get all recognitions.")) + return + } + + json.NewEncoder(w).Encode(recognition_list) +} + +/* + Endpoint to create an recognition +*/ +func CreateRecognition(w http.ResponseWriter, r *http.Request) { + var recognition models.Recognition + json.NewDecoder(r.Body).Decode(&recognition) + + recognition.ID = utils.GenerateUniqueID() + + err := service.CreateRecognition(recognition.ID, recognition) + + if err != nil { + errors.WriteError(w, r, errors.DatabaseError(err.Error(), "Could not create new recognition.")) + return + } + + updated_recognition, err := service.GetRecognition(recognition.ID) + + if err != nil { + errors.WriteError(w, r, errors.DatabaseError(err.Error(), "Could not get updated recognition.")) + return + } + + json.NewEncoder(w).Encode(updated_recognition) +} diff --git a/services/recognition/main.go b/services/recognition/main.go new file mode 100644 index 00000000..f335bc05 --- /dev/null +++ b/services/recognition/main.go @@ -0,0 +1,40 @@ +package recognition + +import ( + "github.com/HackIllinois/api/common/apiserver" + "github.com/HackIllinois/api/services/recognition/config" + "github.com/HackIllinois/api/services/recognition/controller" + "github.com/HackIllinois/api/services/recognition/service" + "github.com/gorilla/mux" + "log" +) + +func Initialize() error { + err := config.Initialize() + + if err != nil { + return err + + } + + err = service.Initialize() + + if err != nil { + return err + } + + return nil +} + +func Entry() { + err := Initialize() + + if err != nil { + log.Fatal(err) + } + + router := mux.NewRouter() + controller.SetupController(router.PathPrefix("/recognition")) + + log.Fatal(apiserver.StartServer(config.RECOGNITION_PORT, router, "recognition", Initialize)) +} diff --git a/services/recognition/models/recognition.go b/services/recognition/models/recognition.go new file mode 100644 index 00000000..3c769c6c --- /dev/null +++ b/services/recognition/models/recognition.go @@ -0,0 +1,10 @@ +package models + +type Recognition struct { + ID string `json:"id" validate:"required"` + Name string `json:"name" validate:"required"` + Description string `json:"description" validate:"required"` + StartTime int64 `json:"startTime" validate:"required"` + EndTime int64 `json:"endTime" validate:"required"` + Sponsor string `json:"sponsor"` +} diff --git a/services/recognition/models/recognition_list.go b/services/recognition/models/recognition_list.go new file mode 100644 index 00000000..64a10d7c --- /dev/null +++ b/services/recognition/models/recognition_list.go @@ -0,0 +1,5 @@ +package models + +type RecognitionList struct { + Recognitions []Recognition `json:"recognitions"` +} diff --git a/services/recognition/service/recognition_service.go b/services/recognition/service/recognition_service.go new file mode 100644 index 00000000..90f39004 --- /dev/null +++ b/services/recognition/service/recognition_service.go @@ -0,0 +1,99 @@ +package service + +import ( + "errors" + + "github.com/HackIllinois/api/common/database" + "github.com/HackIllinois/api/services/recognition/config" + "github.com/HackIllinois/api/services/recognition/models" + "gopkg.in/go-playground/validator.v9" +) + +var validate *validator.Validate + +var db database.Database + +func Initialize() error { + if db != nil { + db.Close() + db = nil + } + + var err error + db, err = database.InitDatabase(config.RECOGNITION_DB_HOST, config.RECOGNITION_DB_NAME) + + if err != nil { + return err + } + + validate = validator.New() + + return nil +} + +/* + Returns the recognition with the given id +*/ +func GetRecognition(id string) (*models.Recognition, error) { + query := database.QuerySelector{ + "id": id, + } + + var recognition models.Recognition + err := db.FindOne("recognitions", query, &recognition) + + if err != nil { + return nil, err + } + + return &recognition, nil +} + + +/* + Returns all the recognitions +*/ +func GetAllRecognitions() (*models.RecognitionList, error) { + recognitions := []models.Recognition{} + // nil implies there are no filters on the query, therefore everything in the "recognitions" collection is returned. + err := db.FindAll("recognitions", nil, &recognitions) + + if err != nil { + return nil, err + } + + recognition_list := models.RecognitionList{ + Recognitions: recognitions, + } + + return &recognition_list, nil +} + + +/* + Creates an recognition with the given id +*/ +func CreateRecognition(id string, recognition models.Recognition) error { + err := validate.Struct(recognition) + + if err != nil { + return err + } + + _, err = GetRecognition(id) + + if err != database.ErrNotFound { + if err != nil { + return err + } + return errors.New("Recognition already exists") + } + + err = db.Insert("recognitions", &recognition) + + if err != nil { + return err + } + + return err +} From 02d8892484633e5481b71131c5c3ef86e5686b78 Mon Sep 17 00:00:00 2001 From: aukey2 Date: Wed, 6 May 2020 14:30:36 -0500 Subject: [PATCH 2/5] Basic endpoint functionlity running --- gateway/services/event.go | 3 --- log/access.log | 0 scripts/run.sh | 1 + services/recognition/models/recognition.go | 2 -- 4 files changed, 1 insertion(+), 5 deletions(-) create mode 100644 log/access.log diff --git a/gateway/services/event.go b/gateway/services/event.go index 8277f299..ed7700d6 100644 --- a/gateway/services/event.go +++ b/gateway/services/event.go @@ -1,7 +1,6 @@ package services import ( - "fmt" "net/http" "github.com/HackIllinois/api/gateway/config" @@ -89,8 +88,6 @@ var EventRoutes = arbor.RouteCollection{ } func GetEvent(w http.ResponseWriter, r *http.Request) { - fmt.Println("Yeaa, uh huh uh huh") - fmt.Println("Hello, %v", r.URL.String()) arbor.GET(w, config.EVENT_SERVICE+r.URL.String(), EventFormat, "", r) } diff --git a/log/access.log b/log/access.log new file mode 100644 index 00000000..e69de29b diff --git a/scripts/run.sh b/scripts/run.sh index 24e951ba..7efb4d19 100755 --- a/scripts/run.sh +++ b/scripts/run.sh @@ -28,6 +28,7 @@ $REPO_ROOT/bin/hackillinois-api --service event & $REPO_ROOT/bin/hackillinois-api --service stat & $REPO_ROOT/bin/hackillinois-api --service notifications & $REPO_ROOT/bin/hackillinois-api --service project & +$REPO_ROOT/bin/hackillinois-api --service recognition & $REPO_ROOT/bin/hackillinois-api --service gateway & diff --git a/services/recognition/models/recognition.go b/services/recognition/models/recognition.go index 3c769c6c..328f3517 100644 --- a/services/recognition/models/recognition.go +++ b/services/recognition/models/recognition.go @@ -4,7 +4,5 @@ type Recognition struct { ID string `json:"id" validate:"required"` Name string `json:"name" validate:"required"` Description string `json:"description" validate:"required"` - StartTime int64 `json:"startTime" validate:"required"` - EndTime int64 `json:"endTime" validate:"required"` Sponsor string `json:"sponsor"` } From baff3673c7037404d5674ae2156fab85fc10a231 Mon Sep 17 00:00:00 2001 From: aukey2 Date: Wed, 6 May 2020 16:41:24 -0500 Subject: [PATCH 3/5] Delete functionality working --- .../docs/reference/services/Recognition.md | 108 ++++++++++++++++++ log/access.log | 0 services/recognition/controller/controller.go | 20 ++++ services/recognition/models/recognition.go | 10 +- .../service/recognition_service.go | 39 +++++++ 5 files changed, 176 insertions(+), 1 deletion(-) create mode 100644 documentation/docs/reference/services/Recognition.md delete mode 100644 log/access.log diff --git a/documentation/docs/reference/services/Recognition.md b/documentation/docs/reference/services/Recognition.md new file mode 100644 index 00000000..91e604e8 --- /dev/null +++ b/documentation/docs/reference/services/Recognition.md @@ -0,0 +1,108 @@ +Recognition +===== + + +GET /recognition/ +--------------------- + +Returns a list of all events. + +Response format: +``` +{ + events: [ + { + "id": "52fdfc072182654f163f5f0f9a621d72", + "name": "Example Recognition 10", + "description": "This is a description", + "startTime": 1532202702, + "endTime": 1532212702, + "locations": [ + { + "description": "Example Location", + "tags": ["SIEBEL0", "ECEB1"], + "latitude": 40.1138, + "longitude": -88.2249 + } + ], + "sponsor": "Example sponsor", + "eventType": "WORKSHOP" + }, + { + "id": "52fdfcab71282654f163f5f0f9a621d72", + "name": "Example Recognition 11", + "description": "This is another description", + "startTime": 1532202702, + "endTime": 1532212702, + "locations": [ + { + "description": "Example Location", + "tags": ["SIEBEL3"], + "latitude": 40.1138, + "longitude": -88.2249 + } + ], + "sponsor": "Example sponsor", + "eventType": "WORKSHOP" + } + ] +} +``` + +POST /recognition/ +----------- + +Creates an event with the requested fields. Returns the created event. + +Request format: +``` +{ + "name": "Example Recognition 10", + "description": "This is a description", + "presenter": "Example presenter", + "eventId": "81855ad8681d0d86d1e91e00167939cb", + "tags": ["Data Science", "Mobile"], + "recipients": [ + { + "type": "ALL" + } + ] +} +``` + +Response format: +``` +{ + "name": "Example Recognition 10", + "description": "This is a description", + "presenter": "Example presenter", + "eventId": "81855ad8681d0d86d1e91e00167939cb", + "tags": ["Data Science", "Mobile"], + "recipients": [ + { + "type": "ALL" + } + ] +} +``` + +DELETE /recognition/RECOGNITIONID/ +----------- + +Endpoint to delete an event with name `RECOGNITIONID` + +Response format: +``` +{ + "name": "Example Recognition 10", + "description": "This is a description", + "presenter": "Example presenter", + "eventId": "81855ad8681d0d86d1e91e00167939cb", + "tags": ["Data Science", "Mobile"], + "recipients": [ + { + "type": "ALL" + } + ] +} +``` diff --git a/log/access.log b/log/access.log deleted file mode 100644 index e69de29b..00000000 diff --git a/services/recognition/controller/controller.go b/services/recognition/controller/controller.go index 4d035ec1..e5de959a 100644 --- a/services/recognition/controller/controller.go +++ b/services/recognition/controller/controller.go @@ -16,6 +16,26 @@ func SetupController(route *mux.Route) { router.HandleFunc("/", CreateRecognition).Methods("POST") router.HandleFunc("/", GetAllRecognitions).Methods("GET") + + router.HandleFunc("/{id}/", DeleteRecognition).Methods("DELETE") +} + +/* + Endpoint to delete an event with the specified id. + It removes the event from the event trackers, and every user's tracker. + On successful deletion, it returns the event that was deleted. +*/ +func DeleteRecognition(w http.ResponseWriter, r *http.Request) { + id := mux.Vars(r)["id"] + + event, err := service.DeleteRecognition(id) + + if err != nil { + errors.WriteError(w, r, errors.InternalError(err.Error(), "Could not delete either the event, event trackers, or user trackers, or an intermediary subroutine failed.")) + return + } + + json.NewEncoder(w).Encode(event) } /* diff --git a/services/recognition/models/recognition.go b/services/recognition/models/recognition.go index 328f3517..85b292f0 100644 --- a/services/recognition/models/recognition.go +++ b/services/recognition/models/recognition.go @@ -4,5 +4,13 @@ type Recognition struct { ID string `json:"id" validate:"required"` Name string `json:"name" validate:"required"` Description string `json:"description" validate:"required"` - Sponsor string `json:"sponsor"` + Presenter string `json:"presenter" validate:"required"` + EventID string `json:"eventId" validate:"required"` + Recepients []Recepient `json:"recipients" validate:"required"` + Tags []string `json:"tags"` +} + +type Recepient struct { + Type string `json:"type" validate:"required,oneof=ALL INDIVIDUAL"` + UserID string `json:"userId"` } diff --git a/services/recognition/service/recognition_service.go b/services/recognition/service/recognition_service.go index 90f39004..c5c0c8eb 100644 --- a/services/recognition/service/recognition_service.go +++ b/services/recognition/service/recognition_service.go @@ -49,6 +49,45 @@ func GetRecognition(id string) (*models.Recognition, error) { return &recognition, nil } +/* + Deletes the recognition with the given id. + Removes the recognition from recognition trackers and every user's tracker. + Returns the recognition that was deleted. +*/ +func DeleteRecognition(id string) (*models.Recognition, error) { + + // Gets recognition to be able to return it later + + recognition, err := GetRecognition(id) + + if err != nil { + return nil, err + } + + query := database.QuerySelector{ + "id": id, + } + + // Remove recognition from recognitions database + err = db.RemoveOne("recognitions", query) + + if err != nil { + return nil, err + } + + // Find all elements, and remove `id` from the Recognitions slice + // All the updates are individually atomic + update_expression := database.QuerySelector { + "$pull": database.QuerySelector{ + "recognitions": id, + }, + } + + _, err = db.UpdateAll("usertrackers", nil, &update_expression) + + return recognition, err +} + /* Returns all the recognitions From 4e7d2b2bd9118205f5d3b62dc2b9e745790b02cf Mon Sep 17 00:00:00 2001 From: aukey2 Date: Wed, 6 May 2020 20:09:26 -0500 Subject: [PATCH 4/5] Added filter feature --- .../docs/reference/services/Recognition.md | 115 +++++++++++------- services/recognition/controller/controller.go | 29 ++++- services/recognition/models/recognition.go | 4 +- .../service/recognition_service.go | 22 ++++ 4 files changed, 120 insertions(+), 50 deletions(-) diff --git a/documentation/docs/reference/services/Recognition.md b/documentation/docs/reference/services/Recognition.md index 91e604e8..0be6aeb9 100644 --- a/documentation/docs/reference/services/Recognition.md +++ b/documentation/docs/reference/services/Recognition.md @@ -5,54 +5,54 @@ Recognition GET /recognition/ --------------------- -Returns a list of all events. +Returns a list of all recognitions. Response format: ``` { - events: [ - { - "id": "52fdfc072182654f163f5f0f9a621d72", - "name": "Example Recognition 10", - "description": "This is a description", - "startTime": 1532202702, - "endTime": 1532212702, - "locations": [ - { - "description": "Example Location", - "tags": ["SIEBEL0", "ECEB1"], - "latitude": 40.1138, - "longitude": -88.2249 - } - ], - "sponsor": "Example sponsor", - "eventType": "WORKSHOP" - }, - { - "id": "52fdfcab71282654f163f5f0f9a621d72", - "name": "Example Recognition 11", - "description": "This is another description", - "startTime": 1532202702, - "endTime": 1532212702, - "locations": [ - { - "description": "Example Location", - "tags": ["SIEBEL3"], - "latitude": 40.1138, - "longitude": -88.2249 - } - ], - "sponsor": "Example sponsor", - "eventType": "WORKSHOP" - } - ] + "recognitions": [ + { + "id": "81855ad8681d0d86d1e91e00167939cb", + "name": "Example Recognition 10", + "description": "This is a description", + "presenter": "Example presenter", + "recognitionId": "81855ad8681d0d86d1e91e00167939cb", + "recipients": [ + { + "type": "PROJECT", + "typeId": "52fdfc072182654f163f5f0f9a621d72" + } + ], + "tags": [ + "Data Science", + "Mobile" + ] + }, + { + "id": "9566c74d10037c4d7bbb0407d1e2c649", + "name": "Best Computer Security", + "description": "Good mastery of safe coding practices", + "presenter": "HackIllinois", + "recognitionId": "52fdfc072182654f163f5f0f9a621d72", + "recipients": [ + { + "type": "INDIVIDUAL", + "typeId": "github09829234" + } + ], + "tags": [ + "Security", + "Virus" + ] + } + ] } ``` POST /recognition/ ----------- -Creates an event with the requested fields. Returns the created event. +Creates an recognition with the requested fields. Returns the created recognition. Request format: ``` @@ -60,7 +60,7 @@ Request format: "name": "Example Recognition 10", "description": "This is a description", "presenter": "Example presenter", - "eventId": "81855ad8681d0d86d1e91e00167939cb", + "recognitionId": "81855ad8681d0d86d1e91e00167939cb", "tags": ["Data Science", "Mobile"], "recipients": [ { @@ -76,7 +76,7 @@ Response format: "name": "Example Recognition 10", "description": "This is a description", "presenter": "Example presenter", - "eventId": "81855ad8681d0d86d1e91e00167939cb", + "recognitionId": "81855ad8681d0d86d1e91e00167939cb", "tags": ["Data Science", "Mobile"], "recipients": [ { @@ -89,7 +89,7 @@ Response format: DELETE /recognition/RECOGNITIONID/ ----------- -Endpoint to delete an event with name `RECOGNITIONID` +Endpoint to delete an recognition with name `RECOGNITIONID` Response format: ``` @@ -97,7 +97,7 @@ Response format: "name": "Example Recognition 10", "description": "This is a description", "presenter": "Example presenter", - "eventId": "81855ad8681d0d86d1e91e00167939cb", + "recognitionId": "81855ad8681d0d86d1e91e00167939cb", "tags": ["Data Science", "Mobile"], "recipients": [ { @@ -106,3 +106,34 @@ Response format: ] } ``` + + +GET /recognition/filter/?key=value +--------------------- + +Returns all recognitions, filtered with the given key-value pairs. + +Response format: +``` +{ + "recognitions": [ + { + "id": "81855ad8681d0d86d1e91e00167939cb", + "name": "Example Recognition 10", + "description": "This is a description", + "presenter": "Example presenter", + "recognitionId": "81855ad8681d0d86d1e91e00167939cb", + "recipients": [ + { + "type": "PROJECT", + "typeId": "52fdfc072182654f163f5f0f9a621d72" + } + ], + "tags": [ + "Data Science", + "Mobile" + ] + } + ] +} +``` diff --git a/services/recognition/controller/controller.go b/services/recognition/controller/controller.go index e5de959a..8fa331cd 100644 --- a/services/recognition/controller/controller.go +++ b/services/recognition/controller/controller.go @@ -17,25 +17,26 @@ func SetupController(route *mux.Route) { router.HandleFunc("/", CreateRecognition).Methods("POST") router.HandleFunc("/", GetAllRecognitions).Methods("GET") + router.HandleFunc("/filter/", GetFilteredRecognitions).Methods("GET") router.HandleFunc("/{id}/", DeleteRecognition).Methods("DELETE") } /* - Endpoint to delete an event with the specified id. - It removes the event from the event trackers, and every user's tracker. - On successful deletion, it returns the event that was deleted. + Endpoint to delete an recognition with the specified id. + It removes the recognition from the recognition trackers, and every user's tracker. + On successful deletion, it returns the recognition that was deleted. */ func DeleteRecognition(w http.ResponseWriter, r *http.Request) { id := mux.Vars(r)["id"] - event, err := service.DeleteRecognition(id) + recognition, err := service.DeleteRecognition(id) if err != nil { - errors.WriteError(w, r, errors.InternalError(err.Error(), "Could not delete either the event, event trackers, or user trackers, or an intermediary subroutine failed.")) + errors.WriteError(w, r, errors.InternalError(err.Error(), "Could not delete either the recognition")) return } - json.NewEncoder(w).Encode(event) + json.NewEncoder(w).Encode(recognition) } /* @@ -77,3 +78,19 @@ func CreateRecognition(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(updated_recognition) } + + +/* + Endpoint to get recognitions based on filters +*/ +func GetFilteredRecognitions(w http.ResponseWriter, r *http.Request) { + parameters := r.URL.Query() + recognition, err := service.GetFilteredRecognitions(parameters) + + if err != nil { + errors.WriteError(w, r, errors.DatabaseError(err.Error(), "Could not fetch filtered list of recognitions.")) + return + } + + json.NewEncoder(w).Encode(recognition) +} diff --git a/services/recognition/models/recognition.go b/services/recognition/models/recognition.go index 85b292f0..e79d4ffa 100644 --- a/services/recognition/models/recognition.go +++ b/services/recognition/models/recognition.go @@ -11,6 +11,6 @@ type Recognition struct { } type Recepient struct { - Type string `json:"type" validate:"required,oneof=ALL INDIVIDUAL"` - UserID string `json:"userId"` + Type string `json:"type" validate:"required,oneof=ALL INDIVIDUAL PROJECT"` + TypeID string `json:"typeId"` } diff --git a/services/recognition/service/recognition_service.go b/services/recognition/service/recognition_service.go index c5c0c8eb..c3b88016 100644 --- a/services/recognition/service/recognition_service.go +++ b/services/recognition/service/recognition_service.go @@ -109,6 +109,28 @@ func GetAllRecognitions() (*models.RecognitionList, error) { } +/* + Returns filtered recognitions +*/ +func GetFilteredRecognitions(parameters map[string][]string) (*models.RecognitionList, error) { + query, err := database.CreateFilterQuery(parameters, models.Recognition{}) + + if err != nil { + return nil, err + } + + recognitions := []models.Recognition{} + filtered_recognitions := models.RecognitionList{Recognitions: recognitions} + err = db.FindAll("recognitions", query, &filtered_recognitions.Recognitions) + + if err != nil { + return nil, err + } + + return &filtered_recognitions, nil +} + + /* Creates an recognition with the given id */ From b4646fffc332ccf314ea091f405306a693abe9ae Mon Sep 17 00:00:00 2001 From: aukey2 Date: Wed, 6 May 2020 20:24:27 -0500 Subject: [PATCH 5/5] Fixed README.md error --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b260148f..78c347a4 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ This `Admin` JWT should be passed as the `Authorization` header when making a re There are a couple other useful but not necessary tools for working on the API. The first is a GUI tool for viewing and modifying the database. There are many options including [MongoDB Compass](https://www.mongodb.com/products/compass) and [Robo 3T](https://robomongo.org/). You will also want to install [Postman](https://www.getpostman.com/) for making requests to the API when testing. ## Building, Testing, and Running the API -In order to simply API development `make` is used for building, testing, and running the API. All `make` commands can be run from the root of the repository and they will properly find and operate on all of the services. +In order to simplify API development `make` is used for building, testing, and running the API. All `make` commands can be run from the root of the repository and they will properly find and operate on all of the services. ### Building the API Run the following command from the root of the repository. The gateway and all services will be built into `bin`.