Skip to content

Commit

Permalink
feature. apply filter to list api.
Browse files Browse the repository at this point in the history
  • Loading branch information
ktkfree committed Feb 27, 2024
1 parent 70159f8 commit 464b6e6
Show file tree
Hide file tree
Showing 27 changed files with 222 additions and 326 deletions.
6 changes: 6 additions & 0 deletions api/swagger/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7469,6 +7469,9 @@ const docTemplate = `{
"domain.GetProjectMembersResponse": {
"type": "object",
"properties": {
"pagination": {
"$ref": "#/definitions/domain.PaginationResponse"
},
"projectMembers": {
"type": "array",
"items": {
Expand Down Expand Up @@ -7534,6 +7537,9 @@ const docTemplate = `{
"domain.GetProjectsResponse": {
"type": "object",
"properties": {
"pagination": {
"$ref": "#/definitions/domain.PaginationResponse"
},
"projects": {
"type": "array",
"items": {
Expand Down
6 changes: 6 additions & 0 deletions api/swagger/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -7463,6 +7463,9 @@
"domain.GetProjectMembersResponse": {
"type": "object",
"properties": {
"pagination": {
"$ref": "#/definitions/domain.PaginationResponse"
},
"projectMembers": {
"type": "array",
"items": {
Expand Down Expand Up @@ -7528,6 +7531,9 @@
"domain.GetProjectsResponse": {
"type": "object",
"properties": {
"pagination": {
"$ref": "#/definitions/domain.PaginationResponse"
},
"projects": {
"type": "array",
"items": {
Expand Down
4 changes: 4 additions & 0 deletions api/swagger/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1380,6 +1380,8 @@ definitions:
type: object
domain.GetProjectMembersResponse:
properties:
pagination:
$ref: '#/definitions/domain.PaginationResponse'
projectMembers:
items:
$ref: '#/definitions/domain.ProjectMemberResponse'
Expand Down Expand Up @@ -1421,6 +1423,8 @@ definitions:
type: object
domain.GetProjectsResponse:
properties:
pagination:
$ref: '#/definitions/domain.PaginationResponse'
projects:
items:
$ref: '#/definitions/domain.ProjectResponse'
Expand Down
17 changes: 2 additions & 15 deletions internal/delivery/http/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,7 @@ func (h *AlertHandler) GetAlerts(w http.ResponseWriter, r *http.Request) {
}

urlParams := r.URL.Query()
pg, err := pagination.NewPagination(&urlParams)
if err != nil {
ErrorJSON(w, r, httpErrors.NewBadRequestError(err, "", ""))
return
}
pg := pagination.NewPagination(&urlParams)
// convert status
for i, filter := range pg.GetFilters() {
if filter.Column == "status" {
Expand Down Expand Up @@ -143,18 +139,9 @@ func (h *AlertHandler) GetAlerts(w http.ResponseWriter, r *http.Request) {
}
}

if err := serializer.Map(*pg, &out.Pagination); err != nil {
if out.Pagination, err = pg.Response(); err != nil {
log.InfoWithContext(r.Context(), err)
}
/*
outFilters := make([]domain.FilterResponse, len(pg.Filters))
for j, filter := range pg.Filters {
if err := serializer.Map(filter, &outFilters[j]); err != nil {
log.InfoWithContext(r.Context(), err)
}
}
out.Pagination.Filters = outFilters
*/

ResponseJSON(w, r, http.StatusOK, out)
}
Expand Down
8 changes: 2 additions & 6 deletions internal/delivery/http/app-group.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,7 @@ func (h *AppGroupHandler) GetAppGroups(w http.ResponseWriter, r *http.Request) {
ErrorJSON(w, r, httpErrors.NewBadRequestError(fmt.Errorf("Invalid clusterId"), "C_INVALID_CLUSTER_ID", ""))
return
}
pg, err := pagination.NewPagination(&urlParams)
if err != nil {
ErrorJSON(w, r, httpErrors.NewBadRequestError(err, "", ""))
return
}
pg := pagination.NewPagination(&urlParams)

appGroups, err := h.usecase.Fetch(r.Context(), domain.ClusterId(clusterId), pg)
if err != nil {
Expand All @@ -103,7 +99,7 @@ func (h *AppGroupHandler) GetAppGroups(w http.ResponseWriter, r *http.Request) {
}
}

if err := serializer.Map(*pg, &out.Pagination); err != nil {
if out.Pagination, err = pg.Response(); err != nil {
log.InfoWithContext(r.Context(), err)
}

Expand Down
17 changes: 4 additions & 13 deletions internal/delivery/http/app-serve-app.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,7 @@ func (h *AppServeAppHandler) GetAppServeApps(w http.ResponseWriter, r *http.Requ
ErrorJSON(w, r, err)
return
}
pg, err := pagination.NewPagination(&urlParams)
if err != nil {
ErrorJSON(w, r, httpErrors.NewBadRequestError(err, "", ""))
return
}

pg := pagination.NewPagination(&urlParams)
apps, err := h.usecase.GetAppServeApps(organizationId, showAll, pg)
if err != nil {
log.ErrorWithContext(r.Context(), "Failed to get Failed to get app-serve-apps ", err)
Expand All @@ -269,7 +264,7 @@ func (h *AppServeAppHandler) GetAppServeApps(w http.ResponseWriter, r *http.Requ
var out domain.GetAppServeAppsResponse
out.AppServeApps = apps

if err := serializer.Map(*pg, &out.Pagination); err != nil {
if out.Pagination, err = pg.Response(); err != nil {
log.InfoWithContext(r.Context(), err)
}

Expand Down Expand Up @@ -452,11 +447,7 @@ func (h *AppServeAppHandler) GetAppServeAppTasksByAppId(w http.ResponseWriter, r
}

urlParams := r.URL.Query()
pg, err := pagination.NewPagination(&urlParams)
if err != nil {
ErrorJSON(w, r, httpErrors.NewBadRequestError(err, "", ""))
return
}
pg := pagination.NewPagination(&urlParams)

tasks, err := h.usecase.GetAppServeAppTasks(appId, pg)
if err != nil {
Expand All @@ -468,7 +459,7 @@ func (h *AppServeAppHandler) GetAppServeAppTasksByAppId(w http.ResponseWriter, r
var out domain.GetAppServeAppTasksResponse
out.AppServeAppTasks = tasks

if err := serializer.Map(*pg, &out.Pagination); err != nil {
if out.Pagination, err = pg.Response(); err != nil {
log.InfoWithContext(r.Context(), err)
}

Expand Down
7 changes: 1 addition & 6 deletions internal/delivery/http/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,7 @@ func (h *AuditHandler) GetAudits(w http.ResponseWriter, r *http.Request) {
}

urlParams := r.URL.Query()
pg, err := pagination.NewPagination(&urlParams)
if err != nil {
ErrorJSON(w, r, httpErrors.NewBadRequestError(err, "", ""))
return
}

pg := pagination.NewPagination(&urlParams)
audits, err := h.usecase.Fetch(r.Context(), organizationId, pg)
if err != nil {
ErrorJSON(w, r, err)
Expand Down
8 changes: 2 additions & 6 deletions internal/delivery/http/cloud-account.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,7 @@ func (h *CloudAccountHandler) GetCloudAccounts(w http.ResponseWriter, r *http.Re
}

urlParams := r.URL.Query()
pg, err := pagination.NewPagination(&urlParams)
if err != nil {
ErrorJSON(w, r, httpErrors.NewBadRequestError(err, "", ""))
return
}
pg := pagination.NewPagination(&urlParams)
cloudAccounts, err := h.usecase.Fetch(r.Context(), organizationId, pg)
if err != nil {
ErrorJSON(w, r, err)
Expand All @@ -114,7 +110,7 @@ func (h *CloudAccountHandler) GetCloudAccounts(w http.ResponseWriter, r *http.Re
}
}

if err := serializer.Map(*pg, &out.Pagination); err != nil {
if out.Pagination, err = pg.Response(); err != nil {
log.InfoWithContext(r.Context(), err)
}

Expand Down
8 changes: 2 additions & 6 deletions internal/delivery/http/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@ func (h *ClusterHandler) GetClusters(w http.ResponseWriter, r *http.Request) {
urlParams := r.URL.Query()

organizationId := urlParams.Get("organizationId")
pg, err := pagination.NewPagination(&urlParams)
if err != nil {
ErrorJSON(w, r, httpErrors.NewBadRequestError(err, "", ""))
return
}
pg := pagination.NewPagination(&urlParams)
clusters, err := h.usecase.Fetch(r.Context(), organizationId, pg)
if err != nil {
ErrorJSON(w, r, err)
Expand All @@ -63,7 +59,7 @@ func (h *ClusterHandler) GetClusters(w http.ResponseWriter, r *http.Request) {
}
}

if err := serializer.Map(*pg, &out.Pagination); err != nil {
if out.Pagination, err = pg.Response(); err != nil {
log.InfoWithContext(r.Context(), err)
}

Expand Down
9 changes: 2 additions & 7 deletions internal/delivery/http/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,7 @@ func (h *OrganizationHandler) CreateOrganization(w http.ResponseWriter, r *http.
// @Security JWT
func (h *OrganizationHandler) GetOrganizations(w http.ResponseWriter, r *http.Request) {
urlParams := r.URL.Query()
pg, err := pagination.NewPagination(&urlParams)
if err != nil {
ErrorJSON(w, r, httpErrors.NewBadRequestError(err, "", ""))
return
}

pg := pagination.NewPagination(&urlParams)
organizations, err := h.usecase.Fetch(pg)
if err != nil {
log.ErrorfWithContext(r.Context(), "error is :%s(%T)", err.Error(), err)
Expand All @@ -116,7 +111,7 @@ func (h *OrganizationHandler) GetOrganizations(w http.ResponseWriter, r *http.Re
log.InfoWithContext(r.Context(), organization)
}

if err := serializer.Map(*pg, &out.Pagination); err != nil {
if out.Pagination, err = pg.Response(); err != nil {
log.InfoWithContext(r.Context(), err)
}

Expand Down
36 changes: 27 additions & 9 deletions internal/delivery/http/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/google/uuid"
"github.com/openinfradev/tks-api/internal/middleware/auth/request"
"github.com/openinfradev/tks-api/internal/pagination"
"github.com/openinfradev/tks-api/internal/serializer"

"github.com/gorilla/mux"
Expand Down Expand Up @@ -168,20 +169,28 @@ func (p ProjectHandler) GetProjects(w http.ResponseWriter, r *http.Request) {
onlyMyProject = true
}

pg := pagination.NewPagination(&urlParams)

// get myUserId from login component
requestUserInfo, ok := request.UserFrom(r.Context())
myUserId := requestUserInfo.GetUserId().String()
pr, err := p.usecase.GetProjects(organizationId, myUserId, onlyMyProject)
pr, err := p.usecase.GetProjects(organizationId, myUserId, onlyMyProject, pg)
if err != nil {
log.ErrorWithContext(r.Context(), "Failed to retrieve projects ", err)
ErrorJSON(w, r, err)
return
}

var out domain.GetProjectsResponse
out.Projects = pr
if out.Pagination, err = pg.Response(); err != nil {
log.InfoWithContext(r.Context(), err)
}

if pr == nil {
ResponseJSON(w, r, http.StatusNotFound, domain.GetProjectsResponse{})
} else {
ResponseJSON(w, r, http.StatusOK, pr)
ResponseJSON(w, r, http.StatusOK, out)
}
}

Expand Down Expand Up @@ -483,7 +492,9 @@ func (p ProjectHandler) AddProjectMember(w http.ResponseWriter, r *http.Request)
return
}

pns, err := p.usecase.GetProjectNamespaces(organizationId, projectId)
urlParams := r.URL.Query()
pg := pagination.NewPagination(&urlParams)
pns, err := p.usecase.GetProjectNamespaces(organizationId, projectId, pg)
if err != nil {
log.Error(err)
ErrorJSON(w, r, httpErrors.NewInternalServerError(err, "", ""))
Expand Down Expand Up @@ -660,7 +671,8 @@ func (p ProjectHandler) GetProjectMembers(w http.ResponseWriter, r *http.Request
return
}

pms, err := p.usecase.GetProjectMembers(projectId, query)
pg := pagination.NewPagination(&urlParams)
pms, err := p.usecase.GetProjectMembers(projectId, query, pg)
if err != nil {
log.ErrorWithContext(r.Context(), "Failed to get project members ", err)
ErrorJSON(w, r, err)
Expand Down Expand Up @@ -692,6 +704,10 @@ func (p ProjectHandler) GetProjectMembers(w http.ResponseWriter, r *http.Request
}

out = domain.GetProjectMembersResponse{ProjectMembers: pmrs}
if out.Pagination, err = pg.Response(); err != nil {
log.InfoWithContext(r.Context(), err)
}

ResponseJSON(w, r, http.StatusOK, out)
}

Expand Down Expand Up @@ -776,7 +792,7 @@ func (p ProjectHandler) RemoveProjectMember(w http.ResponseWriter, r *http.Reque
}

// tasks for keycloak & k8s
pns, err := p.usecase.GetProjectNamespaces(organizationId, projectId)
pns, err := p.usecase.GetProjectNamespaces(organizationId, projectId, nil)
if err != nil {
log.Error(err)
ErrorJSON(w, r, httpErrors.NewInternalServerError(err, "", ""))
Expand Down Expand Up @@ -839,7 +855,7 @@ func (p ProjectHandler) RemoveProjectMembers(w http.ResponseWriter, r *http.Requ
}

// tasks for keycloak & k8s
pns, err := p.usecase.GetProjectNamespaces(organizationId, projectId)
pns, err := p.usecase.GetProjectNamespaces(organizationId, projectId, nil)
if err != nil {
log.Error(err)
ErrorJSON(w, r, httpErrors.NewInternalServerError(err, "", ""))
Expand Down Expand Up @@ -926,7 +942,7 @@ func (p ProjectHandler) UpdateProjectMemberRole(w http.ResponseWriter, r *http.R
return
}

pns, err := p.usecase.GetProjectNamespaces(organizationId, projectId)
pns, err := p.usecase.GetProjectNamespaces(organizationId, projectId, nil)
if err != nil {
log.Error(err)
ErrorJSON(w, r, httpErrors.NewInternalServerError(err, "", ""))
Expand Down Expand Up @@ -1003,7 +1019,7 @@ func (p ProjectHandler) UpdateProjectMembersRole(w http.ResponseWriter, r *http.
return
}

pns, err := p.usecase.GetProjectNamespaces(organizationId, projectId)
pns, err := p.usecase.GetProjectNamespaces(organizationId, projectId, nil)
if err != nil {
log.Error(err)
ErrorJSON(w, r, httpErrors.NewInternalServerError(err, "", ""))
Expand Down Expand Up @@ -1197,7 +1213,9 @@ func (p ProjectHandler) GetProjectNamespaces(w http.ResponseWriter, r *http.Requ
return
}

pns, err := p.usecase.GetProjectNamespaces(organizationId, projectId)
urlParams := r.URL.Query()
pg := pagination.NewPagination(&urlParams)
pns, err := p.usecase.GetProjectNamespaces(organizationId, projectId, pg)
if err != nil {
log.ErrorWithContext(r.Context(), "Failed to get project namespaces.", err)
ErrorJSON(w, r, err)
Expand Down
7 changes: 1 addition & 6 deletions internal/delivery/http/stack-template.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,7 @@ func (h *StackTemplateHandler) CreateStackTemplate(w http.ResponseWriter, r *htt
// @Security JWT
func (h *StackTemplateHandler) GetStackTemplates(w http.ResponseWriter, r *http.Request) {
urlParams := r.URL.Query()
pg, err := pagination.NewPagination(&urlParams)
if err != nil {
ErrorJSON(w, r, httpErrors.NewBadRequestError(err, "", ""))
return
}

pg := pagination.NewPagination(&urlParams)
stackTemplates, err := h.usecase.Fetch(r.Context(), pg)
if err != nil {
ErrorJSON(w, r, err)
Expand Down
8 changes: 2 additions & 6 deletions internal/delivery/http/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,7 @@ func (h *StackHandler) GetStacks(w http.ResponseWriter, r *http.Request) {
}

urlParams := r.URL.Query()
pg, err := pagination.NewPagination(&urlParams)
if err != nil {
ErrorJSON(w, r, httpErrors.NewBadRequestError(err, "", ""))
return
}
pg := pagination.NewPagination(&urlParams)
stacks, err := h.usecase.Fetch(r.Context(), organizationId, pg)
if err != nil {
ErrorJSON(w, r, err)
Expand All @@ -137,7 +133,7 @@ func (h *StackHandler) GetStacks(w http.ResponseWriter, r *http.Request) {
}
}

if err := serializer.Map(*pg, &out.Pagination); err != nil {
if out.Pagination, err = pg.Response(); err != nil {
log.InfoWithContext(r.Context(), err)
}

Expand Down
Loading

0 comments on commit 464b6e6

Please sign in to comment.