Open platform to collect and prioritize feedback
enhancement: a few missing API operations (#484)
Guilherme Oenning111b3e9d7333
diff --git a/app/cmd/routes.go b/app/cmd/routes.goindex 00dc80c92..ffe93a1fc 100644--- a/app/cmd/routes.go+++ b/app/cmd/routes.go@@ -85,9 +85,11 @@ func routes(r *web.Engine) *web.Engine {public := page.Group(){public.Get("/", handlers.Index())- public.Get("/api/v1/posts", apiv1.SearchPosts())public.Get("/posts/:number", handlers.PostDetails())public.Get("/posts/:number/*all", handlers.PostDetails())+ public.Get("/api/v1/posts", apiv1.SearchPosts())+ public.Get("/api/v1/tags", apiv1.ListTags())+ public.Get("/api/v1/posts/:number/comments", apiv1.ListComments())public.Get("/signout", handlers.SignOut())/* This is a temporary redirect and should be removed in the future */@@ -135,6 +137,7 @@ func routes(r *web.Engine) *web.Engine {private.Get("/admin/tags", handlers.ManageTags())private.Get("/admin/authentication", handlers.ManageAuthentication())private.Get("/_api/admin/oauth/:provider", handlers.GetOAuthConfig())+ private.Get("/api/v1/users", apiv1.ListUsers())private.Post("/api/v1/invitations/send", apiv1.SendInvites())private.Post("/api/v1/invitations/sample", apiv1.SendSampleInvite())diff --git a/app/handlers/apiv1/post.go b/app/handlers/apiv1/post.goindex f9e3efa97..ac690ae21 100644--- a/app/handlers/apiv1/post.go+++ b/app/handlers/apiv1/post.go@@ -110,6 +110,28 @@ func DeletePost() web.HandlerFunc {}}+// ListComments returns a list of all comments of a post+func ListComments() web.HandlerFunc {+ return func(c web.Context) error {+ number, err := c.ParamAsInt("number")+ if err != nil {+ return c.Failure(err)+ }++ post, err := c.Services().Posts.GetByNumber(number)+ if err != nil {+ return c.Failure(err)+ }++ comments, err := c.Services().Posts.GetCommentsByPost(post)+ if err != nil {+ return c.Failure(err)+ }++ return c.Ok(comments)+ }+}+// PostComment creates a new comment on given postfunc PostComment() web.HandlerFunc {return func(c web.Context) error {diff --git a/app/handlers/apiv1/post_test.go b/app/handlers/apiv1/post_test.goindex 7b0653690..ae2411e64 100644--- a/app/handlers/apiv1/post_test.go+++ b/app/handlers/apiv1/post_test.go@@ -405,3 +405,24 @@ func TestUpdateCommentHandler_Unauthorized(t *testing.T) {comment, _ := services.Posts.GetCommentByID(commentId)Expect(comment.Content).Equals("My first comment")}++func TestListCommentHandler(t *testing.T) {+ RegisterT(t)++ server, services := mock.NewServer()+ services.SetCurrentTenant(mock.DemoTenant)+ services.SetCurrentUser(mock.JonSnow)+ post, _ := services.Posts.Add("My First Post", "With a description")+ services.Posts.AddComment(post, "This is ...")+ services.Posts.AddComment(post, "Great!")++ code, query := server.+ OnTenant(mock.DemoTenant).+ AsUser(mock.JonSnow).+ AddParam("number", post.Number).+ ExecuteAsJSON(apiv1.ListComments())++ Expect(code).Equals(http.StatusOK)+ Expect(query.IsArray()).IsTrue()+ Expect(query.ArrayLength()).Equals(2)+}diff --git a/app/handlers/apiv1/tag.go b/app/handlers/apiv1/tag.goindex 73aaf0726..6a6a21b4d 100644--- a/app/handlers/apiv1/tag.go+++ b/app/handlers/apiv1/tag.go@@ -6,6 +6,18 @@ import ("github.com/getfider/fider/app/pkg/web")+// ListTags returns all tags+func ListTags() web.HandlerFunc {+ return func(c web.Context) error {+ tags, err := c.Services().Tags.GetAll()+ if err != nil {+ return c.Failure(err)+ }++ return c.Ok(tags)+ }+}+// AssignTag to existing deafunc AssignTag() web.HandlerFunc {return func(c web.Context) error {diff --git a/app/handlers/apiv1/tag_test.go b/app/handlers/apiv1/tag_test.goindex 4486511c9..d2b00f0b0 100644--- a/app/handlers/apiv1/tag_test.go+++ b/app/handlers/apiv1/tag_test.go@@ -251,3 +251,21 @@ func TestUnassignTagHandler_Success(t *testing.T) {Expect(err).IsNil()Expect(tags).HasLen(0)}++func TestListTagsHandler(t *testing.T) {+ RegisterT(t)++ server, services := mock.NewServer()+ services.SetCurrentTenant(mock.DemoTenant)+ services.SetCurrentUser(mock.JonSnow)+ services.Tags.Add("Bug", "0000FF", true)+ services.Tags.Add("Feature Request", "0000FF", true)++ status, query := server.+ AsUser(mock.JonSnow).+ ExecuteAsJSON(apiv1.ListTags())++ Expect(status).Equals(http.StatusOK)+ Expect(query.IsArray()).IsTrue()+ Expect(query.ArrayLength()).Equals(2)+}diff --git a/app/handlers/apiv1/user.go b/app/handlers/apiv1/user.gonew file mode 100644index 000000000..70a4f6ad8--- /dev/null+++ b/app/handlers/apiv1/user.go@@ -0,0 +1,17 @@+package apiv1++import (+ "github.com/getfider/fider/app/pkg/web"+)++// ListUsers returns all registered users+func ListUsers() web.HandlerFunc {+ return func(c web.Context) error {+ users, err := c.Services().Users.GetAll()+ if err != nil {+ return c.Failure(err)+ }++ return c.Ok(users)+ }+}diff --git a/app/handlers/apiv1/user_test.go b/app/handlers/apiv1/user_test.gonew file mode 100644index 000000000..d51de228e--- /dev/null+++ b/app/handlers/apiv1/user_test.go@@ -0,0 +1,26 @@+package apiv1_test++import (+ "net/http"+ "testing"++ "github.com/getfider/fider/app/handlers/apiv1"+ . "github.com/getfider/fider/app/pkg/assert"+ "github.com/getfider/fider/app/pkg/mock"+)++func TestListUsersHandler(t *testing.T) {+ RegisterT(t)++ server, services := mock.NewServer()+ services.SetCurrentTenant(mock.DemoTenant)+ services.SetCurrentUser(mock.JonSnow)++ status, query := server.+ AsUser(mock.JonSnow).+ ExecuteAsJSON(apiv1.ListUsers())++ Expect(status).Equals(http.StatusOK)+ Expect(query.IsArray()).IsTrue()+ Expect(query.ArrayLength()).Equals(2)+}