-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
gateway: add user project favorites api
add api to create, get, delete user project favorites.
- Loading branch information
1 parent
409f488
commit 70b8015
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 CreateUserProjectFavoriteRequest struct { | ||
ProjectRef string | ||
} | ||
|
||
func (h *ActionHandler) CreateUserProjectFavorite(ctx context.Context, req *CreateUserProjectFavoriteRequest) (*cstypes.ProjectFavorite, error) { | ||
if !common.IsUserLogged(ctx) { | ||
return nil, errors.Errorf("user not logged in") | ||
} | ||
|
||
userID := common.CurrentUserID(ctx) | ||
|
||
creq := &csapitypes.CreateProjectFavoriteRequest{ | ||
UserRef: userID, | ||
ProjectRef: req.ProjectRef, | ||
} | ||
|
||
projectFavorite, _, err := h.configstoreClient.CreateProjectFavorite(ctx, creq) | ||
if err != nil { | ||
return nil, util.NewAPIError(util.KindFromRemoteError(err), errors.Wrapf(err, "failed to create project favorite")) | ||
} | ||
|
||
return projectFavorite, nil | ||
} | ||
|
||
func (h *ActionHandler) DeleteUserProjectFavorite(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.DeleteProjectFavorite(ctx, userID, projectRef); err != nil { | ||
return util.NewAPIError(util.KindFromRemoteError(err), errors.Wrapf(err, "failed to delete project favorite")) | ||
} | ||
return nil | ||
} | ||
|
||
type GetUserProjectFavoritesRequest struct { | ||
Cursor string | ||
|
||
Limit int | ||
SortDirection SortDirection | ||
} | ||
|
||
type GetUserProjectFavoritesResponse struct { | ||
ProjectFavorites []*cstypes.ProjectFavorite | ||
Cursor string | ||
} | ||
|
||
func (h *ActionHandler) GetUserProjectFavorites(ctx context.Context, req *GetUserProjectFavoritesRequest) (*GetUserProjectFavoritesResponse, 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 | ||
} | ||
|
||
projectFavorites, resp, err := h.configstoreClient.GetProjectFavorites(ctx, userID, &client.GetProjectFavoritesOptions{ListOptions: &client.ListOptions{Limit: req.Limit, SortDirection: cstypes.SortDirection(sortDirection)}, StartProjectFavoriteID: inCursor.Start}) | ||
if err != nil { | ||
return nil, util.NewAPIError(util.KindFromRemoteError(err), err) | ||
} | ||
|
||
var outCursor string | ||
if resp.HasMore && len(projectFavorites) > 0 { | ||
lastProjectFavoriteID := projectFavorites[len(projectFavorites)-1].ID | ||
outCursor, err = MarshalCursor(&StartCursor{ | ||
Start: lastProjectFavoriteID, | ||
SortDirection: sortDirection, | ||
}) | ||
if err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
} | ||
|
||
res := &GetUserProjectFavoritesResponse{ | ||
ProjectFavorites: projectFavorites, | ||
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 CreateUserProjectFavoriteHandler struct { | ||
log zerolog.Logger | ||
ah *action.ActionHandler | ||
} | ||
|
||
func NewCreateUserProjectFavoriteHandler(log zerolog.Logger, ah *action.ActionHandler) *CreateUserProjectFavoriteHandler { | ||
return &CreateUserProjectFavoriteHandler{log: log, ah: ah} | ||
} | ||
|
||
func (h *CreateUserProjectFavoriteHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
ctx := r.Context() | ||
vars := mux.Vars(r) | ||
|
||
projectRef := vars["projectref"] | ||
|
||
creq := &action.CreateUserProjectFavoriteRequest{ | ||
ProjectRef: projectRef, | ||
} | ||
|
||
projectFavorite, err := h.ah.CreateUserProjectFavorite(ctx, creq) | ||
if util.HTTPError(w, err) { | ||
h.log.Err(err).Send() | ||
return | ||
} | ||
|
||
if err := util.HTTPResponse(w, http.StatusCreated, projectFavorite); err != nil { | ||
h.log.Err(err).Send() | ||
} | ||
} | ||
|
||
type DeleteUserProjectFavoriteHandler struct { | ||
log zerolog.Logger | ||
ah *action.ActionHandler | ||
} | ||
|
||
func NewDeleteUserProjectFavoriteHandler(log zerolog.Logger, ah *action.ActionHandler) *DeleteUserProjectFavoriteHandler { | ||
return &DeleteUserProjectFavoriteHandler{log: log, ah: ah} | ||
} | ||
|
||
func (h *DeleteUserProjectFavoriteHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
ctx := r.Context() | ||
vars := mux.Vars(r) | ||
|
||
projectRef := vars["projectref"] | ||
|
||
err := h.ah.DeleteUserProjectFavorite(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 UserProjectFavoritesHandler struct { | ||
log zerolog.Logger | ||
ah *action.ActionHandler | ||
} | ||
|
||
func NewUserProjectFavoritesHandler(log zerolog.Logger, ah *action.ActionHandler) *UserProjectFavoritesHandler { | ||
return &UserProjectFavoritesHandler{log: log, ah: ah} | ||
} | ||
|
||
func (h *UserProjectFavoritesHandler) 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 *UserProjectFavoritesHandler) do(w http.ResponseWriter, r *http.Request) ([]*gwapitypes.ProjectFavoriteResponse, error) { | ||
ctx := r.Context() | ||
|
||
ropts, err := parseRequestOptions(r) | ||
if err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
|
||
ares, err := h.ah.GetUserProjectFavorites(ctx, &action.GetUserProjectFavoritesRequest{Cursor: ropts.Cursor, Limit: ropts.Limit, SortDirection: action.SortDirection(ropts.SortDirection)}) | ||
if err != nil { | ||
return nil, errors.WithStack(err) | ||
} | ||
|
||
projectFavorites := make([]*gwapitypes.ProjectFavoriteResponse, len(ares.ProjectFavorites)) | ||
for i, p := range ares.ProjectFavorites { | ||
projectFavorites[i] = createProjectFavoriteResponse(p) | ||
} | ||
|
||
addCursorHeader(w, ares.Cursor) | ||
|
||
return projectFavorites, nil | ||
} | ||
|
||
func createProjectFavoriteResponse(o *cstypes.ProjectFavorite) *gwapitypes.ProjectFavoriteResponse { | ||
org := &gwapitypes.ProjectFavoriteResponse{ | ||
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 CreateProjectFavoriteRequest 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 ProjectFavoriteResponse struct { | ||
ID string `json:"id"` | ||
ProjectID string `json:"project_id"` | ||
} | ||
|
||
type CreateProjectFavoriteRequest 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.