Skip to content

Commit

Permalink
Add endpoint for checking if a blog is being followed
Browse files Browse the repository at this point in the history
  • Loading branch information
theandrew168 committed Aug 10, 2024
1 parent a5848ad commit f5be8bb
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
2 changes: 1 addition & 1 deletion backend/web/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func Handler(store *storage.Storage, syncService *service.SyncService) http.Hand

// Check if the account follows a specific blog: HandleBlogFollowing
// GET /blogs/{blogID}/following -> 204 / 404
// mux.Handle("GET /blogs/{blogID}/following", accountRequired(HandleBlogFollowing(store)))
mux.Handle("GET /blogs/{blogID}/following", accountRequired(HandleBlogFollowing(store)))

mux.Handle("GET /blogs/{blogID}/posts", accountRequired(HandlePostList(store)))
mux.Handle("GET /blogs/{blogID}/posts/{postID}", accountRequired(HandlePostRead(store)))
Expand Down
41 changes: 41 additions & 0 deletions backend/web/api/follow.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,44 @@ func HandleBlogUnfollow(store *storage.Storage) http.Handler {
w.WriteHeader(http.StatusNoContent)
})
}

func HandleBlogFollowing(store *storage.Storage) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
account, ok := util.ContextGetAccount(r)
if !ok {
util.UnauthorizedResponse(w, r)
return
}

blogID, err := uuid.Parse(r.PathValue("blogID"))
if err != nil {
util.NotFoundResponse(w, r)
return
}

blog, err := store.Blog().Read(blogID)
if err != nil {
switch {
case errors.Is(err, postgres.ErrNotFound):
util.NotFoundResponse(w, r)
default:
util.ServerErrorResponse(w, r, err)
}

return
}

count, err := store.AccountBlog().Count(account, blog)
if err != nil {
util.ServerErrorResponse(w, r, err)
return
}

if count == 0 {
util.NotFoundResponse(w, r)
return
}

w.WriteHeader(http.StatusNoContent)
})
}
31 changes: 31 additions & 0 deletions backend/web/api/follow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,34 @@ func TestHandleBlogUnfollow(t *testing.T) {
rr := w.Result()
test.AssertEqual(t, rr.StatusCode, http.StatusNoContent)
}

func TestHandleBlogFollowing(t *testing.T) {
t.Parallel()

store, closer := test.NewStorage(t)
defer closer()

h := api.HandleBlogFollowing(store)

account, _ := test.CreateAccount(t, store)
blog := test.CreateBlog(t, store)

url := fmt.Sprintf("/blogs/%s/following", blog.ID())
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", url, nil)
r.SetPathValue("blogID", blog.ID().String())
h.ServeHTTP(w, util.ContextSetAccount(r, account))

rr := w.Result()
test.AssertEqual(t, rr.StatusCode, http.StatusNotFound)

test.CreateAccountBlog(t, store, account, blog)

w = httptest.NewRecorder()
r = httptest.NewRequest("GET", url, nil)
r.SetPathValue("blogID", blog.ID().String())
h.ServeHTTP(w, util.ContextSetAccount(r, account))

rr = w.Result()
test.AssertEqual(t, rr.StatusCode, http.StatusNoContent)
}

0 comments on commit f5be8bb

Please sign in to comment.