From 70f6b60d915824eb2335fc7836856b0efdac86d4 Mon Sep 17 00:00:00 2001 From: Wilfred Asomani Date: Wed, 2 Aug 2023 21:44:03 +0000 Subject: [PATCH] satellite/console: add get user projects http endpoint This change adds an endpoint to get a user's projects, similar to the MyProjects GraphQL query. Issue: https://github.com/storj/storj/issues/6132 Change-Id: I91feb5a1ee8c1231a8a5e6de9b8dc5b256f857c5 --- .../console/consoleweb/consoleapi/projects.go | 41 +++++++++++++++++++ .../console/consoleweb/endpoints_test.go | 16 ++++++++ satellite/console/consoleweb/server.go | 1 + 3 files changed, 58 insertions(+) diff --git a/satellite/console/consoleweb/consoleapi/projects.go b/satellite/console/consoleweb/consoleapi/projects.go index 95616f61b..3cc1bff2e 100644 --- a/satellite/console/consoleweb/consoleapi/projects.go +++ b/satellite/console/consoleweb/consoleapi/projects.go @@ -34,6 +34,47 @@ func NewProjects(log *zap.Logger, service *console.Service) *Projects { } } +// GetUserProjects returns the user's projects. +func (p *Projects) GetUserProjects(w http.ResponseWriter, r *http.Request) { + ctx := r.Context() + var err error + defer mon.Task()(&ctx)(&err) + + w.Header().Set("Content-Type", "application/json") + + projects, err := p.service.GetUsersProjects(ctx) + if err != nil { + p.serveJSONError(ctx, w, http.StatusInternalServerError, err) + return + } + + type jsonProject struct { + ID uuid.UUID `json:"id"` + Name string `json:"name"` + OwnerID uuid.UUID `json:"ownerId"` + Description string `json:"description"` + MemberCount int `json:"memberCount"` + CreatedAt time.Time `json:"createdAt"` + } + + response := make([]jsonProject, 0) + for _, project := range projects { + response = append(response, jsonProject{ + ID: project.PublicID, + Name: project.Name, + OwnerID: project.OwnerID, + Description: project.Description, + MemberCount: project.MemberCount, + CreatedAt: project.CreatedAt, + }) + } + + err = json.NewEncoder(w).Encode(response) + if err != nil { + p.serveJSONError(ctx, w, http.StatusInternalServerError, err) + } +} + // GetSalt returns the project's salt. func (p *Projects) GetSalt(w http.ResponseWriter, r *http.Request) { ctx := r.Context() diff --git a/satellite/console/consoleweb/endpoints_test.go b/satellite/console/consoleweb/endpoints_test.go index 9d73e7596..46b335065 100644 --- a/satellite/console/consoleweb/endpoints_test.go +++ b/satellite/console/consoleweb/endpoints_test.go @@ -485,6 +485,22 @@ func TestProjects(t *testing.T) { require.Equal(t, b64Salt, base64.StdEncoding.EncodeToString(salt)) } + { // Get_User_Projects + + var projects []struct { + ID uuid.UUID `json:"id"` + Name string `json:"name"` + OwnerID uuid.UUID `json:"ownerId"` + Description string `json:"description"` + MemberCount int `json:"memberCount"` + CreatedAt time.Time `json:"createdAt"` + } + resp, body := test.request(http.MethodGet, "/projects", nil) + require.Equal(t, http.StatusOK, resp.StatusCode) + require.NoError(t, json.Unmarshal([]byte(body), &projects)) + require.NotEmpty(t, projects) + } + { // Get_ProjectInfo resp, body := test.request(http.MethodPost, "/graphql", test.toJSON(map[string]interface{}{ diff --git a/satellite/console/consoleweb/server.go b/satellite/console/consoleweb/server.go index 668c5c5f8..100dc566d 100644 --- a/satellite/console/consoleweb/server.go +++ b/satellite/console/consoleweb/server.go @@ -275,6 +275,7 @@ func NewServer(logger *zap.Logger, config Config, service *console.Service, oidc projectsRouter := router.PathPrefix("/api/v0/projects").Subrouter() projectsRouter.Use(server.withCORS) projectsRouter.Use(server.withAuth) + projectsRouter.Handle("", http.HandlerFunc(projectsController.GetUserProjects)).Methods(http.MethodGet, http.MethodOptions) projectsRouter.Handle("/{id}/salt", http.HandlerFunc(projectsController.GetSalt)).Methods(http.MethodGet, http.MethodOptions) projectsRouter.Handle("/{id}/invite", http.HandlerFunc(projectsController.InviteUsers)).Methods(http.MethodPost, http.MethodOptions) projectsRouter.Handle("/{id}/invite-link", http.HandlerFunc(projectsController.GetInviteLink)).Methods(http.MethodGet, http.MethodOptions)