Skip to content

Commit

Permalink
Merge pull request #436 from seungkyua/20240425_change_projects_function
Browse files Browse the repository at this point in the history
20240425 change projects function
  • Loading branch information
ktkfree authored Apr 25, 2024
2 parents 990049b + 5b08f01 commit eb973fe
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 37 deletions.
6 changes: 6 additions & 0 deletions api/swagger/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14485,6 +14485,12 @@ const docTemplate = `{
"organizationId": {
"type": "string"
},
"projectLeaderId": {
"type": "string"
},
"projectLeaderName": {
"type": "string"
},
"projectRoleId": {
"type": "string"
},
Expand Down
6 changes: 6 additions & 0 deletions api/swagger/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -14479,6 +14479,12 @@
"organizationId": {
"type": "string"
},
"projectLeaderId": {
"type": "string"
},
"projectLeaderName": {
"type": "string"
},
"projectRoleId": {
"type": "string"
},
Expand Down
4 changes: 4 additions & 0 deletions api/swagger/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2833,6 +2833,10 @@ definitions:
type: integer
organizationId:
type: string
projectLeaderId:
type: string
projectLeaderName:
type: string
projectRoleId:
type: string
projectRoleName:
Expand Down
56 changes: 36 additions & 20 deletions internal/delivery/http/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,9 @@ func (p ProjectHandler) GetProjects(w http.ResponseWriter, r *http.Request) {
}

if pr == nil {
ResponseJSON(w, r, http.StatusNotFound, domain.GetProjectsResponse{})
} else {
ResponseJSON(w, r, http.StatusOK, out)
out.Projects = make([]domain.ProjectResponse, 0)
}
ResponseJSON(w, r, http.StatusOK, out)
}

// Admin_GetProjects godoc
Expand Down Expand Up @@ -293,25 +292,47 @@ func (p ProjectHandler) GetProject(w http.ResponseWriter, r *http.Request) {
ErrorJSON(w, r, err)
return
}
if project == nil {
project, err := p.usecase.GetProject(r.Context(), organizationId, projectId)
if err != nil {
log.Error(r.Context(), "Failed to retrieve project", err)
ErrorJSON(w, r, err)
return
}
if project == nil {
ResponseJSON(w, r, http.StatusOK, domain.GetProjectResponse{})
}
}

var projectRoleId, projectRoleName string
requestUserInfo, ok := request.UserFrom(r.Context())
if !ok {
ErrorJSON(w, r, fmt.Errorf("failed to retrieve user info from request"))
}
userProjectRole := requestUserInfo.GetRoleProjectMapping()
if userProjectRole != nil {
projectRoleName = userProjectRole[project.ID]
}
projectRoles, err := p.usecase.GetProjectRoles(r.Context(), usecase.ProjectAll)
myUserId := requestUserInfo.GetUserId().String()

var projectRoleId, projectRoleName string
//userProjectRole := requestUserInfo.GetRoleProjectMapping()
//if userProjectRole != nil {
// projectRoleName = userProjectRole[project.ID]
//}
//projectRoles, err := p.usecase.GetProjectRoles(r.Context(), usecase.ProjectAll)
//if err != nil {
// ErrorJSON(w, r, fmt.Errorf("failed to retrieve project role"))
//}
//for _, projectRole := range projectRoles {
// if projectRoleName == projectRole.Name {
// projectRoleId = projectRole.ID
// break
// }
//}

pm, err := p.usecase.GetProjectMemberByUserId(r.Context(), project.ID, myUserId)
if err != nil {
ErrorJSON(w, r, fmt.Errorf("failed to retrieve project role"))
log.Warnf(r.Context(), "failed to retrieve project member %+v", err)
}
for _, projectRole := range projectRoles {
if projectRoleName == projectRole.Name {
projectRoleId = projectRole.ID
break
}
if pm != nil {
projectRoleId = pm.ProjectRole.ID
projectRoleName = pm.ProjectRole.Name
}

//appCount, err := p.usecase.GetAppCount(organizationId, projectId)
Expand All @@ -322,11 +343,6 @@ func (p ProjectHandler) GetProject(w http.ResponseWriter, r *http.Request) {
//}

var out domain.GetProjectResponse
if project == nil {
ResponseJSON(w, r, http.StatusNotFound, out)
return
}

var projectLeaderId, projectLeaderName, projectLeaderAccountId, projectLeaderDepartment string
for _, pu := range project.ProjectMembers {
projectLeaderId = pu.ProjectUser.ID.String()
Expand Down
43 changes: 38 additions & 5 deletions internal/repository/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,13 @@ func (r *ProjectRepository) CreateProject(ctx context.Context, p *model.Project)

func (r *ProjectRepository) GetProjects(ctx context.Context, organizationId string, userId uuid.UUID, projectName string, pg *pagination.Pagination) (pr []domain.ProjectResponse, err error) {
res := r.db.WithContext(ctx).Raw(""+
"select id, organization_id, name, description, created_at, is_my_project, project_role_id, project_role_name, namespace_count, app_count, member_count, "+
" project_leader_id, project_leader_name"+
" from ( "+
"select distinct p.id as id, p.organization_id as organization_id, p.name as name, p.description as description, p.created_at as created_at, "+
" true as is_my_project, pm.project_role_id as project_role_id, pm.pr_name as project_role_name, "+
" pn.count as namespace_count, asa.count as app_count, pm_count.count as member_count "+
" pn.count as namespace_count, asa.count as app_count, pm_count.count as member_count, "+
" pm_leader.project_leader_id as project_leader_id, pm_leader.project_leader_name as project_leader_name "+
" from projects as p "+
" left join "+
" (select pm.project_id as project_id, pm.project_user_id as project_user_id, pm.project_role_id as project_role_id, "+
Expand All @@ -79,6 +83,11 @@ func (r *ProjectRepository) GetProjects(ctx context.Context, organizationId stri
" left join users on users.id = pm.project_user_id "+
" where pm.project_user_id = @userId) as pm on p.id = pm.project_id "+
" left join "+
" (select pm.project_id as project_id, pm.project_user_id as project_leader_id, users.name as project_leader_name "+
" from project_members as pm "+
" left join users on users.id = pm.project_user_id "+
" where pm.is_project_leader = true) as pm_leader on p.id = pm_leader.project_id "+
" left join "+
" (select p.id as project_id, count(pn.stack_id || pn.project_id) as count "+
" from project_namespaces as pn "+
" left join projects as p on pn.project_id = p.id "+
Expand All @@ -99,12 +108,14 @@ func (r *ProjectRepository) GetProjects(ctx context.Context, organizationId stri
" where p.organization_id = @organizationId "+
" group by p.id) as pm_count on p.id = pm_count.project_id "+
" where p.id = pm.project_id "+
" and p.id = pm_leader.project_id "+
" and p.organization_id = @organizationId "+
" and p.name like '%"+projectName+"%' "+
"union "+
"select distinct p.id as id, p.organization_id as organization_id, p.name as name, p.description as description, p.created_at as created_at, "+
" false as is_my_project, '' as project_role_id, '' as project_role_name, "+
" pn.count as namespace_count, asa.count as app_count, pm_count.count as member_count "+
" pn.count as namespace_count, asa.count as app_count, pm_count.count as member_count, "+
" pm_leader.project_leader_id as project_leader_id, pm_leader.project_leader_name as project_leader_name "+
" from projects as p "+
" left join "+
" (select pm.project_id as project_id, pm.project_user_id as project_user_id, pm.project_role_id as project_role_id, "+
Expand All @@ -115,6 +126,11 @@ func (r *ProjectRepository) GetProjects(ctx context.Context, organizationId stri
" left join users on users.id = pm.project_user_id "+
" where pm.project_user_id <> @userId) as pm on p.id = pm.project_id "+
" left join "+
" (select pm.project_id as project_id, pm.project_user_id as project_leader_id, users.name as project_leader_name "+
" from project_members as pm "+
" left join users on users.id = pm.project_user_id "+
" where pm.is_project_leader = true) as pm_leader on p.id = pm_leader.project_id "+
" left join "+
" (select p.id as project_id, count(pn.stack_id || pn.project_id) as count "+
" from project_namespaces as pn "+
" left join projects as p on pn.project_id = p.id "+
Expand All @@ -135,12 +151,15 @@ func (r *ProjectRepository) GetProjects(ctx context.Context, organizationId stri
" where p.organization_id = @organizationId "+
" group by p.id) as pm_count on p.id = pm_count.project_id"+
" where p.id = pm.project_id "+
" and p.id = pm_leader.project_id "+
" and p.organization_id = @organizationId "+
" and p.name like '%"+projectName+"%' "+
" and p.id not in (select projects.id "+
" from projects "+
" left join project_members on project_members.project_id = projects.id "+
" where project_members.project_user_id = @userId) ",
" where project_members.project_user_id = @userId) "+
") as union_project "+
"order by is_my_project desc ",
sql.Named("organizationId", organizationId), sql.Named("userId", userId)).
Scan(&pr)
if res.Error != nil {
Expand All @@ -155,7 +174,8 @@ func (r *ProjectRepository) GetProjectsByUserId(ctx context.Context, organizatio
res := r.db.WithContext(ctx).Raw(""+
"select distinct p.id as id, p.organization_id as organization_id, p.name as name, p.description as description, p.created_at as created_at, "+
" true as is_my_project, pm.project_role_id as project_role_id, pm.pr_name as project_role_name, "+
" pn.count as namespace_count, asa.count as app_count, pm_count.count as member_count "+
" pn.count as namespace_count, asa.count as app_count, pm_count.count as member_count, "+
" pm_leader.project_leader_id as project_leader_id, pm_leader.project_leader_name as project_leader_name "+
" from projects as p "+
" left join "+
" (select pm.project_id as project_id, pm.project_user_id as project_user_id, pm.project_role_id as project_role_id, "+
Expand All @@ -166,6 +186,11 @@ func (r *ProjectRepository) GetProjectsByUserId(ctx context.Context, organizatio
" left join users on users.id = pm.project_user_id "+
" where pm.project_user_id = @userId) as pm on p.id = pm.project_id "+
" left join "+
" (select pm.project_id as project_id, pm.project_user_id as project_leader_id, users.name as project_leader_name "+
" from project_members as pm "+
" left join users on users.id = pm.project_user_id "+
" where pm.is_project_leader = true) as pm_leader on p.id = pm_leader.project_id "+
" left join "+
" (select p.id as project_id, count(pn.stack_id || pn.project_id) as count "+
" from project_namespaces as pn "+
" left join projects as p on pn.project_id = p.id "+
Expand All @@ -186,6 +211,7 @@ func (r *ProjectRepository) GetProjectsByUserId(ctx context.Context, organizatio
" where p.organization_id = @organizationId "+
" group by p.id) as pm_count on p.id = pm_count.project_id "+
" where p.id = pm.project_id "+
" and p.id = pm_leader.project_id "+
" and p.organization_id = @organizationId "+
" and p.name like '%"+projectName+"%' ",
sql.Named("organizationId", organizationId), sql.Named("userId", userId)).
Expand All @@ -208,7 +234,8 @@ func (r *ProjectRepository) GetAllProjects(ctx context.Context, organizationId s
res := r.db.WithContext(ctx).Raw(""+
"select distinct p.id as id, p.organization_id as organization_id, p.name as name, p.description as description, p.created_at as created_at, "+
" false as is_my_project, pm.project_role_id as project_role_id, pm.pr_name as project_role_name, "+
" pn.count as namespace_count, asa.count as app_count, pm_count.count as member_count "+
" pn.count as namespace_count, asa.count as app_count, pm_count.count as member_count, "+
" pm_leader.project_leader_id as project_leader_id, pm_leader.project_leader_name as project_leader_name "+
" from projects as p "+
" left join "+
" (select distinct pm.project_id as project_id, '' as project_user_id, '' as project_role_id, "+
Expand All @@ -218,6 +245,11 @@ func (r *ProjectRepository) GetAllProjects(ctx context.Context, organizationId s
" left join project_roles as pr on pr.id = pm.project_role_id "+
" left join users on users.id = pm.project_user_id) as pm on p.id = pm.project_id "+
" left join "+
" (select pm.project_id as project_id, pm.project_user_id as project_leader_id, users.name as project_leader_name "+
" from project_members as pm "+
" left join users on users.id = pm.project_user_id "+
" where pm.is_project_leader = true) as pm_leader on p.id = pm_leader.project_id "+
" left join "+
" (select p.id as project_id, count(pn.stack_id || pn.project_id) as count "+
" from project_namespaces as pn "+
" left join projects as p on pn.project_id = p.id "+
Expand All @@ -238,6 +270,7 @@ func (r *ProjectRepository) GetAllProjects(ctx context.Context, organizationId s
" where p.organization_id = @organizationId "+
" group by p.id) as pm_count on p.id = pm_count.project_id "+
" where p.id = pm.project_id "+
" and p.id = pm_leader.project_id "+
" and p.organization_id = @organizationId "+
" and p.name like '%"+projectName+"%' ",
sql.Named("organizationId", organizationId)).
Expand Down
11 changes: 11 additions & 0 deletions internal/usecase/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type IProjectUsecase interface {
AddProjectMember(ctx context.Context, organizationId string, pm *model.ProjectMember) (string, error)
GetProjectUser(ctx context.Context, projectUserId string) (*model.ProjectUser, error)
GetProjectMember(ctx context.Context, projectMemberId string) (*model.ProjectMember, error)
GetProjectMemberByUserId(ctx context.Context, projectId string, userId string) (*model.ProjectMember, error)
GetProjectMembers(ctx context.Context, projectId string, query int, pg *pagination.Pagination) ([]model.ProjectMember, error)
GetProjectMemberCount(ctx context.Context, projectMemberId string) (*domain.GetProjectMemberCountResponse, error)
RemoveProjectMember(ctx context.Context, organizationId string, projectMemberId string) error
Expand Down Expand Up @@ -326,6 +327,16 @@ func (u *ProjectUsecase) GetProjectMember(ctx context.Context, projectMemberId s
return pm, nil
}

func (u *ProjectUsecase) GetProjectMemberByUserId(ctx context.Context, projectId string, userId string) (pm *model.ProjectMember, err error) {
pm, err = u.projectRepo.GetProjectMemberByUserId(ctx, projectId, userId)
if err != nil {
log.Error(ctx, err)
return pm, errors.Wrap(err, "Failed to get project member.")
}

return pm, nil
}

func (u *ProjectUsecase) GetProjectMembers(ctx context.Context, projectId string, query int, pg *pagination.Pagination) (pms []model.ProjectMember, err error) {
if query == ProjectLeader {
pms, err = u.projectRepo.GetProjectMembersByProjectIdAndRoleName(ctx, projectId, "project-leader", pg)
Expand Down
26 changes: 14 additions & 12 deletions pkg/domain/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@ package domain
import "time"

type ProjectResponse struct {
ID string `json:"id"`
OrganizationId string `json:"organizationId"`
Name string `json:"name"`
Description string `json:"description"`
IsMyProject string `json:"isMyProject"`
ProjectRoleId string `json:"projectRoleId"`
ProjectRoleName string `json:"projectRoleName"`
NamespaceCount int `json:"namespaceCount"`
AppCount int `json:"appCount"`
MemberCount int `json:"memberCount"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt *time.Time `json:"updatedAt"`
ID string `json:"id"`
OrganizationId string `json:"organizationId"`
Name string `json:"name"`
Description string `json:"description"`
IsMyProject string `json:"isMyProject"`
ProjectRoleId string `json:"projectRoleId"`
ProjectRoleName string `json:"projectRoleName"`
ProjectLeaderId string `json:"projectLeaderId"`
ProjectLeaderName string `json:"projectLeaderName"`
NamespaceCount int `json:"namespaceCount"`
AppCount int `json:"appCount"`
MemberCount int `json:"memberCount"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt *time.Time `json:"updatedAt"`
}

type GetProjectsResponse struct {
Expand Down

0 comments on commit eb973fe

Please sign in to comment.