-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add api to create, get, delete user project stars.
- Loading branch information
1 parent
924ef90
commit f27c17b
Showing
8 changed files
with
682 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
// Copyright 2019 Sorint.lab | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package action | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/sorintlab/errors" | ||
|
||
"agola.io/agola/internal/services/gateway/common" | ||
"agola.io/agola/internal/util" | ||
csapitypes "agola.io/agola/services/configstore/api/types" | ||
"agola.io/agola/services/configstore/client" | ||
cstypes "agola.io/agola/services/configstore/types" | ||
) | ||
|
||
type CreateUserProjectStarRequest struct { | ||
ProjectRef string | ||
} | ||
|
||
func (h *ActionHandler) CreateUserProjectStar(ctx context.Context, req *CreateUserProjectStarRequest) (*cstypes.ProjectStar, error) { | ||
if !common.IsUserLogged(ctx) { | ||
return nil, errors.Errorf("user not logged in") | ||
} | ||
|
||
userID := common.CurrentUserID(ctx) | ||
|
||
creq := &csapitypes.CreateProjectStarRequest{ | ||
UserRef: userID, | ||
ProjectRef: req.ProjectRef, | ||
} | ||
|
||
projectStar, _, err := h.configstoreClient.CreateProjectStar(ctx, creq) | ||
if err != nil { | ||
return nil, util.NewAPIError(util.KindFromRemoteError(err), errors.Wrapf(err, "failed to create project star")) | ||
} | ||
|
||
return projectStar, nil | ||
} | ||
|
||
func (h *ActionHandler) DeleteUserProjectStar(ctx context.Context, projectRef string) error { | ||
if !common.IsUserLogged(ctx) { | ||
return errors.Errorf("user not logged in") | ||
} | ||
|
||
userID := common.CurrentUserID(ctx) | ||
|
||
if _, err := h.configstoreClient.DeleteProjectStar(ctx, userID, projectRef); err != nil { | ||
return util.NewAPIError(util.KindFromRemoteError(err), errors.Wrapf(err, "failed to delete project star")) | ||
} | ||
return nil | ||
} | ||
|
||
type GetUserProjectStarsRequest struct { | ||
Cursor string | ||
|
||
Limit int | ||
SortDirection SortDirection | ||
} | ||
|
||
type GetUserProjectStarsResponse struct { | ||
ProjectStars []*cstypes.ProjectStar | ||
Cursor string | ||
} | ||
|
||
func (h *ActionHandler) GetUserProjectStars(ctx context.Context, req *GetUserProjectStarsRequest) (*GetUserProjectStarsResponse, error) { | ||
if !common.IsUserLogged(ctx) { | ||
return nil, errors.Errorf("user not logged in") | ||
} | ||
userID := common.CurrentUserID(ctx) | ||
|
||
inCursor := &StartCursor{} | ||
sortDirection := req.SortDirection | ||
if req.Cursor != "" { | ||
if err := UnmarshalCursor(req.Cursor, inCursor); err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
sortDirection = inCursor.SortDirection | ||
} | ||
if sortDirection == "" { | ||
sortDirection = SortDirectionAsc | ||
} | ||
|
||
projectStars, resp, err := h.configstoreClient.GetProjectStars(ctx, userID, &client.GetProjectStarsOptions{ListOptions: &client.ListOptions{Limit: req.Limit, SortDirection: cstypes.SortDirection(sortDirection)}, StartProjectStarID: inCursor.Start}) | ||
if err != nil { | ||
return nil, util.NewAPIError(util.KindFromRemoteError(err), err) | ||
} | ||
|
||
var outCursor string | ||
if resp.HasMore && len(projectStars) > 0 { | ||
lastProjectStarID := projectStars[len(projectStars)-1].ID | ||
outCursor, err = MarshalCursor(&StartCursor{ | ||
Start: lastProjectStarID, | ||
SortDirection: sortDirection, | ||
}) | ||
if err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
} | ||
|
||
res := &GetUserProjectStarsResponse{ | ||
ProjectStars: projectStars, | ||
Cursor: outCursor, | ||
} | ||
|
||
return res, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
// Copyright 2024 Sorint.lab | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package api | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/rs/zerolog" | ||
"github.com/sorintlab/errors" | ||
|
||
"agola.io/agola/internal/services/gateway/action" | ||
util "agola.io/agola/internal/util" | ||
cstypes "agola.io/agola/services/configstore/types" | ||
gwapitypes "agola.io/agola/services/gateway/api/types" | ||
) | ||
|
||
type CreateUserProjectStarHandler struct { | ||
log zerolog.Logger | ||
ah *action.ActionHandler | ||
} | ||
|
||
func NewCreateUserProjectStarHandler(log zerolog.Logger, ah *action.ActionHandler) *CreateUserProjectStarHandler { | ||
return &CreateUserProjectStarHandler{log: log, ah: ah} | ||
} | ||
|
||
func (h *CreateUserProjectStarHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
ctx := r.Context() | ||
vars := mux.Vars(r) | ||
|
||
projectRef := vars["projectref"] | ||
|
||
creq := &action.CreateUserProjectStarRequest{ | ||
ProjectRef: projectRef, | ||
} | ||
|
||
projectStar, err := h.ah.CreateUserProjectStar(ctx, creq) | ||
if util.HTTPError(w, err) { | ||
h.log.Err(err).Send() | ||
return | ||
} | ||
|
||
if err := util.HTTPResponse(w, http.StatusCreated, projectStar); err != nil { | ||
h.log.Err(err).Send() | ||
} | ||
} | ||
|
||
type DeleteUserProjectStarHandler struct { | ||
log zerolog.Logger | ||
ah *action.ActionHandler | ||
} | ||
|
||
func NewDeleteUserProjectStarHandler(log zerolog.Logger, ah *action.ActionHandler) *DeleteUserProjectStarHandler { | ||
return &DeleteUserProjectStarHandler{log: log, ah: ah} | ||
} | ||
|
||
func (h *DeleteUserProjectStarHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
ctx := r.Context() | ||
vars := mux.Vars(r) | ||
|
||
projectRef := vars["projectref"] | ||
|
||
err := h.ah.DeleteUserProjectStar(ctx, projectRef) | ||
if util.HTTPError(w, err) { | ||
h.log.Err(err).Send() | ||
return | ||
} | ||
|
||
if err := util.HTTPResponse(w, http.StatusNoContent, nil); err != nil { | ||
h.log.Err(err).Send() | ||
} | ||
} | ||
|
||
type UserProjectStarsHandler struct { | ||
log zerolog.Logger | ||
ah *action.ActionHandler | ||
} | ||
|
||
func NewUserProjectStarsHandler(log zerolog.Logger, ah *action.ActionHandler) *UserProjectStarsHandler { | ||
return &UserProjectStarsHandler{log: log, ah: ah} | ||
} | ||
|
||
func (h *UserProjectStarsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
res, err := h.do(w, r) | ||
if util.HTTPError(w, err) { | ||
h.log.Err(err).Send() | ||
return | ||
} | ||
|
||
if err := util.HTTPResponse(w, http.StatusOK, res); err != nil { | ||
h.log.Err(err).Send() | ||
} | ||
} | ||
|
||
func (h *UserProjectStarsHandler) do(w http.ResponseWriter, r *http.Request) ([]*gwapitypes.ProjectStarResponse, error) { | ||
ctx := r.Context() | ||
|
||
ropts, err := parseRequestOptions(r) | ||
if err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
|
||
ares, err := h.ah.GetUserProjectStars(ctx, &action.GetUserProjectStarsRequest{Cursor: ropts.Cursor, Limit: ropts.Limit, SortDirection: action.SortDirection(ropts.SortDirection)}) | ||
if err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
|
||
projectStars := make([]*gwapitypes.ProjectStarResponse, len(ares.ProjectStars)) | ||
for i, p := range ares.ProjectStars { | ||
projectStars[i] = createProjectStarResponse(p) | ||
} | ||
|
||
addCursorHeader(w, ares.Cursor) | ||
|
||
return projectStars, nil | ||
} | ||
|
||
func createProjectStarResponse(o *cstypes.ProjectStar) *gwapitypes.ProjectStarResponse { | ||
org := &gwapitypes.ProjectStarResponse{ | ||
ID: o.ID, | ||
ProjectID: o.ProjectID, | ||
} | ||
return org | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright 2024 Sorint.lab | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package types | ||
|
||
type CreateProjectStarRequest struct { | ||
UserRef string | ||
ProjectRef string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package types | ||
|
||
type ProjectStarResponse struct { | ||
ID string `json:"id"` | ||
ProjectID string `json:"project_id"` | ||
} | ||
|
||
type CreateProjectStarRequest struct { | ||
ProjectRef string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.