diff --git a/documentation/docs/reference/services/Event.md b/documentation/docs/reference/services/Event.md index b6bdfaa3..25612cf3 100644 --- a/documentation/docs/reference/services/Event.md +++ b/documentation/docs/reference/services/Event.md @@ -361,3 +361,15 @@ Response format: ] } ``` + +DELETE /event/favorite/remove/ +---------------------------- + +Removes all of the events from the favorites for the current user. + +Response format: +``` +{ + "id": "github001", + "events": [] +} diff --git a/gateway/services/event.go b/gateway/services/event.go index ed7700d6..416f2154 100644 --- a/gateway/services/event.go +++ b/gateway/services/event.go @@ -31,6 +31,12 @@ var EventRoutes = arbor.RouteCollection{ "/event/favorite/remove/", alice.New(middleware.AuthMiddleware([]models.Role{models.UserRole}), middleware.IdentificationMiddleware).ThenFunc(RemoveEventFavorite).ServeHTTP, }, + arbor.Route{ + "RemoveAllEventFavorites", + "DELETE", + "/event/favorite/remove/", + alice.New(middleware.AuthMiddleware([]models.Role{models.UserRole}), middleware.IdentificationMiddleware).ThenFunc(RemoveAllEventFavorites).ServeHTTP, + }, arbor.Route{ "MarkUserAsAttendingEvent", "POST", @@ -130,3 +136,7 @@ func AddEventFavorite(w http.ResponseWriter, r *http.Request) { func RemoveEventFavorite(w http.ResponseWriter, r *http.Request) { arbor.POST(w, config.EVENT_SERVICE+r.URL.String(), EventFormat, "", r) } + +func RemoveAllEventFavorites(w http.ResponseWriter, r *http.Request) { + arbor.DELETE(w, config.EVENT_SERVICE+r.URL.String(), EventFormat, "", r) +} diff --git a/services/event/controller/controller.go b/services/event/controller/controller.go index 26d4b09e..01ed5d12 100644 --- a/services/event/controller/controller.go +++ b/services/event/controller/controller.go @@ -17,6 +17,7 @@ func SetupController(route *mux.Route) { router.HandleFunc("/favorite/", GetEventFavorites).Methods("GET") router.HandleFunc("/favorite/add/", AddEventFavorite).Methods("POST") router.HandleFunc("/favorite/remove/", RemoveEventFavorite).Methods("POST") + router.HandleFunc("/favorite/remove/", RemoveAllEventFavorites).Methods("DELETE") router.HandleFunc("/filter/", GetFilteredEvents).Methods("GET") router.HandleFunc("/{id}/", GetEvent).Methods("GET") @@ -240,7 +241,7 @@ func GetEventFavorites(w http.ResponseWriter, r *http.Request) { favorites, err := service.GetEventFavorites(id) if err != nil { - errors.WriteError(w, r, errors.DatabaseError(err.Error(), "Could not get user's event favourites.")) + errors.WriteError(w, r, errors.DatabaseError(err.Error(), "Could not get user's event favorites.")) return } @@ -292,7 +293,30 @@ func RemoveEventFavorite(w http.ResponseWriter, r *http.Request) { favorites, err := service.GetEventFavorites(id) if err != nil { - errors.WriteError(w, r, errors.DatabaseError(err.Error(), "Could not fetch updated event favourites for the user (post-removal).")) + errors.WriteError(w, r, errors.DatabaseError(err.Error(), "Could not fetch updated event favorites for the user (post-removal).")) + return + } + + json.NewEncoder(w).Encode(favorites) +} + +/* + Endpoint to remove all of the event favorites for the current user +*/ +func RemoveAllEventFavorites(w http.ResponseWriter, r *http.Request) { + id := r.Header.Get("HackIllinois-Identity") + + err := service.RemoveAllEventFavorites(id) + + if err != nil { + errors.WriteError(w, r, errors.DatabaseError(err.Error(), "Could not remove all event favorites for the current user.")) + return + } + + favorites, err := service.GetEventFavorites(id) + + if err != nil { + errors.WriteError(w, r, errors.DatabaseError(err.Error(), "Could not fetch updated event favorites for the user (post-removal).")) return } diff --git a/services/event/service/event_service.go b/services/event/service/event_service.go index 794be1b7..0db244a7 100644 --- a/services/event/service/event_service.go +++ b/services/event/service/event_service.go @@ -416,7 +416,7 @@ func AddEventFavorite(id string, event string) error { } /* - Removes the given event to the favorites for the user with the given id + Removes the given event from the favorites for the user with the given id */ func RemoveEventFavorite(id string, event string) error { selector := database.QuerySelector{ @@ -440,6 +440,21 @@ func RemoveEventFavorite(id string, event string) error { return err } +/* + Removes all of the favorite events from the user with the given id +*/ +func RemoveAllEventFavorites(id string) error { + selector := database.QuerySelector{ + "id": id, + } + + _, err := db.RemoveAll("favorites", selector) + if err != nil { + return errors.New("Failed to delete all of the user's favorite events.") + } + return err +} + /* Returns all event stats */ diff --git a/services/event/tests/event_test.go b/services/event/tests/event_test.go index 686f828d..3fa0ed47 100644 --- a/services/event/tests/event_test.go +++ b/services/event/tests/event_test.go @@ -813,3 +813,71 @@ func TestRemoveEventFavorite(t *testing.T) { CleanupTestDB(t) } + +/* + Tests that clearing event favorites works correctly at the service level. +*/ +func TestRemoveAllEventFavorites(t *testing.T) { + SetupTestDB(t) + + // Add a second event (to test multi-event removal) + event := models.Event{ + ID: "testid2", + Name: "testname2", + Description: "testdescription2", + StartTime: TestTime, + EndTime: TestTime + 60000, + Sponsor: "testsponsor", + EventType: "WORKSHOP", + Locations: []models.EventLocation{ + { + Description: "testlocationdescription", + Tags: []string{"SIEBEL0", "ECEB1"}, + Latitude: 123.456, + Longitude: 123.456, + }, + }, + } + + err := db.Insert("events", &event) + + if err != nil { + t.Fatal(err) + } + + // Favorite event 1 + err = service.AddEventFavorite("testuser", "testid") + + if err != nil { + t.Fatal(err) + } + + // Favorite event 2 + err = service.AddEventFavorite("testuser", "testid2") + if err != nil { + t.Fatal(err) + } + + err = service.RemoveAllEventFavorites("testuser") + + if err != nil { + t.Fatal(err) + } + + event_favorites, err := service.GetEventFavorites("testuser") + + if err != nil { + t.Fatal(err) + } + + expected_event_favorites := models.EventFavorites{ + ID: "testuser", + Events: []string{}, + } + + if !reflect.DeepEqual(event_favorites, &expected_event_favorites) { + t.Errorf("Wrong tracker info. Expected %v, got %v", &expected_event_favorites, event_favorites) + } + + CleanupTestDB(t) +} diff --git a/services/project/controller/controller.go b/services/project/controller/controller.go index 8654b794..2849fa5f 100644 --- a/services/project/controller/controller.go +++ b/services/project/controller/controller.go @@ -35,7 +35,7 @@ func GetProjectFavorites(w http.ResponseWriter, r *http.Request) { favorites, err := service.GetProjectFavorites(id) if err != nil { - errors.WriteError(w, r, errors.DatabaseError(err.Error(), "Could not get user's project favourites.")) + errors.WriteError(w, r, errors.DatabaseError(err.Error(), "Could not get user's project favorites.")) return } @@ -87,7 +87,7 @@ func RemoveProjectFavorite(w http.ResponseWriter, r *http.Request) { favorites, err := service.GetProjectFavorites(id) if err != nil { - errors.WriteError(w, r, errors.DatabaseError(err.Error(), "Could not fetch updated project favourites for the user (post-removal).")) + errors.WriteError(w, r, errors.DatabaseError(err.Error(), "Could not fetch updated project favorites for the user (post-removal).")) return }