|
| 1 | +// Copyright 2024 Sorint.lab |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package api |
| 16 | + |
| 17 | +import ( |
| 18 | + "net/http" |
| 19 | + |
| 20 | + "github.com/gorilla/mux" |
| 21 | + "github.com/rs/zerolog" |
| 22 | + "github.com/sorintlab/errors" |
| 23 | + |
| 24 | + "agola.io/agola/internal/services/gateway/action" |
| 25 | + util "agola.io/agola/internal/util" |
| 26 | + cstypes "agola.io/agola/services/configstore/types" |
| 27 | + gwapitypes "agola.io/agola/services/gateway/api/types" |
| 28 | +) |
| 29 | + |
| 30 | +type CreateUserProjectStarHandler struct { |
| 31 | + log zerolog.Logger |
| 32 | + ah *action.ActionHandler |
| 33 | +} |
| 34 | + |
| 35 | +func NewCreateUserProjectStarHandler(log zerolog.Logger, ah *action.ActionHandler) *CreateUserProjectStarHandler { |
| 36 | + return &CreateUserProjectStarHandler{log: log, ah: ah} |
| 37 | +} |
| 38 | + |
| 39 | +func (h *CreateUserProjectStarHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 40 | + ctx := r.Context() |
| 41 | + vars := mux.Vars(r) |
| 42 | + |
| 43 | + projectRef := vars["projectref"] |
| 44 | + |
| 45 | + creq := &action.CreateUserProjectStarRequest{ |
| 46 | + ProjectRef: projectRef, |
| 47 | + } |
| 48 | + |
| 49 | + projectStar, err := h.ah.CreateUserProjectStar(ctx, creq) |
| 50 | + if util.HTTPError(w, err) { |
| 51 | + h.log.Err(err).Send() |
| 52 | + return |
| 53 | + } |
| 54 | + |
| 55 | + if err := util.HTTPResponse(w, http.StatusCreated, projectStar); err != nil { |
| 56 | + h.log.Err(err).Send() |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +type DeleteUserProjectStarHandler struct { |
| 61 | + log zerolog.Logger |
| 62 | + ah *action.ActionHandler |
| 63 | +} |
| 64 | + |
| 65 | +func NewDeleteUserProjectStarHandler(log zerolog.Logger, ah *action.ActionHandler) *DeleteUserProjectStarHandler { |
| 66 | + return &DeleteUserProjectStarHandler{log: log, ah: ah} |
| 67 | +} |
| 68 | + |
| 69 | +func (h *DeleteUserProjectStarHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 70 | + ctx := r.Context() |
| 71 | + vars := mux.Vars(r) |
| 72 | + |
| 73 | + projectRef := vars["projectref"] |
| 74 | + |
| 75 | + err := h.ah.DeleteUserProjectStar(ctx, projectRef) |
| 76 | + if util.HTTPError(w, err) { |
| 77 | + h.log.Err(err).Send() |
| 78 | + return |
| 79 | + } |
| 80 | + |
| 81 | + if err := util.HTTPResponse(w, http.StatusNoContent, nil); err != nil { |
| 82 | + h.log.Err(err).Send() |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +type UserProjectStarsHandler struct { |
| 87 | + log zerolog.Logger |
| 88 | + ah *action.ActionHandler |
| 89 | +} |
| 90 | + |
| 91 | +func NewUserProjectStarsHandler(log zerolog.Logger, ah *action.ActionHandler) *UserProjectStarsHandler { |
| 92 | + return &UserProjectStarsHandler{log: log, ah: ah} |
| 93 | +} |
| 94 | + |
| 95 | +func (h *UserProjectStarsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 96 | + res, err := h.do(w, r) |
| 97 | + if util.HTTPError(w, err) { |
| 98 | + h.log.Err(err).Send() |
| 99 | + return |
| 100 | + } |
| 101 | + |
| 102 | + if err := util.HTTPResponse(w, http.StatusOK, res); err != nil { |
| 103 | + h.log.Err(err).Send() |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +func (h *UserProjectStarsHandler) do(w http.ResponseWriter, r *http.Request) ([]*gwapitypes.ProjectStarResponse, error) { |
| 108 | + ctx := r.Context() |
| 109 | + |
| 110 | + ropts, err := parseRequestOptions(r) |
| 111 | + if err != nil { |
| 112 | + return nil, errors.WithStack(err) |
| 113 | + } |
| 114 | + |
| 115 | + ares, err := h.ah.GetUserProjectStars(ctx, &action.GetUserProjectStarsRequest{Cursor: ropts.Cursor, Limit: ropts.Limit, SortDirection: action.SortDirection(ropts.SortDirection)}) |
| 116 | + if err != nil { |
| 117 | + return nil, errors.WithStack(err) |
| 118 | + } |
| 119 | + |
| 120 | + projectStars := make([]*gwapitypes.ProjectStarResponse, len(ares.ProjectStars)) |
| 121 | + for i, p := range ares.ProjectStars { |
| 122 | + projectStars[i] = createProjectStarResponse(p) |
| 123 | + } |
| 124 | + |
| 125 | + addCursorHeader(w, ares.Cursor) |
| 126 | + |
| 127 | + return projectStars, nil |
| 128 | +} |
| 129 | + |
| 130 | +func createProjectStarResponse(o *cstypes.ProjectStar) *gwapitypes.ProjectStarResponse { |
| 131 | + org := &gwapitypes.ProjectStarResponse{ |
| 132 | + ID: o.ID, |
| 133 | + ProjectID: o.ProjectID, |
| 134 | + } |
| 135 | + return org |
| 136 | +} |
0 commit comments