// This function is a nasty copy and paste of testRepositoryUpdateFeedWithFetchSuccess // Fix me when refactoring tests func TestDataUpdateFeedWithFetchSuccessWithoutPublicationTime(t *testing.T) { pool := newConnPool(t) userID, err := data.CreateUser(pool, newUser()) if err != nil { t.Fatal(err) } now := time.Now() url := "http://bar" err = data.InsertSubscription(pool, userID, url) 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"}, }} nullString := data.String{Status: data.Null} err = data.UpdateFeedWithFetchSuccess(pool, feedID, update, nullString, now) if err != nil { t.Fatal(err) } buffer := &bytes.Buffer{} err = data.CopyUnreadItemsAsJSONByUserID(pool, buffer, userID) if err != nil { t.Fatal(err) } type UnreadItemsFromJSON struct { ID int32 `json:id` } var unreadItems []UnreadItemsFromJSON err = json.Unmarshal(buffer.Bytes(), &unreadItems) if err != nil { t.Fatal(err) } if len(unreadItems) != 1 { t.Fatalf("Found %d unreadItems, expected 1", len(unreadItems)) } // Update again and ensure item does not get created again err = data.UpdateFeedWithFetchSuccess(pool, feedID, update, nullString, now) if err != nil { t.Fatal(err) } buffer.Reset() err = data.CopyUnreadItemsAsJSONByUserID(pool, buffer, userID) if err != nil { t.Fatal(err) } err = json.Unmarshal(buffer.Bytes(), &unreadItems) if err != nil { t.Fatal(err) } if len(unreadItems) != 1 { t.Fatalf("Found %d unreadItems, expected 1", len(unreadItems)) } }
func GetUnreadItemsHandler(w http.ResponseWriter, req *http.Request, env *environment) { w.Header().Set("Content-Type", "application/json") if err := data.CopyUnreadItemsAsJSONByUserID(env.pool, w, env.user.ID.Value); err != nil { http.Error(w, "Internal server error", http.StatusInternalServerError) } }