Skip to content

Commit

Permalink
fix swagger ui and project uri path
Browse files Browse the repository at this point in the history
  • Loading branch information
seungkyua committed Feb 22, 2024
1 parent 4d94376 commit fbc46e5
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 64 deletions.
25 changes: 7 additions & 18 deletions api/swagger/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2290,7 +2290,7 @@ const docTemplate = `{
}
}
},
"/organizations/{organizationId}/projects/check/existence": {
"/organizations/{organizationId}/projects/existence": {
"get": {
"security": [
{
Expand Down Expand Up @@ -2340,7 +2340,7 @@ const docTemplate = `{
}
}
},
"/organizations/{organizationId}/projects/pass/project-roles": {
"/organizations/{organizationId}/projects/project-roles": {
"get": {
"security": [
{
Expand Down Expand Up @@ -2383,7 +2383,7 @@ const docTemplate = `{
}
}
},
"/organizations/{organizationId}/projects/pass/project-roles/{projectRoleId}": {
"/organizations/{organizationId}/projects/project-roles/{projectRoleId}": {
"get": {
"security": [
{
Expand Down Expand Up @@ -3581,7 +3581,7 @@ const docTemplate = `{
}
}
},
"/organizations/{organizationId}/projects/{projectId}/members/project-roles/count": {
"/organizations/{organizationId}/projects/{projectId}/members/count": {
"get": {
"security": [
{
Expand Down Expand Up @@ -3817,13 +3817,6 @@ const docTemplate = `{
"name": "projectId",
"in": "path",
"required": true
},
{
"type": "string",
"description": "Project Stack ID",
"name": "stackId",
"in": "path",
"required": true
}
],
"responses": {
Expand Down Expand Up @@ -3867,13 +3860,6 @@ const docTemplate = `{
"in": "path",
"required": true
},
{
"type": "string",
"description": "Stack ID",
"name": "stackId",
"in": "path",
"required": true
},
{
"description": "Request body to create project namespace",
"name": "request",
Expand Down Expand Up @@ -8539,6 +8525,9 @@ const docTemplate = `{
},
"name": {
"type": "string"
},
"projectLeaderId": {
"type": "string"
}
}
},
Expand Down
25 changes: 7 additions & 18 deletions api/swagger/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -2284,7 +2284,7 @@
}
}
},
"/organizations/{organizationId}/projects/check/existence": {
"/organizations/{organizationId}/projects/existence": {
"get": {
"security": [
{
Expand Down Expand Up @@ -2334,7 +2334,7 @@
}
}
},
"/organizations/{organizationId}/projects/pass/project-roles": {
"/organizations/{organizationId}/projects/project-roles": {
"get": {
"security": [
{
Expand Down Expand Up @@ -2377,7 +2377,7 @@
}
}
},
"/organizations/{organizationId}/projects/pass/project-roles/{projectRoleId}": {
"/organizations/{organizationId}/projects/project-roles/{projectRoleId}": {
"get": {
"security": [
{
Expand Down Expand Up @@ -3575,7 +3575,7 @@
}
}
},
"/organizations/{organizationId}/projects/{projectId}/members/project-roles/count": {
"/organizations/{organizationId}/projects/{projectId}/members/count": {
"get": {
"security": [
{
Expand Down Expand Up @@ -3811,13 +3811,6 @@
"name": "projectId",
"in": "path",
"required": true
},
{
"type": "string",
"description": "Project Stack ID",
"name": "stackId",
"in": "path",
"required": true
}
],
"responses": {
Expand Down Expand Up @@ -3861,13 +3854,6 @@
"in": "path",
"required": true
},
{
"type": "string",
"description": "Stack ID",
"name": "stackId",
"in": "path",
"required": true
},
{
"description": "Request body to create project namespace",
"name": "request",
Expand Down Expand Up @@ -8533,6 +8519,9 @@
},
"name": {
"type": "string"
},
"projectLeaderId": {
"type": "string"
}
}
},
Expand Down
20 changes: 6 additions & 14 deletions api/swagger/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2240,6 +2240,8 @@ definitions:
type: string
name:
type: string
projectLeaderId:
type: string
required:
- name
type: object
Expand Down Expand Up @@ -4694,7 +4696,7 @@ paths:
summary: Update project member Role
tags:
- Projects
/organizations/{organizationId}/projects/{projectId}/members/project-roles/count:
/organizations/{organizationId}/projects/{projectId}/members/count:
get:
consumes:
- application/json
Expand Down Expand Up @@ -4738,11 +4740,6 @@ paths:
name: projectId
required: true
type: string
- description: Project Stack ID
in: path
name: stackId
required: true
type: string
produces:
- application/json
responses:
Expand Down Expand Up @@ -4770,11 +4767,6 @@ paths:
name: projectId
required: true
type: string
- description: Stack ID
in: path
name: stackId
required: true
type: string
- description: Request body to create project namespace
in: body
name: request
Expand Down Expand Up @@ -4949,7 +4941,7 @@ paths:
summary: Check project namespace exist
tags:
- Projects
/organizations/{organizationId}/projects/check/existence:
/organizations/{organizationId}/projects/existence:
get:
consumes:
- application/json
Expand Down Expand Up @@ -4981,7 +4973,7 @@ paths:
summary: Check project name exist
tags:
- Projects
/organizations/{organizationId}/projects/pass/project-roles:
/organizations/{organizationId}/projects/project-roles:
get:
consumes:
- application/json
Expand Down Expand Up @@ -5009,7 +5001,7 @@ paths:
summary: Get project roles
tags:
- Projects
/organizations/{organizationId}/projects/pass/project-roles/{projectRoleId}:
/organizations/{organizationId}/projects/project-roles/{projectRoleId}:
get:
consumes:
- application/json
Expand Down
18 changes: 12 additions & 6 deletions internal/delivery/http/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ func (p ProjectHandler) GetProject(w http.ResponseWriter, r *http.Request) {
// @Param type query string false "type (name)"
// @Param value query string true "value (project name)"
// @Success 200 {object} domain.CheckExistedResponse
// @Router /organizations/{organizationId}/projects/check/existence [get]
// @Router /organizations/{organizationId}/projects/existence [get]
// @Security JWT
func (p ProjectHandler) IsProjectNameExist(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
Expand Down Expand Up @@ -348,18 +348,24 @@ func (p ProjectHandler) UpdateProject(w http.ResponseWriter, r *http.Request) {
}

now := time.Now()
project, err := p.usecase.GetProject(organizationId, projectId)
project, err := p.usecase.GetProjectWithLeader(organizationId, projectId)
if err != nil {

ErrorJSON(w, r, err)
return
}
if project == nil {
project, err = p.usecase.GetProject(organizationId, projectId)
if err != nil {
ErrorJSON(w, r, err)
return
}
}

project.Name = projectReq.Name
project.Description = projectReq.Description
project.UpdatedAt = &now
//project.ProjectNamespaces = nil
//project.ProjectMembers = nil

if err := p.usecase.UpdateProject(project); err != nil {
if err := p.usecase.UpdateProject(project, projectReq.ProjectLeaderId); err != nil {
ErrorJSON(w, r, err)
return
}
Expand Down
24 changes: 23 additions & 1 deletion internal/repository/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type IProjectRepository interface {
GetProjectMembersByProjectIdAndRoleName(projectId string, memberRole string) ([]domain.ProjectMember, error)
GetProjectMemberCountByProjectId(projectId string) (*domain.GetProjectMemberCountResponse, error)
GetProjectMemberById(projectMemberId string) (*domain.ProjectMember, error)
GetProjectMemberByUserId(projectId string, projectUserId string) (pm *domain.ProjectMember, err error)
RemoveProjectMember(projectMemberId string) error
UpdateProjectMemberRole(pm *domain.ProjectMember) error
CreateProjectNamespace(organizationId string, pn *domain.ProjectNamespace) error
Expand Down Expand Up @@ -280,6 +281,22 @@ func (r *ProjectRepository) GetProjectMemberById(projectMemberId string) (pm *do
return pm, nil
}

func (r *ProjectRepository) GetProjectMemberByUserId(projectId string, projectUserId string) (pm *domain.ProjectMember, err error) {
res := r.db.Preload("ProjectUser").
Joins("ProjectRole").Where("project_id = ? and project_user_id = ?", projectId, projectUserId).First(&pm)
if res.Error != nil {
if errors.Is(res.Error, gorm.ErrRecordNotFound) {
log.Info("Cannot find project member")
return nil, nil
} else {
log.Error(res.Error)
return nil, res.Error
}
}

return pm, nil
}

func (r *ProjectRepository) RemoveProjectMember(projectMemberId string) error {
res := r.db.Delete(&domain.ProjectMember{ID: projectMemberId})
if res.Error != nil {
Expand All @@ -299,7 +316,12 @@ func (r *ProjectRepository) RemoveProjectMember(projectMemberId string) error {
//}

func (r *ProjectRepository) UpdateProjectMemberRole(pm *domain.ProjectMember) error {
res := r.db.Model(&pm).Updates(domain.ProjectMember{ProjectRoleId: pm.ProjectRoleId, UpdatedAt: pm.UpdatedAt})
res := r.db.Model(&pm).Updates(
domain.ProjectMember{
ProjectRoleId: pm.ProjectRoleId,
IsProjectLeader: pm.IsProjectLeader,
UpdatedAt: pm.UpdatedAt,
})
if res.Error != nil {
return res.Error
}
Expand Down
2 changes: 1 addition & 1 deletion internal/route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@ func SetupRouter(db *gorm.DB, argoClient argowf.ArgoClient, kc keycloak.IKeycloa
r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/projects", customMiddleware.Handle(internalApi.GetProjects, http.HandlerFunc(projectHandler.GetProjects))).Methods(http.MethodGet)
r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/projects/project-roles", customMiddleware.Handle(internalApi.GetProjectRoles, http.HandlerFunc(projectHandler.GetProjectRoles))).Methods(http.MethodGet)
r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/projects/project-roles/{projectRoleId}", customMiddleware.Handle(internalApi.GetProjectRole, http.HandlerFunc(projectHandler.GetProjectRole))).Methods(http.MethodGet)
r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/projects/existence", customMiddleware.Handle(internalApi.GetProjectNamespace, http.HandlerFunc(projectHandler.IsProjectNameExist))).Methods(http.MethodGet)
r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/projects/{projectId}", customMiddleware.Handle(internalApi.GetProject, http.HandlerFunc(projectHandler.GetProject))).Methods(http.MethodGet)
r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/projects/check/existence", customMiddleware.Handle(internalApi.GetProjectNamespace, http.HandlerFunc(projectHandler.IsProjectNameExist))).Methods(http.MethodGet)
r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/projects/{projectId}", customMiddleware.Handle(internalApi.UpdateProject, http.HandlerFunc(projectHandler.UpdateProject))).Methods(http.MethodPut)
//r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/projects/{projectId}", customMiddleware.Handle(internalApi.DeleteProject, http.HandlerFunc(projectHandler.DeleteProject))).Methods(http.MethodDelete)
r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/projects/{projectId}/members", customMiddleware.Handle(internalApi.AddProjectMember, http.HandlerFunc(projectHandler.AddProjectMember))).Methods(http.MethodPost)
Expand Down
62 changes: 60 additions & 2 deletions internal/usecase/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type IProjectUsecase interface {
GetProject(organizationId string, projectId string) (*domain.Project, error)
GetProjectWithLeader(organizationId string, projectId string) (*domain.Project, error)
IsProjectNameExist(organizationId string, projectName string) (bool, error)
UpdateProject(p *domain.Project) error
UpdateProject(p *domain.Project, newLeaderId string) error
GetProjectRole(id string) (*domain.ProjectRole, error)
GetProjectRoles(int) ([]domain.ProjectRole, error)
AddProjectMember(pm *domain.ProjectMember) (string, error)
Expand Down Expand Up @@ -116,11 +116,69 @@ func (u *ProjectUsecase) IsProjectNameExist(organizationId string, projectName s
return exist, nil
}

func (u *ProjectUsecase) UpdateProject(p *domain.Project) error {
func (u *ProjectUsecase) UpdateProject(p *domain.Project, newLeaderId string) error {

var currentMemberId, currentLeaderId, projectRoleId string
for _, pm := range p.ProjectMembers {
currentMemberId = pm.ID
currentLeaderId = pm.ProjectUser.ID.String()
projectRoleId = pm.ProjectRole.ID
}
p.ProjectNamespaces = nil
p.ProjectMembers = nil

if err := u.projectRepo.UpdateProject(p); err != nil {
log.Error(err)
return errors.Wrap(err, "Failed to update project.")
}

if newLeaderId != "" && currentLeaderId != newLeaderId {
if err := u.RemoveProjectMember(currentMemberId); err != nil {
log.Error(err)
return errors.Wrap(err, "Failed to remove project member.")
}

pu, err := u.GetProjectUser(newLeaderId)
if err != nil {
return err
}
if pu == nil {
return errors.Wrap(err, "No userid")
}

pm, err := u.projectRepo.GetProjectMemberByUserId(p.ID, newLeaderId)
if err != nil {
return err
}
if pm == nil {
newPm := &domain.ProjectMember{
ProjectId: p.ID,
ProjectUserId: pu.ID,
ProjectUser: nil,
ProjectRoleId: projectRoleId,
ProjectRole: nil,
IsProjectLeader: true,
CreatedAt: *p.UpdatedAt,
}
res, err := u.AddProjectMember(newPm)
if err != nil {
return err
}
log.Infof("Added project member: %s", res)
} else {
pm.ProjectUserId = pu.ID
pm.ProjectRoleId = projectRoleId
pm.IsProjectLeader = true
pm.UpdatedAt = p.UpdatedAt
pm.ProjectUser = nil
pm.ProjectRole = nil
err := u.UpdateProjectMemberRole(pm)
if err != nil {
return err
}
}
}

return nil
}

Expand Down
Loading

0 comments on commit fbc46e5

Please sign in to comment.