Skip to content

Commit

Permalink
Merge pull request #249 from seungkyua/20240227_list_projects
Browse files Browse the repository at this point in the history
Change project lists to a single SQL statement
  • Loading branch information
ktkfree authored Feb 27, 2024
2 parents fe57b86 + 987c59a commit 70159f8
Show file tree
Hide file tree
Showing 7 changed files with 245 additions and 62 deletions.
11 changes: 10 additions & 1 deletion api/swagger/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2414,6 +2414,12 @@ const docTemplate = `{
"name": "organizationId",
"in": "path",
"required": true
},
{
"type": "string",
"description": "(all | only)",
"name": "query",
"in": "query"
}
],
"responses": {
Expand Down Expand Up @@ -7935,7 +7941,7 @@ const docTemplate = `{
"type": "object",
"properties": {
"createdAt": {
"description": "NamespaceCount int ` + "`" + `json:\"namespaceCount\"` + "`" + `\nAppCount int ` + "`" + `json:\"appCount\"` + "`" + `\nMemberCount int ` + "`" + `json:\"memberCount\"` + "`" + `",
"description": "AppCount int ` + "`" + `json:\"appCount\"` + "`" + `\nNamespaceCount int ` + "`" + `json:\"namespaceCount\"` + "`" + `\nMemberCount int ` + "`" + `json:\"memberCount\"` + "`" + `",
"type": "string"
},
"description": {
Expand Down Expand Up @@ -8105,6 +8111,9 @@ const docTemplate = `{
"id": {
"type": "string"
},
"isMyProject": {
"type": "string"
},
"memberCount": {
"type": "integer"
},
Expand Down
11 changes: 10 additions & 1 deletion api/swagger/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -2408,6 +2408,12 @@
"name": "organizationId",
"in": "path",
"required": true
},
{
"type": "string",
"description": "(all | only)",
"name": "query",
"in": "query"
}
],
"responses": {
Expand Down Expand Up @@ -7929,7 +7935,7 @@
"type": "object",
"properties": {
"createdAt": {
"description": "NamespaceCount int `json:\"namespaceCount\"`\nAppCount int `json:\"appCount\"`\nMemberCount int `json:\"memberCount\"`",
"description": "AppCount int `json:\"appCount\"`\nNamespaceCount int `json:\"namespaceCount\"`\nMemberCount int `json:\"memberCount\"`",
"type": "string"
},
"description": {
Expand Down Expand Up @@ -8099,6 +8105,9 @@
"id": {
"type": "string"
},
"isMyProject": {
"type": "string"
},
"memberCount": {
"type": "integer"
},
Expand Down
8 changes: 7 additions & 1 deletion api/swagger/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1693,8 +1693,8 @@ definitions:
properties:
createdAt:
description: |-
AppCount int `json:"appCount"`
NamespaceCount int `json:"namespaceCount"`
AppCount int `json:"appCount"`
MemberCount int `json:"memberCount"`
type: string
description:
Expand Down Expand Up @@ -1807,6 +1807,8 @@ definitions:
type: string
id:
type: string
isMyProject:
type: string
memberCount:
type: integer
name:
Expand Down Expand Up @@ -4006,6 +4008,10 @@ paths:
name: organizationId
required: true
type: string
- description: (all | only)
in: query
name: query
type: string
produces:
- application/json
responses:
Expand Down
82 changes: 38 additions & 44 deletions internal/delivery/http/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ func (p ProjectHandler) CreateProject(w http.ResponseWriter, r *http.Request) {
// @Accept json
// @Produce json
// @Param organizationId path string true "Organization ID"
// @Param query query string false "(all | only)"
// @Success 200 {object} domain.GetProjectsResponse
// @Router /organizations/{organizationId}/projects [get]
// @Security JWT
Expand All @@ -160,49 +161,27 @@ func (p ProjectHandler) GetProjects(w http.ResponseWriter, r *http.Request) {
return
}

ps, err := p.usecase.GetProjects(organizationId)
if err != nil {
log.ErrorWithContext(r.Context(), "Failed to retrieve projects ", err)
ErrorJSON(w, r, err)
return
urlParams := r.URL.Query()
queryName := urlParams.Get("query")
onlyMyProject := false
if queryName == "only" {
onlyMyProject = true
}

// get myUserId from login component
requestUserInfo, ok := request.UserFrom(r.Context())
myUserId := requestUserInfo.GetUserId().String()
prs := make([]domain.ProjectResponse, 0)
for _, project := range ps {
var projectRoleId, projectRoleName string
for _, pm := range project.ProjectMembers {
if myUserId == pm.ProjectUserId.String() {
projectRoleId = pm.ProjectRoleId
projectRoleName = pm.ProjectRole.Name
continue
}
}
// TODO: implement AppCount
pr := domain.ProjectResponse{
ID: project.ID,
OrganizationId: project.OrganizationId,
Name: project.Name,
Description: project.Description,
ProjectRoleId: projectRoleId,
ProjectRoleName: projectRoleName,
NamespaceCount: len(project.ProjectNamespaces),
AppCount: 0,
MemberCount: len(project.ProjectMembers),
CreatedAt: project.CreatedAt,
}
prs = append(prs, pr)
pr, err := p.usecase.GetProjects(organizationId, myUserId, onlyMyProject)
if err != nil {
log.ErrorWithContext(r.Context(), "Failed to retrieve projects ", err)
ErrorJSON(w, r, err)
return
}

var out domain.GetProjectsResponse
out.Projects = prs

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

Expand Down Expand Up @@ -239,6 +218,13 @@ func (p ProjectHandler) GetProject(w http.ResponseWriter, r *http.Request) {
return
}

//appCount, err := p.usecase.GetAppCount(organizationId, projectId)
//if err != nil {
// log.ErrorWithContext(r.Context(), "Failed to retrieve app count", err)
// ErrorJSON(w, r, err)
// return
//}

var out domain.GetProjectResponse
if project == nil {
ResponseJSON(w, r, http.StatusNotFound, out)
Expand Down Expand Up @@ -268,10 +254,7 @@ func (p ProjectHandler) GetProject(w http.ResponseWriter, r *http.Request) {
pdr.ProjectLeaderDepartment = projectLeaderDepartment
pdr.ProjectRoleId = projectRoleId
pdr.ProjectRoleName = projectRoleName
//pdr.NamespaceCount = len(project.ProjectNamespaces)
//pdr.MemberCount = len(project.ProjectMembers)
////TODO implement AppCount
//pdr.AppCount = 0
//pdr.AppCount = appCount

out.Project = &pdr
ResponseJSON(w, r, http.StatusOK, out)
Expand Down Expand Up @@ -1234,10 +1217,15 @@ func (p ProjectHandler) GetProjectNamespaces(w http.ResponseWriter, r *http.Requ
ErrorJSON(w, r, err)
return
}
pnr.StackName = pn.Stack.Name
//TODO: implement AppCount
pnr.AppCount = 0
appCount, err := p.usecase.GetAppCount(organizationId, projectId, pn.Namespace)
if err != nil {
log.ErrorWithContext(r.Context(), "Failed to retrieve app count", err)
ErrorJSON(w, r, err)
return
}

pnr.StackName = pn.Stack.Name
pnr.AppCount = appCount
pnrs = append(pnrs, pnr)
}

Expand Down Expand Up @@ -1293,6 +1281,13 @@ func (p ProjectHandler) GetProjectNamespace(w http.ResponseWriter, r *http.Reque
return
}

appCount, err := p.usecase.GetAppCount(organizationId, projectId, projectNamespace)
if err != nil {
log.ErrorWithContext(r.Context(), "Failed to retrieve app count", err)
ErrorJSON(w, r, err)
return
}

var out domain.GetProjectNamespaceResponse
if pn == nil {
ResponseJSON(w, r, http.StatusNotFound, out)
Expand All @@ -1306,8 +1301,7 @@ func (p ProjectHandler) GetProjectNamespace(w http.ResponseWriter, r *http.Reque
return
}
pnr.StackName = pn.Stack.Name
//TODO: implement AppCount
pnr.AppCount = 0
pnr.AppCount = appCount

out.ProjectNamespace = &pnr
ResponseJSON(w, r, http.StatusOK, out)
Expand Down
Loading

0 comments on commit 70159f8

Please sign in to comment.