Skip to content

Commit

Permalink
Modify the logic that manages the project
Browse files Browse the repository at this point in the history
  • Loading branch information
seungkyua committed Feb 15, 2024
1 parent ff35f44 commit 5ac3c66
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 62 deletions.
96 changes: 62 additions & 34 deletions internal/delivery/http/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,23 @@ func (p ProjectHandler) CreateProject(w http.ResponseWriter, r *http.Request) {
ErrorJSON(w, r, httpErrors.NewInternalServerError(err, "", "Failed to parse uuid to string"))
return
}

prs, err := p.usecase.GetProjectRoles(usecase.ProjectLeader)
if err != nil {
log.Error(err)
ErrorJSON(w, r, httpErrors.NewInternalServerError(err, "", "Failed to retrieve project-leader id"))
return
}

//Don't add ProjectUser Object because of Cascading
pm := &domain.ProjectMember{
ProjectId: projectId,
//ProjectUser: &domain.ProjectUser{ID: ProjectLeaderId},
//ProjectRole: &domain.ProjectRole{ID: projectReq.ProjectRoleId},
ProjectUserId: ProjectLeaderId,
ProjectRoleId: projectReq.ProjectRoleId,
CreatedAt: now,
ProjectUserId: ProjectLeaderId,
ProjectRoleId: prs[0].ID,
IsProjectLeader: true,
CreatedAt: now,
}

projectMemberId, err := p.usecase.AddProjectMember(pm)
Expand All @@ -119,22 +128,6 @@ func (p ProjectHandler) CreateProject(w http.ResponseWriter, r *http.Request) {
return
}

//pr, err := p.usecase.GetProjectRoles(usecase.ProjectLeader)
//if err != nil {
// ErrorJSON(w, r, httpErrors.NewInternalServerError(err, "", ""))
// return
//}
//
//pms := make([]domain.ProjectMember, 0)
//pm.ID = projectMemberId
//pm.ProjectRole = pr[0]
//pms = append(pms, *pm)
//
//project.ProjectMembers = pms
//projectRes := domain.CreateProjectResponse{
// Project: *project,
//}

out := domain.CreateProjectResponse{ProjectId: projectId}
ResponseJSON(w, r, http.StatusOK, out)

Expand Down Expand Up @@ -229,7 +222,7 @@ func (p ProjectHandler) GetProject(w http.ResponseWriter, r *http.Request) {
return
}

project, err := p.usecase.GetProject(organizationId, projectId)
project, err := p.usecase.GetProjectWithLeader(organizationId, projectId)
if err != nil {
log.ErrorWithContext(r.Context(), "Failed to retrieve project", err)
ErrorJSON(w, r, err)
Expand All @@ -243,13 +236,14 @@ func (p ProjectHandler) GetProject(w http.ResponseWriter, r *http.Request) {
}

var projectLeaderId, projectLeaderName, projectLeaderAccountId, projectLeaderDepartment string
var projectRoleId, projectRoleName string
for _, pu := range project.ProjectMembers {
if pu.ProjectRole.Name == "project-leader" {
projectLeaderId = pu.ProjectUser.ID.String()
projectLeaderName = pu.ProjectUser.Name
projectLeaderAccountId = pu.ProjectUser.AccountId
projectLeaderDepartment = pu.ProjectUser.Department
}
projectLeaderId = pu.ProjectUser.ID.String()
projectLeaderName = pu.ProjectUser.Name
projectLeaderAccountId = pu.ProjectUser.AccountId
projectLeaderDepartment = pu.ProjectUser.Department
projectRoleId = pu.ProjectRole.ID
projectRoleName = pu.ProjectRole.Name
}

var pdr domain.ProjectDetailResponse
Expand All @@ -262,10 +256,12 @@ func (p ProjectHandler) GetProject(w http.ResponseWriter, r *http.Request) {
pdr.ProjectLeaderName = projectLeaderName
pdr.ProjectLeaderAccountId = projectLeaderAccountId
pdr.ProjectLeaderDepartment = projectLeaderDepartment
pdr.NamespaceCount = len(project.ProjectNamespaces)
pdr.MemberCount = len(project.ProjectMembers)
//TODO implement AppCount
pdr.AppCount = 0
pdr.ProjectRoleId = projectRoleId
pdr.ProjectRoleName = projectRoleName
//pdr.NamespaceCount = len(project.ProjectNamespaces)
//pdr.MemberCount = len(project.ProjectMembers)
////TODO implement AppCount
//pdr.AppCount = 0

out.Project = &pdr
ResponseJSON(w, r, http.StatusOK, out)
Expand Down Expand Up @@ -315,8 +311,8 @@ func (p ProjectHandler) UpdateProject(w http.ResponseWriter, r *http.Request) {
project.Name = projectReq.Name
project.Description = projectReq.Description
project.UpdatedAt = &now
project.ProjectNamespaces = nil
project.ProjectMembers = nil
//project.ProjectNamespaces = nil
//project.ProjectMembers = nil

if err := p.usecase.UpdateProject(project); err != nil {
ErrorJSON(w, r, err)
Expand Down Expand Up @@ -537,7 +533,21 @@ func (p ProjectHandler) GetProjectMember(w http.ResponseWriter, r *http.Request)
return
}

out := domain.GetProjectMemberResponse{ProjectMember: pm}
pmr := &domain.ProjectMemberResponse{
ID: pm.ID,
ProjectId: pm.ProjectId,
ProjectUserId: pm.ProjectUser.ID.String(),
ProjectUserName: pm.ProjectUser.Name,
ProjectUserAccountId: pm.ProjectUser.AccountId,
ProjectUserEmail: pm.ProjectUser.Email,
ProjectUserDepartment: pm.ProjectUser.Department,
ProjectRoleId: pm.ProjectRole.ID,
ProjectRoleName: pm.ProjectRole.Name,
CreatedAt: pm.CreatedAt,
UpdatedAt: pm.UpdatedAt,
}

out := domain.GetProjectMemberResponse{ProjectMember: pmr}
if pm == nil {
ResponseJSON(w, r, http.StatusNotFound, out)
return
Expand Down Expand Up @@ -580,7 +590,25 @@ func (p ProjectHandler) GetProjectMembers(w http.ResponseWriter, r *http.Request
return
}

out := domain.GetProjectMembersResponse{ProjectMembers: pms}
pmrs := make([]domain.ProjectMemberResponse, 0)
for _, pm := range pms {
pmr := domain.ProjectMemberResponse{
ID: pm.ID,
ProjectId: pm.ProjectId,
ProjectUserId: pm.ProjectUser.ID.String(),
ProjectUserName: pm.ProjectUser.Name,
ProjectUserAccountId: pm.ProjectUser.AccountId,
ProjectUserEmail: pm.ProjectUser.Email,
ProjectUserDepartment: pm.ProjectUser.Department,
ProjectRoleId: pm.ProjectRole.ID,
ProjectRoleName: pm.ProjectRole.Name,
CreatedAt: pm.CreatedAt,
UpdatedAt: pm.UpdatedAt,
}
pmrs = append(pmrs, pmr)
}

out := domain.GetProjectMembersResponse{ProjectMembers: pmrs}

ResponseJSON(w, r, http.StatusOK, out)
}
Expand Down
18 changes: 17 additions & 1 deletion internal/repository/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type IProjectRepository interface {
CreateProject(p *domain.Project) (string, error)
GetProjects(organizationId string) ([]domain.Project, error)
GetProjectById(organizationId string, projectId string) (*domain.Project, error)
GetProjectByIdAndLeader(organizationId string, projectId string) (*domain.Project, error)
UpdateProject(p *domain.Project) error
GetAllProjectRoles() ([]domain.ProjectRole, error)
GetProjectRoleByName(name string) (*domain.ProjectRole, error)
Expand Down Expand Up @@ -67,8 +68,23 @@ func (r *ProjectRepository) GetProjects(organizationId string) (ps []domain.Proj
}

func (r *ProjectRepository) GetProjectById(organizationId string, projectId string) (p *domain.Project, err error) {
res := r.db.Limit(1).Where("organization_id = ? and id = ?", organizationId, projectId).First(&p)
if res.Error != nil {
if errors.Is(res.Error, gorm.ErrRecordNotFound) {
log.Info("Cannot find project")
return nil, nil
} else {
log.Error(res.Error)
return nil, res.Error
}
}

return p, nil
}

func (r *ProjectRepository) GetProjectByIdAndLeader(organizationId string, projectId string) (p *domain.Project, err error) {
res := r.db.Limit(1).Where("organization_id = ? and id = ?", organizationId, projectId).
Preload("ProjectMembers").
Limit(1).Preload("ProjectMembers", "is_project_leader = ?", true).
Preload("ProjectMembers.ProjectRole").
Preload("ProjectMembers.ProjectUser").
First(&p)
Expand Down
10 changes: 10 additions & 0 deletions internal/usecase/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type IProjectUsecase interface {
CreateProject(*domain.Project) (string, error)
GetProjects(organizationId string) ([]domain.Project, error)
GetProject(organizationId string, projectId string) (*domain.Project, error)
GetProjectWithLeader(organizationId string, projectId string) (*domain.Project, error)
UpdateProject(p *domain.Project) error
GetProjectRole(id string) (*domain.ProjectRole, error)
GetProjectRoles(int) ([]domain.ProjectRole, error)
Expand Down Expand Up @@ -86,6 +87,15 @@ func (u *ProjectUsecase) GetProject(organizationId string, projectId string) (*d
return p, err
}

func (u *ProjectUsecase) GetProjectWithLeader(organizationId string, projectId string) (*domain.Project, error) {
p, err := u.projectRepo.GetProjectByIdAndLeader(organizationId, projectId)
if err != nil {
log.Error(err)
return nil, errors.Wrap(err, "Failed to get projects.")
}
return p, err
}

func (u *ProjectUsecase) UpdateProject(p *domain.Project) error {
if err := u.projectRepo.UpdateProject(p); err != nil {
log.Error(err)
Expand Down
70 changes: 43 additions & 27 deletions pkg/domain/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ type Project struct {
CreatedAt time.Time `gorm:"autoCreateTime:false" json:"createdAt"`
UpdatedAt *time.Time `gorm:"autoUpdateTime:false" json:"updatedAt"`
DeletedAt *time.Time `json:"deletedAt"`
ProjectMembers []ProjectMember `gorm:"foreignKey:ProjectId" json:"projectMembers,omitempty"`
ProjectNamespaces []ProjectNamespace `gorm:"foreignKey:ProjectId" json:"projectNamespaces,omitempty"`
ProjectMembers []ProjectMember `gorm:"foreignKey:ProjectId;constraint:OnUpdate:RESTRICT,OnDelete:RESTRICT" json:"projectMembers,omitempty"`
ProjectNamespaces []ProjectNamespace `gorm:"foreignKey:ProjectId;constraint:OnUpdate:RESTRICT,OnDelete:RESTRICT" json:"projectNamespaces,omitempty"`
}

type ProjectResponse struct {
Expand All @@ -61,19 +61,21 @@ type GetProjectsResponse struct {
}

type ProjectDetailResponse struct {
ID string `json:"id"`
OrganizationId string `json:"organizationId"`
Name string `json:"name"`
Description string `json:"description"`
ProjectLeaderId string `json:"projectLeaderId"`
ProjectLeaderName string `json:"projectLeaderName"`
ProjectLeaderAccountId string `json:"projectLeaderAccountId"`
ProjectLeaderDepartment string `json:"projectLeaderDepartment"`
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"`
ProjectLeaderId string `json:"projectLeaderId"`
ProjectLeaderName string `json:"projectLeaderName"`
ProjectLeaderAccountId string `json:"projectLeaderAccountId"`
ProjectLeaderDepartment string `json:"projectLeaderDepartment"`
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"`
}

type GetProjectResponse struct {
Expand Down Expand Up @@ -105,15 +107,16 @@ func (ProjectUser) TableName() string {
}

type ProjectMember struct {
ID string `gorm:"primarykey" json:"id"`
ProjectId string `gorm:"not null" json:"projectId"`
ProjectUserId uuid.UUID `json:"projectUserId"`
ProjectUser *ProjectUser `gorm:"foreignKey:ProjectUserId;references:ID;constraint:OnUpdate:RESTRICT,OnDelete:RESTRICT" json:"projectUser"`
ProjectRoleId string `json:"projectRoleId"`
ProjectRole *ProjectRole `gorm:"foreignKey:ProjectRoleId;references:ID;constraint:OnUpdate:RESTRICT,OnDelete:RESTRICT" json:"projectRole"`
CreatedAt time.Time `gorm:"autoCreateTime:false" json:"createdAt"`
UpdatedAt *time.Time `gorm:"autoUpdateTime:false" json:"updatedAt"`
DeletedAt *time.Time `json:"deletedAt"`
ID string `gorm:"primarykey" json:"id"`
ProjectId string `gorm:"not null" json:"projectId"`
ProjectUserId uuid.UUID `json:"projectUserId"`
ProjectUser *ProjectUser `gorm:"foreignKey:ProjectUserId;references:ID;constraint:OnUpdate:RESTRICT,OnDelete:RESTRICT" json:"projectUser"`
ProjectRoleId string `json:"projectRoleId"`
ProjectRole *ProjectRole `gorm:"foreignKey:ProjectRoleId;references:ID;constraint:OnUpdate:RESTRICT,OnDelete:RESTRICT" json:"projectRole"`
IsProjectLeader bool `gorm:"default:false" json:"projectLeader"`
CreatedAt time.Time `gorm:"autoCreateTime:false" json:"createdAt"`
UpdatedAt *time.Time `gorm:"autoUpdateTime:false" json:"updatedAt"`
DeletedAt *time.Time `json:"deletedAt"`
}

type ProjectStack struct {
Expand Down Expand Up @@ -142,7 +145,6 @@ type CreateProjectRequest struct {
Name string `json:"name" validate:"required"`
Description string `json:"description"`
ProjectLeaderId string `json:"projectLeaderId"`
ProjectRoleId string `json:"projectRoleId"`
}

type CreateProjectResponse struct {
Expand Down Expand Up @@ -172,12 +174,26 @@ type AddProjectMemberRequest struct {
ProjectMemberRequests []ProjectMemberRequest `json:"projectMembers"`
}

type ProjectMemberResponse struct {
ID string `json:"id"`
ProjectId string `json:"projectId"`
ProjectUserId string `json:"projectUserId"`
ProjectUserName string `json:"projectUserName"`
ProjectUserAccountId string `json:"projectUserAccountId"`
ProjectUserEmail string `json:"projectUserEmail"`
ProjectUserDepartment string `json:"projectUserDepartment"`
ProjectRoleId string `json:"projectRoleId"`
ProjectRoleName string `json:"projectRoleName"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt *time.Time `json:"updatedAt"`
}

type GetProjectMemberResponse struct {
ProjectMember *ProjectMember `json:"projectMember"`
ProjectMember *ProjectMemberResponse `json:"projectMember"`
}

type GetProjectMembersResponse struct {
ProjectMembers []ProjectMember `json:"projectMembers"`
ProjectMembers []ProjectMemberResponse `json:"projectMembers"`
}

type RemoveProjectMemberRequest struct {
Expand Down

0 comments on commit 5ac3c66

Please sign in to comment.