func TestDataDeleteSubscription(t *testing.T) { pool := newConnPool(t) userID, err := data.CreateUser(pool, newUser()) if err != nil { t.Fatal(err) } err = data.InsertSubscription(pool, userID, "http://foo") if err != nil { t.Fatal(err) } subscriptions, err := data.SelectSubscriptions(pool, userID) if err != nil { t.Fatal(err) } if len(subscriptions) != 1 { t.Fatalf("Found %d subscriptions, expected 1", len(subscriptions)) } feedID := subscriptions[0].FeedID.Value update := &data.ParsedFeed{Name: "baz", Items: []data.ParsedItem{ {URL: "http://baz/bar", Title: "Baz", PublicationTime: data.NewTime(time.Now())}, }} nullString := data.String{Status: data.Null} err = data.UpdateFeedWithFetchSuccess(pool, feedID, update, nullString, time.Now().Add(-20*time.Minute)) if err != nil { t.Fatal(err) } err = data.DeleteSubscription(pool, userID, feedID) if err != nil { t.Fatal(err) } subscriptions, err = data.SelectSubscriptions(pool, userID) if err != nil { t.Fatal(err) } if len(subscriptions) != 0 { t.Errorf("Found %d subscriptions, expected 0", len(subscriptions)) } // feed should have been deleted as it was the last user staleFeeds, err := data.GetFeedsUncheckedSince(pool, time.Now()) if err != nil { t.Fatal(err) } if len(staleFeeds) != 0 { t.Errorf("Found %d staleFeeds, expected 0", len(staleFeeds)) } }
func DeleteSubscriptionHandler(w http.ResponseWriter, req *http.Request, env *environment) { feedID, err := strconv.ParseInt(req.FormValue("id"), 10, 32) if err != nil { // If not an integer it clearly can't be found http.NotFound(w, req) return } if err := data.DeleteSubscription(env.pool, env.user.ID.Value, int32(feedID)); err != nil { w.WriteHeader(422) fmt.Fprintf(w, "Error deleting subscription: %v", err) return } w.WriteHeader(http.StatusOK) }